Send DOPUS filepaths to node server

So i have 100s of filepaths I need to process and editing DOPUS JScript is difficult in the DOPUS SCRIPT EDITOR. So i want to send filepaths in a collection (recursive) to an external program. I tried using a txt file intermediary, but that is super slow as there are too many files. I tried using DOPUSRT and clipboard but that was very limited in terms of number of filepaths.

So then i tried CHATGPT and came up with using ActiveX to send to a node server. The node server works fine. But the DOPUS script does not seem to send anything.

`
    tab = clickData.func.sourcetab;
    xhr = new ActiveXObject("MSXML2.ServerXMLHTTP");  // Use MSXML2.ServerXMLHTTP

    try {
        xhr.open("POST", url, false);
        xhr.setRequestHeader("Content-Type", "application/json");

        fileList = [];
        for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
            var item = e.item();
            var realpath = item.realpath;
            var json_path = replaceBackslashes(realpath);
            fileList.push(json_path);
        }

        var json_data = JSON.stringify(fileList);
        DOpus.Output("Sending data: " + json_data);  // Log the data being sent
        xhr.send(json_data);

        DOpus.Output("Status: " + xhr.status);  // Log the status code
        DOpus.Output("Response: " + xhr.responseText);  // Log the response text

        DOpus.Output("File list sent to Node.js server.");
    } catch (e) {
        DOpus.Output("Error sending file list: " + e.message);
    }
}`


Any help would be appreciated? 

That should not be slow. How were you generating the text file? How was it then being read? How long did each one take?

Keeping it simple is best, and using simple temp-files for the lists should work fine.

DOpusRT can output to a file as well. I'm not sure if there's a limit on clipboard text length, but outputting to the clipboard wouldn't be appropriate anyway: every time you used it, it would trash whatever was in your clipboard. Reading and writing the clipboard shouldn't be much faster/different to reading and writing a temp-file, so there isn't any reason to use the clipboard for this, IMO.

I would stick to using files for the list of paths, and work out why it was slow, because it shouldn't be.

thx for reply. Using a JScript to check subfolders and export to a text file - and that's what seems to take the most time. What is the best way to export recursively to a txt file?

Opus can do that for you, from a normal (non-script) command:

"C:\Example\Thing.exe" {allfilepath|filem|utf8|nobom}

That will run Thing.exe with a single file path, which points to a text file.

The text file will list all the selected paths, one per line. It'll be in UTF-8 format without a BOM (byte order mark), which is most likely to work with JavaScript and most modern tools/languages.

Opus will delete the file automatically after Thing.exe exits. (Not always immediately, but it will clean it up on a schedule, and you don't have to worry about deleting it at the other end.)

Detail about what the codes mean, and other similar codes:

What happens if no files are selected....? Will it list ALL OF THE FILES, including files in subfolders ? Will it work with collections ?
BTW - not quite sure what thing.exe is all about ? Is that an external app to which will use the text file as argument ?

Thing.exe is an example, you would substitute the name of the tool or script you want to run.

allfilepath is passing selected files.
If no file is selected, it will call without argument. If you want to avoid calling without argument, you need to use allfilepath$

A few things I find unclear about what you describe are :

  • You're talking about your first script taking time : it should not, it's straightforward and just enumerates selected files, put them in an array and then call an url with them (in json).
  • You're also mentioning recursivity multiple times as in : going inside folders to fetch all files and for subfolders to fetch all files, and for subfolders to fetch all files, ... : but your script is not doing that, maybe it is what your node server is doing afterward. Note that analyzing subfolders recursively can take time if folder structure is deep and/or a lot of files are found (and especially if your node server is executing something for each file).
  • You're saying your node server is OK but Opus is not sending anything. Since you log what is being sent to your server (DOpus.Output("Sending data: " + json_data);) : what is that output is saying ?

And finally :

There is an option to replace Opus script editor with the one of your choice (Should be somewhere in Misc/External Tools

Additionally, there's no need to use the outdated (and lame) HTTP Request ActiveX, as DOpus provides a more powerful option.

Thanks so much. Great replies. I am relatively new to js, so will need to digest your replies.

Dialogs in dopus are still fairly tricky for me, but I will master soon enough.

I went to httprequest page you provided. Although it describes all methods and properties, is there a short working script on dopus site that I could study (especially since the Dialogue obj comes into play

  • and I am still trying to understand it)

Thx

First result of this forum search for HTTPRequest looks like a good starting point :

This one is also very illustrative, specially when you're trying to use POST sending a json.

thx for everyones help :innocent:

I am pulling hair out :woozy_face:. This is what I (and AI) came up with, but cannot get past the

req.open("POST", "http://localhost:3000", true);

keep getting error: Object doesn't support this property or method

	fileList = [
	    "C:/Users/JohnDoe/Documents/file1.txt",
	    "C:/Users/JohnDoe/Documents/file2.txt",
	    "C:/Users/JohnDoe/Documents/file3.txt"
	]; //using forward slashes to prep for node server

    var Dlg = DOpus.Dlg;
    Dlg.window = clickData.func.sourcetab;
    Dlg.template = "testdlg";
    Dlg.detach = true;
	Dlg.Show();

	
	jsonString = JSON.stringify(fileList); 
	DOpus.Output(jsonString)

	var req = Dlg.NewHTTPReq();
	req.open("POST", "http://localhost:3000", true);
	req.setRequestHeader("Content-Type", "application/json");
	
	req.onreadystatechange = function() {
	    if (req.readyState === 4 && req.status === 200) {
	        console.log("Response received:", req.responseText);
	    }
	};

	// Add error handling for the request
	req.onerror = function() {
	    console.error("Request failed");
	};
	
	// Send the JSON data
	req.send(jsonString);

BTW i am still learning about Diaolgues in DOPUS

I don't know much about HTTPReq object, but I do know what you're doing is illegal :slight_smile:

Some basics (sorry if I go backwards too much) : in Object Oriented Programming, you're dealing (most of the time) with objects. Such objects have :

  • A way to create them
  • Properties, which are values (or other objects) that define them (think of radius property for a Circle object). They can be read/write but sometimes not both.
  • Methods, which are functions that either act upon the object (could be IncreaseRadius on a circle) or do things that depend on the object and its properties (could be ComputeArea or ComputePerimeter if we keep the Circle object example).

Here, you are using the HTTPReq object and you are trying to apply/call methods on it that are the ones belonging to the MSXML2.ServerXMLHTTP ActiveX object of your first script : it just can't work, and the error message you're getting is exactly telling you that : HTTPReq object does not support the open method.

Note that the examples that were given do not call such a method.

If you want to get a better understanding of the objects (and their properties and methods) available to you, you can get a look at the post I put a link below and more specifically to the link to JScript.

Next thing you'll want to look at is the Opus documentation, and more specifically the Reference/Scripting Reference/Scripting Objects part where you will have a description of all the objects Opus added to the ones provided natively by JScript. These objects will (mostly) be related to Opus.

I'll try and see what you have to change in your code to use the HTTPReq properly based on what @errante provided.

The problem with using AI is that what it produces is usually nonsense. Looks convincing, but is nonsense.

2 Likes

Quite true. As a side note, last week I was trying to get my head around an evaluator column counting files under the displayed folders : it came back inventing evaluator functions. When I told "it" it was improvising and that these functions didn't exist, it answered very politely "you're right", and gave me another solution with another improvised function. That's where I stopped :slight_smile:

2 Likes

I hear ya

illegal ??
Really - in what way ? I am shocked. Just for clarification - i am using my OWN server and my own machine. This is not meant to communicate to other machines.

In any case that for all the helpful insights.

it (AI) not only produces nonsense, but when you correct it several times, it still spews out the same nonsense :upside_down_face:

:slight_smile: This was kind of a joke !
It means you're trying to call methods from one type of object on another type of object : this won't work, and you get an error at execution time.

Lol - gotcha. Will go now to review the help for that particular object