Pass a variable (folder pathway) to AppleScript in Automator

applescriptautomator

I am trying to pass a variable that has been set in Automator to an AppleScript within Automator. My Automator workflow is as follows:

Ask for Finder Items (only allowing folder type)

Set Value of Variable (variable set to "chosenfolder")

Get Value of Variable (chosenfolder)

Run AppleScript

property odd : true -- set to false to get odd numbers

on run {input, parameters}

set thefolder to (input as text)

set TheNumber to (odd as integer)
tell application "Finder"
    set FileList to every file of thefolder
    repeat with oneFile in FileList
        set Ex to name extension of oneFile
        set NameString to text -4 thru -1 of ("000" & TheNumber) & "." & Ex
        set name of oneFile to NameString
        set TheNumber to TheNumber + 2
    end repeat
end tell

return input
end run

This returns the error:

Can’t get every file of "Macintosh SSD:Users:etc....

Does anyone have any suggestions as to why the chosen folder in automator cannot be accepted in the AppleScript?

Note: I understand that I can just use "choose folder" in the AppleScript instead of using the Automator workflow items, but I truncated my actual workflow just to show this portion. The variable is needed elsewhere in the Automator workflow in addition to passing it to the AppleScript.

Best Answer

When using on run {input, parameters}, input is a list, and as such you need to change the following line of your code:

set thefolder to (input as text)

To:

set thefolder to (item 1 of input)

That will fix the Can’t get every file of ... error.

Also, while you didn't state the complete settings of the Get Value of Variable action, I'll assume that under Options, you have checked the [√] Ignore this action's input checkbox to unlink it from the preceding Set Value of Variable action. Otherwise, it passes the variable twice to the Run AppleScript action! That is, in this example workflow you've presented.