My company allows us to upload our phone bills to their system so we can expense them. They’re fine with us only uploading the single page of the bill where the amount is listed. On my phone bills, this occurs on page 3. I was trying to create a Hazel/AppleScript rule that does the following:
It opens the bill PDF in Preview
Selects Page 3
Exports ONLY that page as a JPG
Nice to have:
Generates an email to my work address with the JPG as an attachment
From what I’ve read, it looks like Preview isn’t very scriptable and the app Skim doesn’t export to JPG. Is something like this possible? Thanks!
I realize you asked about a Mac solution. If, by chance, you have access to the file in iOS, here is an example Shortcut that you could build from. — jay
Honestly, that’s incredible. Thank you. I’m going to keep looking for a way to do it on the Mac if only to boost my own scripting/automation skills, but I’ll definitely be putting your shortcut to work every month. Thank you so much!
As an alternative approach, consider triggering a script from Hazel and using a command line tool to split your PDF into separate pages, then discarding all but the third. If you use your favourite search engine, you’ll find quite the array as it is a common thing to do. A common one is splitpdf.
There are also command line tools you can use to convert a PDF to a JPEG (e.g. sips, imageMagick), but I would suggest sending the PDF as is. I think it is unlikely that it needs to be a JPEG rather than a PDF. You’re sending it by e-mail, so it doesn’t matter as an attachment, and every expense system I’ve come across to date accepts PDF copies for receipts; though there is always a first time to find one that doesn’t.
Once you have the file, you should also be able to try sending that from the same script.
The following command (based on this answer on StackExchange) extracts page 3 from indata.pdf to outdata.pdf. You can modify the page(s) extracted by changing the values for -dFirstPage and -dLastPage, which should be self-explanatory.
Note that outdata.pdf is extracted non-destructively, meaning that it is still vectorized. If your company’s system allows you to upload .pdf files, you are good to go. Otherwise, you can convert the generated file to a rasterized (pixelated) jpg using the following command:
Immediately after posting my previous post, I thought to myself: “this could be turned into an Automator service”. A few minues later and it is complete!
Unfortunately, uploading .zip files to the forum is forbidden, so I cannot share it that way. Instead, I show how it can be recreated:
Open Automator.
Create new document.
Select “Service” from the list of options.
Set the workflow to accept PDFs from all applications.
In the library, search for “run shell script”.
Drag “run shell script” to the right.
Change the shell to /bin/bash from the drop-down menu.
Change “Pass Inputs” to “as arguments”.
Copy and paste the following to the script:
cd ~/Desktop
for f in "$@"
do
/usr/local/bin/gs -dNOPAUSE -dBATCH -dFirstPage=3 -dLastPage=3 -sDEVICE=pdfwrite -sOutputFile=outdata.pdf -f $f
/usr/local/bin/convert -density 300 outdata.pdf -resize 1024 -background white -alpha remove outdata.jpg
rm outdata.pdf
done
Save the file as Export Third Page and exit Automator.
Now, you can right-click on any PDF file and from the menu select Services->Export Third Page. A few seconds later, the file output.jpg will appear on your desktop.
Thanks for the tip! I looked into splitPDF and found that there’s an automator workflow step called Split PDF. Here’s what I did - it’s kludgy, but it works for now:
Like I said - this is clunky and ugly, but it works. And you’re right - I can just upload the new PDF to the expense system instead of doing a bunch of conversion. My HR person mentioned we can upload single images of the one page, so that’s what I had stuck in my head, but a PDF works just as well. Now to get it to automatically email me.
Here’s the code for anyone else who wants to use it, but doesn’t want to retype it from the image like I had to ;))
set theAttachment1 to (POSIX path of theFile)
set subject_ to "Phone Bill"
set the_content to "Here's your phone bill. Have a great day!"
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:subject_, content:the_content & return & return}
tell newMessage
set visible to false
set sender to "senderaddress@url.com" --<<<<----------------(* change this*)
make new to recipient at end of to recipients with properties {address:"recipientaddress@url.com"} --<<<<----------------(*change this email address too *)
make new attachment with properties {file name:theAttachment1} at after the last paragraph
delay 5
(* change save to send to send*)
send --<<<<---------------- change save to send to send or send to save to save to drafts
(* change save to send to send*)
end tell
end tell
Here’s the additional Hazel rule I added, which then moves the PDF to the trash once the email has been sent.