Function/commands parameters in add-ins script

I am still going a little nutty trying to understand OBJECTS within DOPUS, especially the clickData, cmdData, and the newest one I found in my research, the scriptCmdData.

So first, I am generally a little confused about how to best setup my addin script. I had it working pretty well, but then I keep having issues with the cmd.addFiles() (throwing automation error).

So first basic setup question

This is the init section of my add-in:

function OnAddCommands(addCmdData) {
    var cmd1 = addCmdData.AddCommand();
    cmd1.name = "Z_collectNoCPFiles";
    cmd1.method = "Z_collectNoCPFiles";
    cmd1.desc = "Z_collectNoCPFiles";
    cmd1.label = "Z_collectNoCPFiles";

    var cmd2 = addCmdData.AddCommand();
    cmd2.name = "Z_normalizeSeriesSuffix";
    cmd2.method = "Z_normalizeSeriesSuffix";
    cmd2.desc = "normalize random Series Suffix";
    cmd2.label = "Z_normalizeSeriesSuffix";

    var cmd3 = addCmdData.AddCommand();
    cmd3.name = "Z_collectSeriesFiles";
    cmd3.method = "Z_collectSeriesFiles";
    cmd3.desc = "Z_collectSeriesFiles";
    cmd3.label = "Z_collectSeriesFiles";
}

***I know this can be done with FIND and FILTERS in DOPUS, but I do lots of other processing to each file.

Here is one of the basic function/command definitions:

function Z_collectNoCPFiles(scriptCmdData) {
    var cmd = scriptCmdData.func.command;
    var srcColl = "MYCOLL";
    var destColl = "noCP";
    var srcEnum = DOpus.FSUtil.ReadDir('coll://' + srcColl, true);
    var chunkSizeMax = 5;

    while (!srcEnum.complete) {
        var collItem = srcEnum.Next();
        if (collItem.is_dir) continue;
        var filesToCopy = [];

        var allConditions = !collItem.is_dir &&  validCount < addToCollLimit

        if (allConditions) {
            filesToCopy.push(collItem.realpath);
            if (filesToCopy.length <= chunkSizeMax) {
                cmd.AddFiles(filesToCopy);
            } else {
                cmd.RunCommand('Copy TO coll://' + destColl + ' WHENEXISTS=skip');
                DOpus.Output(filesToCopy.join('\n'));
                filesToCopy = [];
            }
        }
    }

QUESTIONS :upside_down_face: :thinking:

  1. Is the OnAddCommands(scriptCmdData) section correct way to add commands so they appear in the BUTTON SCRIPT commands list?

  2. Is scriptCmdData the correct param to use i.e. OnAddCommands(scriptCmdData)? Furthermore, is an obj paramater that is inherited from ___???

  3. Is var cmd = scriptCmdData.func.command; the proper way to add the command that is later used for cmd.addFIles() (trick question - I know it is NOT because I keep getting error)

  4. I think I have more questions - but cant think of them right now.

Thanks all for help :nerd_face:

Yes.
Yes.
Yes.
Yes.

For 3. : you can also create a fresh Command object (the one in scriptCmdData already contains selected files I think) : DOpus.Create.Command().
And you should either ClearFiles() on the cmd object after each RunCommand() or create a new one (running the command leaves files previously added).

What does the error message say?

Error message says: class not support automation, referring to the add files() line

That seems strange. We'd need to see the whole script, I think.

Note that you add the array everytime in the loop.
You'd better add one file at a time.

I will update my post.

On a related note, since the collection is very large, can the script still handle adding one file at a time or ( as in my current example) should I add 5 or 10 at a time - in chunks?

When I tried to use find and filters manually, dopus froze up (most likely due to huge size).

Any other workarounds anyone can suggest that could handle large collections?

I can't really say. From what I remember, adding lots of files to collections does take time.
I would still do it by chunks (I would go by 50) : add them one at a time to the Command object, and run the command by chunks.
Here, your script builds arrays of 5 items, but adds the full array on each iteration (condition filesToCopy.length <= chunkSizeMax should be filesToCopy.length == chunkSizeMax).
EDIT : or replace cmd.AddFiles(filesToCopy) by cmd.AddFile(collItem.realpath)
EDIT2: Not sure adding arrays work. Manual only mentions Opus Collections (Vectors, Sets, ...)

Hmmm maybe that's why getting errors. Will tinker a bit and see how goes. Thank you so much for your help.