简体   繁体   中英

Simple Apple Automator iterator not working (using AppleScript) - why?

I'm trying to do the simplest thing in the world - a basic iterator in Automator. The workflow goes:

Get Value of a Variable (initially set to 1)

Run Applescript:

on run {input, parameters}
    set input to input + 1
    return input
end run

Set Value of a Variable

Loop

It works the first time, moving from 1 up to 2 as expected. But it fails on the second pass, giving the error

Can't make {} into type number. (-1700)

I'm clueless as to why - I've tried getting it to output from the Applescript as an integer and it makes no difference. Can anyone shed some light?

Your error is because on the second loop of your workflow your applescript is not receiving any input. I would guess that your loop function is not receiving any input and therefore it is not passing anything back into the applescript. Whatever is between your applescript and the loop function must be interfering somehow.

As an alternative, try this as your applescript. Your automator workflow should only have 2 actions, this applescript code and the loop action set to "use current results...".

In this code, on the first loop there won't be any input to the applescript so it will ask you for input, and then on subsequent loops the applescript will receive input from the loop action and thus it will increment your initial input.

Good luck.

on run {input, parameters}
    if input is {} then
        display dialog "Enter a number" default answer "1"
        set input to (text returned of result) as number
    else
        set input to input + 1
    end if
    return input
end run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM