Search code examples
applescriptautomator

Automator Quick Action - Run AppleScript on each item in folder


I am trying to build a Quick Action in Automator that I can run on any folder, which would run the following AppleScript on each of the .pdf files inside that folder one by one - waiting for the first to finish, before starting again on the next file.

Currently I have only the AppleScript code that I want to run, but I can't figure out how to attach it to a Quick Action and make it run the code on each pdf item inside.

Note: The number of PDF files in the folder can be different each time.

on run {input, parameters}

    set theFile to input's item 1

    set theSeconds to time of (current date)

    tell application "Finder"
        set theDuplicate to duplicate file "Macintosh HD:Users:username:Documents:IddTest:template.indd"
    end tell

    tell application "Adobe InDesign 2020" to open file (theDuplicate as string)

    tell application "Adobe InDesign 2020"
        if document 1 exists then
            repeat 34 times
                tell document 1
                    relink link "placeholder.pdf" to theFile
                    try
                        update link "placeholder.pdf"
                    end try
                end tell
            end repeat
        end if
    
        close document 1 saving yes
    end tell

    tell application "Finder"
        set name of theDuplicate to "" & theSeconds & ".indd"
    end tell

end run

Solution

  • Note that I do not have Adobe InDesign 2020 so my rework of your AppleScript code has not been tested, however I believe it should work.

    • In Automator create a new workflow as a Quick Action with setting as shown in the image below.

    • Add a Get Folder Contents action.

    • Add a Filter Finder Items action with setting as shown in the image below.

    • Add a Run AppleScript action replacing the default code with the AppleScript code below.

    Example AppleScript code:

    on run {input, parameters}
    
        repeat with thisFile in input
    
            set theSeconds to time of (current date)
    
            tell application "Finder"
                set theDuplicate to duplicate file "Macintosh HD:Users:username:Documents:IddTest:template.indd"
            end tell
    
            tell application "Adobe InDesign 2020"
                open file (theDuplicate as string)
                delay 2
                if document 1 exists then
                    repeat 34 times
                        tell document 1
                            relink link "placeholder.pdf" to thisFile
                            try
                                update link "placeholder.pdf"
                            end try
                        end tell
                    end repeat
                end if
                close document 1 saving yes
            end tell
    
            tell application "Finder"
                set name of theDuplicate to "" & theSeconds & ".indd"
            end tell
        
        end repeat
    
    end run
    


    enter image description here