OS X Server is an inexpensive app, available from the Mac App Store for about twenty dollars, which puts some GUI lipstick on conventional UNIX server functionality. This makes the functionality a little bit easier to use and manage (but, warning, only a little bit). Out of the box, Server provides a very secure L2TP VPN option, which has proven to be useful as a complement to the slightly less secure PPTP VPN running on my Raspberry Pi. I have CloudPull running continuously on this machine, backing up my gmail account to the machine's hard disk, and this Mac is also linked to my Dropbox account, so it continuously duplicates my critical work-in-progress from the various machines I use. From there Time Machine backs everything up to two alternating disks attached to my home's Time Capsule router. It's all part of my multi-layered, duplicative backup strategy.
Best of all, the machine's desktop is remotely and securely accessible via Screen Sharing. I moved all my email archives to this machine, and now I can search for old emails easily when traveling by starting Screen Sharing, using Spotlight to search my email stash for keywords, and then paging through the results in Finder using Cover Flow and Quick Look-- a delightful reason to use mail.app in the first place. It's simply wonderful for an email pack-rat like me.
Meanwhile--quite aside from the topic of this nifty little server--my HP OfficeJet multifunction printer has e-print capability that lets print-jobs be sent via a special email address assigned to the printer. So for two years I've been trying to configure things so that my travel receipts are automatically printed and waiting for me when I return from business trips. My typical travel partners are Hertz, Marriott, United Airlines, Southwest Airlines, Uber and the occasional Square-powered taxi. All of these companies send me receipts via email. I use a Gmail account for receiving these and also because the marvelous TripIt.com service can scour a Gmail account for travel reservations and automatically put schedule details into my Mac/iPhone calendar. So cool.
One would think that Gmail's filtering capability would allow email receipts from these companies to be automatically forwarded to the HP printer's e-print address. A Gmail filter like this should do the job:
Matches: ("southwest airlines confirmation" OR "eticket itinerary" OR "Hertz E-Mail Statement of Charges" OR "thanks for staying" OR (squareup AND "receipt from") OR "Uber ride receipt")
Do this: Forward to xxxxxx@hpeprint.com
Do this: Forward to xxxxxx@hpeprint.com
...where xxxxx@hpeprint.com is my printer's email address.
Well. Not so easy. First, the hpeprint.com service rejects automatic forwards from Gmail with a cryptic error message:
Technical details of permanent failure:
Google tried to deliver your message, but it was rejected by the server for the recipient domain hpeprint.com byonramp02.hpeprint.com. [15.201.184.202].
The error that the other server returned was:
550 5.7.1 Command rejected
Google tried to deliver your message, but it was rejected by the server for the recipient domain hpeprint.com byonramp02.hpeprint.com. [15.201.184.202].
The error that the other server returned was:
550 5.7.1 Command rejected
So while manually-forwarded emails usually worked, filter-based automatic forwards wouldn't. Bad enough, but then HP recently morphed hpeprint.com into "hpconnected.com," necessitating a new email address for the printer. And, after transitioning my account to the new service, the service quit working altogether! Some quick googling shows that this service is now unreliable at best and totally inoperative for legions of users.
Nothing to count on, then. But why not implement something similar on my doughty old Macbook Pro?
Here's how to implement e-print functionality on OS X
This should work with any printer. My objective is to have my travel receipt emails and their attachments automatically printed and filed as .pdfs, but this same technique can be used to automatically print-and-file emails of any sort. (You don't have to be running your Mac as a server.)My approach borrows from a host of web posts from folks doing similar things, but many previous examples are now complicated by the new application sandboxing security features introduced in OS X 10.8 Mountain Lion. The following works with the new ways of doing things and includes a cute twist or two.
The two tools used are AppleScript and Automator, both of which are built-into every Mac. AppleScript integrates easily into mail.app's rules framework, so incoming emails that meet certain criteria can be processed automatically. AppleScript scripts can be programmed from raw commands, or the AppleScript engine can mimic user interactions with application GUIs. Meanwhile, Automator makes automatic printing and filing super-easy. The combination is very powerful and convenient, and it's all good geeky fun. (And you can still use any e-print service your printer might support too-- there's nothing mutually-exclusive about this.)
- First, out of fondness for the TripIt.com service, I receive my emailed receipts to my gmail account.
- I set up a new free account on the superb GMX.com webmail service for exclusive use for receipt-printing purposes. There are other ways of doing this, but this works brilliantly. I've had my eye on GMX.com for a while now and have had a test account there for some months. It works well in a browser and integrates beautifully with the Mac's mail.app email client. GMX.com seems a more private and standards-compliant alternative to other webmail vendors, and its spam filtering is effective (perhaps a bit aggressive-- I had to white-list my Gmail and other addresses in it). GMX.com provides you with a free IMAP account with unlimited email storage; setting up a new account takes perhaps five minutes and requires minimal personal information and no credit-card. Besides being great for general email purposes, GMX.com is terrific for setting up a specific "print to me" email account. Good stuff.
- Once the print-to-me email account was set up on GMX.com and active in mail.app, I set up the Gmail filter to forward incoming receipts to the new print-to-me email. This just meant replacing the xxxxx@hpeprint.com address in the filter rule above with the new GMX.com address.
- In mail.app, go to the app's Preferences, then the Rules pane. Add a rule. In the Description field, name it something like "Receipt Print". In the conditions block, one way of doing thing is to select "If [any] of the following conditions are met: [Account] is [gmx account]". Then, in the Perform the Following Actions field, select Run Applescript, and select Open in Finder. This will open an AppleScript editor.
- Paste the following into the AppleScript editor:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using terms from application "Mail" | |
on perform mail action with messages theselectedMessages | |
-- get the home folder | |
set sandboxedhomeFolder to (path to home folder) | |
-- strip out the sandbox filigrees in the folder name | |
set homeFolder to (do shell script "echo " & sandboxedhomeFolder & " | sed 's/Librar.*//'") | |
-- add on the desired attachments folder (must be somewhere Mail can write to) | |
set attachmentsFolder to homeFolder & "Downloads:autoprint" | |
tell application "Mail" | |
activate | |
repeat with theMessage in theselectedMessages | |
-- get the sender's name | |
set senderName to extract name from theMessage's sender | |
set messageID to (id of theMessage as string) | |
set messageSubject to subject of theMessage | |
repeat with theAttachment in theMessage's mail attachments | |
set originalName to name of theAttachment | |
-- name the file with the sender's name as well as the attachment name | |
set savePath to attachmentsFolder & ":" & messageSubject & "-" & messageID & "-" & originalName | |
try | |
-- Save the attachment | |
save theAttachment in savePath | |
end try | |
end repeat | |
set saveName to messageSubject & "-" & messageID & "-msg.pdf" | |
tell application "System Events" to tell process "Mail" | |
keystroke "p" using command down | |
repeat until exists sheet 1 of window 1 | |
delay 0.9 | |
end repeat | |
tell menu button "PDF" of sheet 1 of front window | |
click | |
delay 1.5 | |
click menu item "Save as PDF…" of menu "PDF" | |
end tell | |
repeat until exists sheet 1 of sheet 1 of front window | |
delay 1.5 | |
end repeat | |
tell sheet 1 of sheet 1 of front window | |
keystroke "g" using {command down, shift down} | |
repeat until exists sheet 1 | |
delay 1.5 | |
end repeat | |
set value of text field 1 of sheet 1 to "/Users/scott/Downloads/autoprint/" | |
click button "Go" of sheet 1 | |
set value of text field 1 to saveName | |
click button "Save" | |
end tell | |
end tell | |
set notifySubject to "PRINTED: " & messageSubject | |
set notifyContent to "The email & attachments regarding " & messageSubject & " has been printed at JordanManor" | |
set notifyRecipient to "your name" | |
set notifyAddress to "your_notification_email_address@account.com" | |
-- Note: best to use a different account for notifications to avoid endless loops should | |
-- the notification content trigger your email filters! | |
tell application "Mail" | |
set notifyMessage to make new outgoing message with properties {subject:notifySubject, content:notifyContent, visible:true} | |
tell notifyMessage | |
make new to recipient with properties {name:notifyRecipient, address:notifyAddress} | |
send | |
end tell | |
end tell | |
end repeat | |
end tell | |
end perform mail action with messages | |
end using terms from |
- Note I created a folder called "autoprint" inside my Downloads folder. The application sandboxing introduced with Mountain Lion limits where apps can write to your disk; Downloads is one of the few places mail.app is now allowed to write without user intervention. So, create that folder in your Downloads folder. Inside of that, make another folder called "printed".
- Edit the hard-coded folder names in my example script (especially the one in quotes towards the end) to match your user name and so forth, minding case. Then click the compile button at the top of the AppleScript editor, and check for any errors. When satisfied, save it. Check that your mail.app rule points to the AppleScript you just made. (Note that initiating the script's construction from within mail.app ensures that the script is stored within mail.app's own script folder, ~/Library/Application Scripts/com.apple.mail ...another recent security improvement.)
- If you're running OS X 10.8 Mountain Lion: In System Preferences, click the Accessibility icon and ensure that "Enable access for assistive devices" is checkmarked at the bottom. This allows the GUI scripting in the script to operate. See this helpful post for how to do the same thing in OS X 10.9 Mavericks, which grants assistive permissions with per-app granularity for security reasons.
- Now, load Automator. Choose "Folder Action". In the Choose Folder pull-down, select Other, then navigate to select your autoprint folder. Drag the Print Finder Items action from the column at the left into the workflow. Then drag the Move Finder Items action under that, and specify that printed items should be moved to the "printed" subfolder you previously placed in your autoprint folder. Save this new Folder Action you've made.
You're done. Incoming emails will be split from their attachments. Any attachments will be given unique names based on the numeric mail ID and original subject, then they will be placed in the autoprint folder. Meanwhile the body of the email will be converted to .pdf (complete with clickable embedded links!) and also placed in the autoprint folder. Printing will occur automatically, after which the files will be moved to the printed folder. A print-success email notification is then sent back to you. The original emails will remain untouched in mail.app.
So handy for compiling expense reports after a long trip! You can even snap a photo of paper receipts with your phone as you travel and email them to your print-to-me address. It's a great way to ensure receipts aren't lost or forgotten.
The script is easy to modify if you have different needs, too. Enjoy.
0 comments:
Post a Comment