AppleScript, variables, and dropped filenames in Automator
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.
- Get Specified Finder Items (the disclaimer)
- Combine PDF Pages (appending pages)
- Move Finder Items (to a special folder created for the combined items)
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.
- Save the path as a variable.
- Modify the path into a filename, and save that as a variable.
- 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
-
tell application "Finder"
- 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.
You’ll need to drag the variables from the variable list into the text box.
Most likely it will also work with the Run Shell Script action.
↑
- Append multiple PDF files
- “I have about 600 PDF files that I want to add a single disclaimer page to the beginning of each of them. So, I need to find a way to merge two PDF documents where one file is always the same and and comes first and the second file is changing.”
- Combining multiple PDF files into a single file
- Automator allows you to combine multiple PDF files into a single file.
More AppleScript
- Find all parent mailboxes in macOS Mail
- The macOS Mail app seems to want to hide the existence of mailboxes and any sense of hierarchical storage. These two AppleScripts will help you find the full path to a selected message and open the message’s mailbox.
- JXA and AppleScript compared via HyperCard
- How does JXA compare to the AppleScript solution for screenshotting every card in HyperCardPreview?
- Using version control with AppleScripts
- AppleScripts aren’t stored as text, which makes it impossible to track changes in AppleScript files using version control software such as Mercurial or Git.
- Save clipboard text to the current folder
- Use the Finder toolbar to save text on the current clipboard directly to a file in the folder that Finder window is displaying.
- Adding parenthetical asides to photograph titles on macOS
- Use Applescript to append a parenthetical to the titles of all selected photographs in Photos on macOS.
- 17 more pages with the topic AppleScript, and other related pages
More Automator
- Another Safari 14 Services workaround
- Another workaround to Safari 14’s Services bug is to copy the results to the clipboard.
- Renumber selected lines of text
- This very short script renumbers lines of text and normalizes on a single tab/space combination in front of each number.
- Automator problems with custom Python
- At least some Automator actions use Python. If you’ve updated Python on your system, these actions might not be able to import core functionality from OS X.
- Combining multiple PDF files into a single file
- Automator allows you to combine multiple PDF files into a single file.