Archives / Search ›

AppleScript is fun!

No, not really; I find it really frustrating programming almost exclusively by trial and error. I figured I might as well potentially save some people time in experimentation, and share the couple of scripts I wrote this week.

Move message to saved (view as HTML, download AppleScript) lets me use my inboxes as “to-do” lists; after I put the script in ~/Library/Scripts/Mail Scripts/ and name it “Move message to saved___cmd-d”, I can use command-D to move mail from my inbox to a mailbox named after the month.

It does for Apple Mail what takes a couple of lines of configuration file in Mutt:

folder-hook . save-hook .+ `date +'=%Y-%m'`
folder-hook \\+/.* set mbox=`date +'=+/%Y-%m'`

(The Mail version only matches INBOX and +/INBOX boxes, though, since in actuality those are the only ones I ever evaluate the save-hook from.)

Mutt’s save-hooks let me automatically file mail according to any characteristic of the mail I want. It’s perfect when I want certain pieces of mail to be high-priority enough to be dumped into my inbox, but don’t want them saved along with the rest of my mail (say, if they are sent to a mailing list). It’s a bit like Eudora’s ability to specify that certain rules should only apply when manually filtering. Apple Mail has an “Apply Rules” menu item, but there appears to be no way to specify that certain rules don’t get applied automatically.

Thankfully, Apple Mail is reasonably scriptable. The obvious handler for selected messages doesn’t work, for some reason; when I try to extract the mailboxes from those messages, despite their being the exact same mailbox, they don’t test as equal to the two inboxes I want to check for. So I just grab the selection out of the frontmost mail viewer explicitly.

Otherwise, the only other painful part was ensuring that some message was selected once the mail was moved; I had to do that with accessibility scripting (hit the down arrow key, after spending 10 minutes figuring out what key code that was).

Still, scripting the Mac is a lot nicer than it used to be. Here’s hoping whatever eventually replaces AppleScript will deal with the terminology messes once and for all.

No comments yet. Be the first.

Leave a reply