2

When I add a Run JavaScript for Automation action to a Shortcut that I'm writing, it comes with a run() function:

function run(input, parameters) {
    // Your script goes here
    return input;
}

When writing AppleScript in Automator, what is the significance of "input" and "parameters"? has a number of responses with links that would be somewhat helpful if I were writing AppleScript, but so far, the best link, The Structure of the on run Command Handler has only this to say about the contents of input:

The input parameter contains (in most instances) the output of the previous action in the workflow; it is almost always in the form of a list.

This is less helpful than I'd hope for from first-party documentation.

So far, I've been able to figure this much out about what I can expect, describing it all using JSDoc types:

/**
 * An item that is passed to an on-run handler.
 * 
 * If you have plain text on the clipboard, this will be that as a string.
 * If you have rich text on the clipboard, this will be a path to an RTF file in, roughly, `/private/var/folders/…/com.apple.WorkflowKit.BackgroundShortcutRunner/Clipboard Written Out Date.rtf`.
 * If you have one file on the clipboard, this will be its UNIX pathname.
 * If you have multiple files on the clipboard, this will be a list-like object that will have all the path names.
 * @typedef {string|object} InputItem
 */

/**
 * A bundle of information passed in as the second (?) item in the array called `input`.
 * @typedef {string} InputInfo
 * @property {string} [source] - the JavaScript source code of this action.
 * @property {string} [temporary items path] - the path to a folder in `/var/folders/…/com.apple.WorkflowKit.BackgroundShortcutRunner//com.apple.shortcuts.RunScript`
 * @property {boolean} ignoresInput - false if this action receives input, true otherwise.
 */


/**
 * @param {[InputItem, InputInfo]} input 
 * @param {*} parameters 
 * @returns {InputItem | string}
 */
function run(input, parameters) {
    // Your script goes here
    return input;
}

I know that I can also return a string from the run function, at least in some cases, because it worked for me elsewhere.

Still, what kinds of object(s) can I expect in the input and parameters arguments?

I'm using the following to help me flesh out the different things that can get passed in:

function run(input, parameters) {
    // Your script goes here
    let ret = '';
    for (const [k, v] of Object.entries(input)) {
        ret += `---\nk: ${k}\nv: ${v}\nt: ${typeof v}\n`;
    
    }
    ret += '...\n'
    
    const second = input[1];
    if (second === undefined) return ret;

    for (const [k, v] of Object.entries(second)) {
        ret += `---\nk: ${k}\nv: ${v}\n`;
    }   
    
    return ret;
}

0

You must log in to answer this question.

Browse other questions tagged .