@evanfuchs
I see what it is you are trying to do. Although the full detail of your workflow are not present in the image, it looks like you have basically taken a workflow for PDF combining that is fairly widely available, based on a workflow presented on the MacAutomator website. No harm there, of course - that’s why the website exists in the first place. However, I don’t think this is the best way to approach your task.
The workflow you have used is based on the idea that ALL of the pdfs to be combined are supplied as input to a single execution of the workflow. However, that is not the way Hazel works. Rather, Hazel works with a single file at a time. Further, Automator’s CombinePDFs element does not allow you to specify a file that was NOT passed in to the workflow; in this case, your base PDF that should be accumulating all of the others. Since you need two files at a minimum (the base PDF and one to append), I do not think this can be done in Automator…except as I will show you below!
I think what you really want to be doing is to establish the base PDF to which all others are added, and then use the Hazel action with a script to add files to it, one at a time. This will be a bit of a kludge, but I think this approach will work.
Assumptions: 1) the PDF that everything is appended to is called “base.pdf” and is located in a known place - which can be, but does not have to be, the same folder that Hazel is scanning for PDFs to add.
2) When you are done, you want the result to be that a new pdf is appended to the end of base.pdf.
Here then is what I would do. Note that this could break over system version updates, but has remained stable for quite some time so far.
-
Have Hazel scan the desired folder for all files of type PDF. You may modify by a) excluding files named base.pdf if that file will reside in the same folder; b) exclude files tagged “added” if you are going to use that scheme as per my earlier post to mark files already added, c) any other criteria you need to select which pdf’s are to be added.
-
The Hazel action for each file is to run a script. I would do this script as a shell script (eg in bash), but you can code in python or whatever if you prefer.
-
This script has the name of base.pdf hard coded into it:
BASEPDF="/Users/me/MyFolder/base.pdf"
You will also want to know the name of the folder the file is in, and create some variables that will be useful:
BASEDIR=${dirname $BASEPDF} # pull out the folder name
OUTPUTFILE="$BASEDIR/output.pdf" # filename for the output
-
Do the append. For this, we are going to use the following python script:
/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py
Note that this is actually the guts of the CombinePDF automatic action which actually does all of the work. You can look at the source code to learn how to use it.
So, remembering that in a bash script, Hazel gives you the filename as “$1”
/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py --output=$OUTPUTFILE $BASEPDF “$1”
-
At this point, the file named in the variable OUTPUTFILE has the append of base.pdf and the new file to be added. You now need to move OUTPUTFILE to BASEPDF. You accomplish this via:
mv $OUTPUTFILE $BASEPDF
-
Since step 5 will overwrite BASEPDF, I would consider first making a copy of BASEPDF before doing the move. You can do this via the cp command, and you could add code to save various versions, etc, because if the above actions fail and you do the move (mv), you will lose BASEPDF. There are a variety of ways to do this; one is to just copy BASEPDF to BASEPDF with the current date/time appended. You will generate a backup each time Hazel adds a file, but you can delete these copies once you are sure base.pdf is intact, via Hazel rule or other approach.
I hope this is helpful. I have obviously made some assumptions, eg that you have some familiarity with shells scripting. If you don’t the code here is pretty easy to write, and I can finish off the script for you if you need that help.
Note that again you can easily trash your base.pdf if you aren’t careful and I strongly recommend that you have the script make a copy with a date/time stamp before you let Hazel run this script so that you don’t lose the base.pdf due to an execution error.