Adding a new Internal Command
// Create a new ScriptCommand object and initialize it to add the command to Opus. var cmd = initData.AddCommand(); cmd.name = "SelectNewest"; cmd.method = "OnSelectNewest"; cmd.desc = initData.desc; cmd.label = "Select Newest Files"; cmd.template = "NUMBER/N,FROM/K/N,NODESELECT/S";// First get the starting index to select from, from the FROM argument. Default to 0 (newest) if not supplied. iStartPos = 0; if ( scriptCmdData.func.args.got_arg.from ) iStartPos = scriptCmdData.func.args.from; // Next get the number of files to select, from the NUMBER argument. Default to 1 if not supplied. iNumber = 1; if ( scriptCmdData.func.args.got_arg.number ) iNumber = scriptCmdData.func.args.number; // The func.sourcetab property returns a Tab object representing the function's source tab. // We create an enumerator for files in the source tab (sourcetab.files). // To change this to operate on files and folders rather than just files, use sourcetab.all instead. var objEnum = new Enumerator( scriptCmdData.func.sourcetab.files ); // build an Array of the files so we can sort it. var objFiles = new Array(); i = 0; while (!objEnum.atEnd()) { objFiles[i++] = objEnum.item(); objEnum.moveNext(); } if (objFiles.length > 0) { // Sort the array by the "modify" property. // This will sort the file array in order from newest to oldest. objFiles.sort( function(a, b) { if (a.modify < b.modify) return 1; if (a.modify > b.modify) return -1; return 0; }); // By default we get given a Command object pre-filled for the current tab. // We can use that instead of creating our own, but we need to clear // out its list of files if it has one already. scriptCmdData.func.command.ClearFiles(); // Starting from the specified start position, add the requested number // of files to the command object. Because we have sorted the array from newest // to oldest, this will automatically add the newest files to the command. for ( i = iStartPos; i < objFiles.length && i < iStartPos + iNumber; i++ ) { scriptCmdData.func.command.AddFile( objFiles[i] ); } // Build the Select command line to run that will actually select the files. // The FROMSCRIPT argument is needed to select files from the command object. // The DESELECTNOMATCH argument is used to deselect all other files, unless the // script's NODESELECT argument was used. strCommand = "Select FROMSCRIPT"; if (!scriptCmdData.func.args.got_arg.nodeselect) strCommand += " DESELECTNOMATCH"; // run the Select command via the Command object's RunCommand method. scriptCmdData.func.command.RunCommand( strCommand ); }
最后更新于