Question

Goal

I would like to use AppleScript to open Automator and add specific actions to a new Workflow, regardless of the localization settings (language). In other words, I would like to use AppleScript to open a new Automator workflow and add specific actions in a specific order (at indexes) using the bundle identifiers of each action (to avoid language issues) or some other way to avoid language issues.

The Problem

The problem is that I cannot get Automator to add anything to the workflow.

  • What is the syntax for the add command?
  • Can I use bundle ids to add actions?

So far, this is what I have:

tell application "Automator"
    make new workflow with properties {name:"Merge PDF Files"} -- Skips Opening Dialog
    add "Get Specified Finder Items" to workflow 1 at index 1 --("com.apple.Automator.SpecifiedFiles")
    add "PDF-Seiten kombinieren" to workflow 1 at index 2 --("com.apple.action.joinpdf")
end tell

Complete Code for a Merge PDF Files Droplet

After bwaldie posted his answer, I would like to share my useful droplet. You can put it on any Mac running OS X (especially useful for people who aren't familiar with Automator). To use it, just paste the code into the AppleScript Editor and save it as an application.

See code for droplet:

on open the_Droppings
    -- CONVERT INPUT LIST OF ALIASES TO POSIX PATHS
    repeat with itemStep from 1 to count of the_Droppings
        set item itemStep of the_Droppings to POSIX path of item itemStep of the_Droppings
    end repeat
    tell application "Automator"
        activate
        set theWorkflowName to "Merge PDF Files"
        set msg to "no"
        tell application "Finder" to if exists POSIX path of ((path to temporary items as string) & theWorkflowName & ".workflow" as string) as POSIX file then set msg to "yes"
        if msg is "no" then
            set myWorkflow to make new workflow with properties {name:theWorkflowName, path:POSIX path of ((path to temporary items as string) & theWorkflowName & ".workflow" as string)}
            add (first Automator action where its id = "com.apple.Automator.SpecifiedFiles") to myWorkflow at index 1
            add (first Automator action where its id = "com.apple.action.joinpdf") to myWorkflow at index 2 --("com.apple.Automator.SpecifiedFiles")
        else
            set myWorkflow to open POSIX path of ((path to temporary items as string) & theWorkflowName & ".workflow" as string)
        end if
        
        set actionsList to name of Automator action of myWorkflow
        set firstAction to item 1 of actionsList
        tell myWorkflow
            set value of setting of Automator action firstAction to the_Droppings -- MUST BE LIST OF POSIX PATHS
        end tell
    end tell
end open
Was it helpful?

Solution

Try something along the lines of...

set theWorkflowName to "Merge PDF Files"
tell application "Automator"
    set myWorkflow to make new workflow with properties {name:theWorkflowName, path:POSIX path of ((path to desktop folder as string) & theWorkflowName & ".workflow" as string)}
    save myWorkflow
    add (first Automator action where its id = "com.apple.Automator.SpecifiedFiles") to myWorkflow
end tell

I think the key is to create the workflow, then save it, and then add the action.

Hope this helps!

-Ben

OTHER TIPS

If you want to use Automator, there is no need for scripting. This works for me:

  1. start Automator;
  2. create new Service or e.g. create new Workflow;
  3. if you created a Service, select "Service receives PDF files in Finder.app" at the top of the window; if you chose Workflow, select Files & FOlders in the list of Actions on the left and drag Get Selected Finder Items to the right and make sure that Appending Pages is selected;
  4. Select PDFs in the Actions list and drag Combine PDF Pages to the right
  5. Select Files and Folders in the Actions list and drag Copy Finder Items to the right and make sure that Desktop is selected
  6. Drag "Make Finder Item Names Sequential" to the right, select Make Sequential" and "Add number to existing item name".

You may want to tweak and experiment with Automator a bit to make sure that it does exactly what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top