Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, Swift, BASIC, and whatever else I happen to feel like hacking at.

AppleScript, variables, and dropped filenames in Automator

Jerry Stratton, October 6, 2014

Prepend Disclaimer workflow: Prepend a PDF file to any PDF dropped onto this Automator workflow application.; PDF; Automator; Services, Quick Actions

Simple workflow to prepend a known PDF to an arbitrary dropped PDF.

Over on Stack Overflow the other day, someone asked an intriguing question about wanting to prepend a disclaimer page to hundreds of PDF files. Now, merging multiple PDFs into a single PDF is the kind of task Automator excels at, and it’s very easy to do. Appending a single page to multiple PDF files, however, is a classic loop. This is not something Automator excels at. It requires thinking outside the workflow.

The basic workflow is an application—application workflows take dropped items and do things with them.

  1. Get Specified Finder Items (the disclaimer)
  2. Combine PDF Pages (appending pages)
  3. Move Finder Items (to a special folder created for the combined items)
Disclaimer Loop workflow: Take dropped files one at a time and run a workflow as if those files had been dropped on the second workflow.; Automator; Services, Quick Actions; loops

Automator can loop through dropped items one at a time using Run Workflow.

However, this only works for one file at a time. How to prepend the disclaimer to hundreds of files? Automator has a loop workflow action, but all it does is go back to the beginning of the workflow. It doesn’t do anything to incrementally loop through the list of dropped files. For that, we need the Run Workflow action. This runs a different workflow, and can send its own dropped items one at a time to that second workflow.

But, there’s a second problem: the Combine PDF Pages action creates files with a random filename. There is no option to tell it to take one of the dropped files and use that as its new filename.

Automator has some very impressive variable options: there are variables that are really AppleScripts, variables that are really shell scripts, variables containing many system names and common system paths. There is no variable that contains the dropped filename, however, and the AppleScript/Shell Script variables don’t take any arguments. As far as I can tell, the variables are all, basically, static, even if in a dynamic way. There is, thus, no way to modify the dropped file’s path using either an AppleScript variable or a Shell Script variable in order to get the dropped file’s base name.

Modifying the dropped file’s path is possible with the Run AppleScript action.1 It’s trivial enough to put a Run AppleScript at the top of the workflow and save the result to a variable for use in a Rename Finder Items action at the end of the workflow. However, in Automator workflows every action feeds into the next action: modifying the file path into a file name means that the Combine PDF Pages action will get the file’s base name rather than the file’s path, and it won’t be able to find the file.

In order to get this to work, we need to save the path as a variable, too, then restore it later.

  1. Save the path as a variable.
  2. Modify the path into a filename, and save that as a variable.
  3. Restore the full path into the workflow.

The AppleScript to get the full path is simple enough, since all it has to do is echo its input:

[toggle code]

  • on run {input, parameters}
    • return input
  • end run

The AppleScript to get just the filename calls the Finder to get it:

[toggle code]

  • on run {input, parameters}
    • tell application "Finder"
      • set filename to name of file input
    • end tell
  • end run

After each Run AppleScript use a Set Value of Variable action to save the results, first in filepath and then in filename. Then, use a Get Value of Variable to get filepath and put it back into the workflow.

Finally, use Rename Finder Items to change the Full Name of the resulting file to the filename variable.

Prepend with Rename workflow: A more complex workflow to prepend a PDF to the dropped file, and then name the new file with the same name as the old.; Automator; Services, Quick Actions; variables; filenames

Use variables to sort of save state in the workflow, then restore it later.

You’ll need to drag the variables from the variable list into the text box.

  1. Most likely it will also work with the Run Shell Script action.

  1. <- Django bookmarklets
  2. icalBuddy eventsFrom ->