AppleScript is a great tool for automating the apps you use daily on your Mac—if you can figure out the proper code. We’re here to help: We’ve asked scripting and Automator expert Ben Waldie to solve some reader problems using AppleScript. The idea is not just to provide specific solutions for specific problems, but also to demonstrate some general principles and provide sample scripts that, with a little tinkering, you can adapt to solve a bunch of other problems, too. First up: A few questions about Mail. Fire up AppleScript Editor (/Applications/Utilities) and follow along.
Q: I have tons of mailbox folders in Mail, some of them nested several levels deep. Over the years I’ve filed thousands of messages in them, some of which I never marked as read. Is there a way to go through all those folders and mark all of the messages as read?
Absolutely. You want to tell Mail to set the read status attribute of every unread message in your Inbox, local mailboxes, and mailboxes on your mail server to false. Be prepared, though: Depending on the number of messages and mailboxes you have, this procedure may take a while to run. And since individual AppleScript commands time out if they don’t finish within 2 minutes, you might also get an error. That’s why it’s a good idea to enclose your script in a timeout wrapper to prevent such errors. The script below allows each action up to 5 minutes to finish; feel free to increase the timeout period if necessary.
--Allow up to 5 minuteswith timeout of 300 seconds tell application "Mail" -- Process all unread messages in the inbox set read status of every message of inbox where its read status = false to true -- Process all unread messages on your local Mac set read status of every message of every mailbox where its read status = false to true -- Process all unread messages on your mail server set read status of every message of every mailbox of every account where its read status = false to true end tellend timeout
Q: For archiving reasons at work, I’ve been asked to print thousands of Mail email messages to PDF format. How can I automate that?
Unfortunately, Mail doesn’t have built-in AppleScript support for saving a message as a PDF, so the solution isn’t as simple as it might seem. But it’s definitely possible. The trick is to use some UI scripting to work around the limitation. First, go to the Accessibility system preference, and make sure the Enable access for assistive devices checkbox is selected; UI scripting doesn’t work unless this setting is turned on.
This script saves the frontmost opened message as a PDF. To process multiple messages, you can expand the script by wrapping it in a repeat loop and adding some error protection. ( Here’s a script that saves all inbox messages as PDFs.)
tell application "System Events" tell process "Mail" -- Bring Mail to the front set frontmost to true -- Select the Print menu item click (first menu item of menu "File" of menu bar 1 whose name begins with "Print") tell window 1 -- Wait until the print sheet appears repeat until sheet 1 exists end repeat tell sheet 1 -- Click the PDF button click menu button "PDF" -- Select the Save as PDF menu item click (first menu item of menu 1 of menu button "PDF" whose name begins with "Save as PDF") -- Wait until the save as sheet appears repeat until sheet 1 exists end repeat tell sheet 1 -- Navigate to the Desktop keystroke "D" using command down -- Enter a name for the PDF keystroke "My PDF.pdf" -- Click the Save button click button "Save" end tell end tell end tell end tellend tell
Keep in mind that this script is tied to Mail’s interface, and that it expects certain menus, buttons, and windows to exist. If Apple moves things around in a future version of OS X, you might need to make some adjustments to the script.
Q: When I’m at the office, I connect my MacBook Air to an external monitor, which I then use as my primary screen; the Air’s screen is mainly for my Calendar and Mail. I’d love to have a script that I can run when I arrive at work that would automatically move these application windows to the Air’s screen and resize them. Can it be done?
Certainly. When you’re in the office, move your Calendar and Mail windows onto the Air’s screen and position them where you want them. Now you’re ready to capture their bounds (position and size). Run each of the following scripts in AppleScript Editor. They’ll return the bounds in the lower Result pane; make a note of the results.
-- To get the Calendar window's position and sizetell application "Calendar" return bounds of front windowend tell
-- To get the Mail window's position and sizetell application "Mail" return bounds of front message viewerend tell
Let’s say the first script returns the values {-1426, 102, -491, 930}
, while the second returns --> {-955, 100, -7, 845}
. Plug those numbers into the script below. When you run the script, it will resize and reposition the windows accordingly. Save the script as an application, and drag it to your Dock. Whenever you’re in the office, just click it to reset the windows. You might want to consider writing a second version of the script to reposition them when you’re using the MacBook’s screen alone.
-- To set the Calendar window position and sizetell application "Calendar" set bounds of front window to {-1426, 102, -491, 930}end tell-- To set the Mail window position and sizetell application "Mail" set bounds of front message viewer to {-955, 100, -7, 845}end tell
Have a problem that the Script Doctor might help with? Send them to scriptdoctor@macworld.com.
Updated 10/29/12 4:45 p.m. PT to correct editing error in question re: printing to PDFs.