Error in applescript “Invalid index”

applescriptautomatorerror

I have an automator workflow which works fine except for a part of it – applescript.

This is how the workflow goes –

Run applescript (this part works fine) –

on run {input, parameters}

    tell application "Mail" to set theMessageText to content of (get first message of inbox)
    set x to text ((offset of "9" in theMessageText) + 2) thru ((offset of " " in theMessageText) - 1) of theMessageText
end run

Set value for variable (The name of the variable is 'number')

Get value for variable (I have made sure that this receives no direct input from previous actions. I am calling the variable 'number' here and I have duplicated this process 2 more times to call 2 more variables)

Run Applescript (This is the code I have problem with) –

on run {input, parameters}
    set cat to first item of input
    set x to second item of input
    set o to third item of input
    tell application "Contacts"
        set thePerson to make new person with properties {first name:"Testing", last name:"Auto"}
        make new phone at end of phones of thePerson with properties {label:"Main", value:x}
        repeat with z from 1 to o
            set a to item z of cat
            add thePerson to group a
        end repeat
        save
    end tell
end run

The error shown here is "Syntax Error – Can’t get item 2. Invalid index".

The point of the workflow is to get some contact details from an email and add them as a contact in address book (This isn't the full workflow as it is a fairly big one. I have shown only the relevant actions).

I would be grateful for any help.

Best Answer

The values you set before are not given via the inputparameter. As long as you run your Automator Workflow as an Automator Workflow and not as an Application or Service, you can access your formerly set variables using the following:

tell application "Automator"
    set cat to value of variable "NameOfFirstVariable" of front workflow
    set x to value of variable "NameOfSecondVariable" of front workflow
    set o to value of variable "NameOfThirdVariable" of front workflow
end tell

inside your "Run Applescript" action.

Greetings, Michael / Hamburg