Scripts

Also see Function

Scripts allow you to define Javascript functions that can be re-used as user-created Nodes in a Flow.

A script can be shared with the community to implement common or missing features not found in a plugin.

Each script will be added as a Flow Node in the Flow Editor.

Types of Scripts

  1. Flow Scripts These are scripts that will be available as additional nodes in a Flow. These must follow a strict pattern described below.
  2. System Scripts These scripts are special scripts that can be used by the system for Tasks and events. These do not follow a strict pattern, and are treaty as “truthy”, if they return a “falsey” value (false, 0, null etc) when use they stop an action. For example if a script returns 0 and is used in a “Pre Execute Script” for a Processing Node that will prevent that node from processing any more files.
  3. Shared Scripts These scripts are libraries that can be used by any script and can provide helper methods to simplify other scripts. For example, there is a FileFlowsAPI shared script which provides access to some FileFlows API methods.

Creating a Script

Each script must have the following a starting comment block describing the script and a Script function that will be the entry point of the script.

Script Comment Block

/**
 * Requires a 7Zip tool to be configured and will zip files
 * @author John Andrews
 * @version 0.6.1.900
 * @param {string} ArchiveFile The name of the zip file to create, if empty a random name will be used
 * @param {string} Pattern The filename pattern to use, eg *.txt
 * @param {bool} SetWorkingFileToZip If the working file in the flow should be set to the newly created zip file
 * @output Zip file created
 */
  • The text before the first @ is the description of the Script
  • @author [optional] and it is the person who created the script
  • @version [optional] the version of the script
  • @param {type} [name] [description]
    • Type: tells the UI what type of input this is, available options are “string”, “bool”, “int”
    • Name: The variable name passed in to the function, no spaces are allowed
    • Description: everything after name
  • @output these the possible output connections this function has. The order is import, the first @ouput will be “Output 1”, 2nd will be “Output 2” etc.

Entry Point

  • The script must have a “Script” function as the entry point
  • The script will have the variables defined in the comments passed into the script when called
    function Script(ArchiveFile, Pattern, SetWorkingFileToZip)
    

Complete Example

/**
 * Requires a 7Zip tool to be configured and will zip files
 * @author John Andrews
 * @version 0.6.1.900
 * @param {string} ArchiveFile The name of hte zip file to create, if empty a random name will be used
 * @param {string} Pattern The filename pattern to use, eg *.txt
 * @param {bool} SetWorkingFileToZip If the working file in the flow should be set to the newly created zip file
 * @output Zip file created
 */
function Script(ArchiveFile, Pattern, SetWorkingFileToZip)
{
    let output = '' + ArchiveFile; // ensures ArchiveFile is a string
    if(!output || output.trim().length == 0)
        output = Flow.TempPath + '/' + Flow.NewGuid() + '.zip';
    Logger.ILog('Output: ' + output);
    let sevenZip = Flow.GetToolPath('7zip');

    let process = Flow.Execute({
        command: sevenZip,
	argumentList: [
	    'a',
	    output,
	    Pattern
    	]
    });

    if(process.exitCode !== 0){
        Logger.ELog('Failed to zip: ' + process.exitCode);
	return -1;
    }

    if(SetWorkingFileToZip)
	Flow.SetWorkingFile(output);
    return 1;
}
© 2022 FileFlows