How to make automator only perform an operation on one file at a time

applescriptautomatorfolder-action

I have with 'Folder Action' that automatically runs a Photoshop script on each file that is added to the folder.

The folder action has two steps:

  1. Open this file with Adobe Photoshop CC

  2. Run the following AppleScript:

    on run {input, parameters}
    
        tell application "Adobe Photoshop CC"
            do action "frame for print" from "Zak's actions"
        end tell
    
        return input
    end run
    

This folder action works fine when only one item is added to the folder at a time, but if multiple items are added, the folder action fires on each of the new files almost instantaneously so they all open in Photoshop but the Photoshop action only runs on the most recently opened file. (I'm assuming that is because each time the Photoshop action is started it halts the execution of the previous instance)

I tried adding a pause after the AppleScript hoping that Automator was just thinking that the folder action was done after the AppleScript fired without waiting for it to finish, but even with the pause the folder action was activated for all new files at the same time.

Is there a way to make sure my folder action only runs on one file at a time and waits for the AppleScript to execute before moving onto the next file?

Best Answer

You can do this multilpe ways, one is to use applescript to do a 'for each' loop.

Take this applescript code snippet as an example and work it out from there.

on adding folder items to this_folder after receiving these_items
   repeat with an_item in these_items
       tell application "Adobe Photoshop CC"
           activate
           open an_item
           do action "frame for print" from "Zak's actions"
       end tell
   end repeat
end adding folder items to