S. A.
S. A.

Reputation: 111

Pass automator variable to javascript

I'd like to use the selected text in my automator workflow. If I use "Run bash action" I have the option "pass input as arguments". But in the case of "Run JavaScript" action - I don't.

So, what should I do to pass clipboard data (text) to my JS function sum_letters?

My Automator workflow

Upvotes: 0

Views: 3411

Answers (1)

CJK
CJK

Reputation: 6112

Run Javascript in Automator requires the declaration of a run() function, which is called upon initialisation. It's where your main code implementations are placed. It is defined like so:

    function run(input, parameters) {

        // Your script goes here

        return input
    }

It has two arguments. The one of interest to you is the input argument, which will contain any data passed through from the previous action in the workflow, stored as an array.

In your workflow, the contents of the clipboard is carried through from the Copy to Clipboard action, and passed into the input variable, which will contain a single element, input[0], the value of which will be the clipboard's contents.

You can then pass this as an argument to your function sum_letters().

Here's what it would look like:

Run Javascript:

    function run(input, parameters) {
        var clipboardText = input[0]
        sum_letters(clipboardText)

        // Other lines of code

        return input[0]
    }

    function num_letters(k,d) {
        var i = '', e = [

        // ...etc...
    }

    function sum_letters(t) {

        // Lines of code
    }

and so on. So run() is called immediately when the workflow reaches the Run Javascript action. Other functions, such as num_letters() and sum_letters() only get called if done so explicitly from either inside run(), or from inside another function that is called from run().

run() terminates when it reaches a return statement. This passes a value of your choosing back to the Automator workflow and onto the next action.

If you need any clarification or have further questions, let me know.

Upvotes: 1

Related Questions