Automator tricks: setting automator variables at workflow launch – part 2

Let’s now see another way to get multiple values as input, and save them as automator workflow variables. The code is self-explaining

on run {input, parameters}
    #convert the input in a string
    set inputText to input as string

    #define text separator
    set text item delimiters to "+"

    #create a list containing the text items
    set textItemList to text items of inputText

    #count the number of items in the list
    set n to count of every item of textItemList

    #show all items in the text list
    set i to 0
    repeat with p in textItemList
        set i to i + 1
        #display dialog "i: " & i & " value:" & p
    end repeat

    #put the values of the input parameters into three workflow variables
    set value of variable "A" of front workflow  to item 1 of textItemList
    set value of variable "B" of front workflow to item 2 of textItemList
    set value of variable "C" of front workflow to item 3 of textItemList

end run

Now, if we run the script by passing in input

alpha+beta+delta

it would associate

  • alpha with the workflow variable A
  • beta with the workflow variable B
  • delta with the workflow variable C

Of course the separator char can be changed.

There is a catch though… This script works when run from within the Automator app, but NOT if you run it from a command line with the automator command! The reason is that “front workflow” is not defined when running from a shell. Hence, it cannot be used e.g. on a server… And, I did not yet find a solution to this problem – sigh.

BTW, in the code above we have been copying applescript  variables into workflow variables. If we wanted to do the opposite, i.e. to read a workflow variable and store its value in an applescript variable, we would have been using:

   
    #read and display a workflow variable
    set firstVar to get value of variable "A" of front workflow
    display dialog "A: " & firstVar
Advertisements
Tagged with:
Posted in Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: