I'm new to AppleScript. I'm trying to create a script which takes the list items from a previous Automator workflow action and convert them into variables.
There will be an undetermined number of list items (well, between 1 and 40 each run).
This works for a determined number of list items, let's say three.
on run {input, parameters}
set value of variable "F1" of front workflow to item 1 of input
set value of variable "F2" of front workflow to item 2 of input
set value of variable "F3" of front workflow to item 3 of input
return input
end run
But if there are only two list items generated from the previous workflow action, we run into problems.
So, I updated the script using if
to try to get around this issue.
on run {input, parameters}
if item 1 exists then
set value of variable "F1" of front workflow to item 1 of input
end if
if item 2 exists then
set value of variable "F2" of front workflow to item 2 of input
end if
if item 3 exists then
set value of variable "F3" of front workflow to item 3 of input
end if
return input
end run
When I run this, only the first variable F1
populates. The others are left blank/do not populate. I'm not sure how to move forward with this?
Can anyone tell me if I'm on the right track, or if there's a better way to do this?
Best Answer
Variable declaration must be made before run time. The following example AppleScript code, run in Script Editor, is to demonstrate a way of doing it:
The log result is:
Obviously the
log
command is being used to show that the variables were assigned a new value, except the last one because theinput list
only had three items in it. You'll need to built this out for however many variables could be needed, as you can't add them after execution of the code has started. You mentioned "between 1 and 40 each run" in your OP.You'd remove the
log
commands for the real code within each of theif
statement blocks, besides the variable assignments, as appropriate at that point in the execution of the code, if any.Note that by wrapping the code of the group of
if
statement blocks, that sets the new value of the variables based on the value of thelist item
, in atry
statement when there are lessitems
in theinput list
, it doesn't throw an error that would halt execution of the code, and continues with the remaining code.The value of the variable is initially being assigned
missing value
, however""
(double-quotes) can be used instead as well. I like to usemissing value
at times as it can be easier to read the code.NOTE: The example AppleScript code above it just that, and is coded as is to demonstrate a way of assigning the variables a value from a list. Which is what
input
is in the context of e.g.on run {input, parameters}
of a Run AppleScript action in an Automator workflow. Depending on what the source of theinput
is and what will be done with it, you may need to add e.g.as string
oras text
or other coercion as appropriate as/if applicable, in this case afteritem n of input
, e.g.set F1 to item 1 of input as string
.