Matching payment receipts to invoices with Hazel, AppleScript & Automator

I recently had a request to share some more details about an automation I have used to attach the record of paying a invoice to the original invoice. The workflow looks like this:

  1. The original invoice has already, when received, been saved onto my Desktop and moved into a specific folder by a simple “Move to folder” Hazel rule. (If needed, a task is also added to OmniFocus as part of this rule.)
  2. When payment is made, I also save the payment receipt onto my Desktop and it is matched by a second Hazel rule which runs the Applescript below. This script:
    1. Finds the most recently created file in the specified folder (which is hard-coded into the script), assuming that that is the most recent invoice that is being paid.
    2. Sends the above file, and the file that Hazel has matched (the payment receipt) to the Automator action below.
  3. The Automator action accepts the two PDF files and then:
    1. Sorts them by creation date (from oldest to newest).
    2. Combines the PDFs into one.
    3. Saves the resulting PDF in the same folder that contained the original invoice, with the same name (i.e. replacing the original invoice).
  4. The Hazel script then deletes the original payment receipt from the Desktop.

This is a prime example of an automation that’s been hacked together from different bits and pieces that people cleverer than me have written, so I’m sure there are more efficient ways to accomplish the same result. Nevertheless, I hope it can prove useful to somebody!

Applescript

set sourceFolder to "{{FOLDER PATH}}" as alias

tell application "Finder"
sort (get files of sourceFolder) by creation date
set mergedFile to (last item of result) as alias
end tell

set FileList to {POSIX path of theFile, POSIX path of mergedFile}

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set input to FileList as text
set AppleScript's text item delimiters to astid
set qtinput to quoted form of input

-- the below part of the script was written by Nigel Garvey http://www.macscripter.net/viewtopic.php?id=29258&p=1
set workflowpath to "{{AUTOMATOR WORKFLOW PATH}}"
set qtdworkflowpath to quoted form of (POSIX path of workflowpath)
set command to "/usr/bin/automator -i " & qtinput & space & qtdworkflowpath
set output to do shell script command

Some things to note:

  • {{FOLDER PATH}} and {{AUTOMATOR WORKFLOW PATH}} both need to be replaced by the relevant paths; folders should be separated by colons. The {{FOLDER PATH}} is the folder where the invoices are saved, and the {{AUTOMATOR WORKFLOW PATH}} is the location of the automator action (which should include the file itself).
  • For files stored in iCloud, the path value needs to begin with users:{{USERNAME}}:Library:Mobile Documents:com~apple~CloudDocs: (where {{USERNAME}} is replaced by your username)
  • For files note stored in iCloud, the path values need to begin with users:{{USERNAME}}: (again, where {{USERNAME}} is replaced by your username).

Automator action

The ‘Combine PDFs’ action can be downloaded here:

Leave a Reply