Keyboard Maestro Macro / AppleScript: Mail This Selected Item

*(This post is a part of a series on Keyboard Maestro, [see more here](http://brooksreview.net/tag/KM-SERIES/).)* As part of my job I compile a monthly PDF report which I send out to the owners of properties I manage. I do this for quite a few properties every month. When I started I used a bunch of TextExpander […]

*(This post is a part of a series on Keyboard Maestro, [see more here](http://brooksreview.net/tag/KM-SERIES/).)*

As part of my job I compile a monthly PDF report which I send out to the owners of properties I manage. I do this for quite a few properties every month.

When I started I used a bunch of TextExpander shortcuts to build the email, but this soon became too cumbersome. Not one to rest, I eventually built a Keyboard Maestro macro based off of Automator to send these files. Using Automator allowed me to pre-fill all the info in the email, with custom dates and `To` fields, but Automator has a significant downside.

Anyone who triggers Automator via Keyboard Maestro knows that Automator is a really slow tool to use. I would be waiting for my computer to think and work before I could do anything else and this drove me nuts.

I immediately started to work with AppleScript to try and replace Automator — I figured out how to do everything except automatically attach the selected file from Finder. The more I searched, the less I came up with a solution. Until [Eelco Lempsink](http://tupil.com) got in touch, responding to my plea for help on App.net, Lempsink sent over an awesome AppleScript that builds a new email with the selected Finder item attached.

So with Lempsink’s permission, [here’s the AppleScript he built](https://f3a98a5aca88d28ed629-2f664c0697d743fb9a738111ab4002bd.ssl.cf1.rackcdn.com/mail-attach-fixed-again.zip):

tell application "Finder"
 -- Make a list to gather the names of the selected files
 set fileAliases to {}
 -- Get the selection of the frontmost Finder window
 set fileSelection to the selection
 -- Iterate of the selection
 repeat with fileItem in fileSelection
 copy the fileItem as alias to the end of fileAliases
 end repeat
 -- Check if the selection is not empty
 if the number of items of fileAliases is 0 then
 -- Audible feedback, so the script always does something.
 beep
 else
 -- Now talk to mail to create the message
 tell application "Mail"
 set newMessage to make new outgoing message at beginning with properties {visible:true}
 -- Attach all the selected files
 repeat with fileAlias in fileAliases
 make new attachment with properties {file name:fileAlias} at after the last paragraph of newMessage
 end repeat
 -- Put Mail in the foreground
 activate
 end tell
 end if
 end tell

When executed this AppleScript grabs the selected Finder item(s) and attaches them to the email. For my purposes I needed to add a bit more to it so that I didn’t have to touch a single thing in the email before I sent it.

[Here’s my modifications](https://f3a98a5aca88d28ed629-2f664c0697d743fb9a738111ab4002bd.ssl.cf1.rackcdn.com/Maill-attach-Ben-Mod.zip):

-- Variables
 set theSubject to "PROPERTY NAME Owner's Report"
 set theContent to "Let me know if you have any questions. -Ben"
 set recipientAddress to {}
 tell application "Contacts"
 if group "CONTACT GROUP" exists then
 set recipientAddress to "CONTACT GROUP"
 end if
 end tell
 tell application "Finder"
 -- Make a list to gather the names of the selected files
 set fileAliases to {}
 -- Get the selection of the frontmost Finder window
 set fileSelection to the selection
 -- Iterate of the selection
 repeat with fileItem in fileSelection
 copy the fileItem as alias to the end of fileAliases
 end repeat
 -- Check if the selection is not empty
 if the number of items of fileAliases is 0 then
 -- Audible feedback, so the script always does something.
 beep
 else
 -- Now talk to mail to create the message
 tell application "Mail"
 set newMessage to make new outgoing message at beginning with properties {subject:theSubject, content:theContent, visible:true}
 -- Set a recipient
 tell newMessage
 make new to recipient at end with properties {address:recipientAddress}
 make new bcc recipient at end of bcc recipients with properties {address:"HIGHRISE BCC ADDRESS"}
 end tell
 -- Attach all the selected files
 repeat with fileAlias in fileAliases
 make new attachment with properties {file name:fileAlias} at after the last paragraph of newMessage
 end repeat
 -- Put Mail in the foreground
 activate
 end tell
 end if
 end tell

I’ve added a few things:

– A variable block, so that I can define items one time. Here I define the report subject, the content, and the contacts to send the email to. I added this because I had to create one Applescript for each property I manage. I decided to use a contact group instead of individual contact emails in the script because it allows for easier information change down the line: I just update the Contacts application.
– The other changes are just adding in the spots for the variables.
– Additionally I added in a BCC field where I stick my [Highrise](http://highrisehq.com) BCC address for logging my email correspondence.

That’s the AppleScript, and it rocks.

## Keyboard Maestro

I, of course, trigger this with Keyboard Maestro. You could use FastScripts, but I use Keyboard Maestro so that I can add one more thing: today’s date. I know you can get this in AppleScript, but for the life of me I can’t figure out how to format the date the way I want it. ((I truly suck at Applescript.))

The Keyboard Maestro Macro.

Depending on what fields you have Mail showing in the compose window you may need to edit the tabbing and arrows on the macro — essentially I just use keyboard shortcuts to navigate to the subject line and append today’s date.

The entire macro only takes a few seconds to run and that makes it much faster than Automator.

I stack every property under one hot key trigger so I can just click the one I want to use. This is saving me a ton of work each month.

BECOME A MEMBER

Join Today, for Exclusive Access.


Posted

in

by