1

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?

1

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:

set input to {"one", "two", "three"}

set F1 to missing value
set F2 to missing value
set F3 to missing value
set F4 to missing value

try
    if item 1 of input is not missing value then
        set F1 to item 1 of input
        log "F1 is: " & F1
    end if
    if item 2 of input is not missing value then
        set F2 to item 2 of input
        log "F2 is: " & F2
    end if
    if item 3 of input is not missing value then
        set F3 to item 3 of input
        log "F3 is: " & F3
    end if
    if item 4 of input is not missing value then
        set F4 to item 4 of input
        log "F4 is: " & F4
    end if
end try

log "F4 is still: " & F4

The log result is:

(*F1 is: one*)
(*F2 is: two*)
(*F3 is: three*)
(*F4 is still: missing value*)

Obviously the log command is being used to show that the variables were assigned a new value, except the last one because the input 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 the if 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 the list item, in a try statement when there are less items in the input 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 use missing 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 the input is and what will be done with it, you may need to add e.g. as string or as text or other coercion as appropriate as/if applicable, in this case after item n of input, e.g. set F1 to item 1 of input as string.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .