Applescript To Send iMessage As Email Notification

I was playing around AppleScript and Mail Rules and it struck me that it might be useful to be able to send an iMessage when an important email arrives in your inbox. However, a little bit of research indicated that the iOS Mail VIP inbox could already provide very similar functionality - it allows you to specify contacts that are considered important, and any emails they send you go into a separate inbox. More importantly though, is that you can configure a different notification for your VIP emails, which means you can get your phone to shout at you when these arrive, whilst any non-VIP emails can be left to quietly sidle into your "Peasant" inbox.

Slightly perturbed, I tried to see if I could think of any scenarios where I could still justify the iMessage notification script:

1. If you've turned off email push, or it's not enabled for some reason, then emails may only appear when Mail is opened, or, at best, every 15 minutes.

2. You might want more fine grained control of the emails that trigger a notification, such as the subject line, or keyword in the body of the email.

3. iMessage notifications can be set to repeat if not acknowledged, but email notifications only sound once.

4. You may want to send iMessages to various people.

Maybe not very compelling reasons for most people, but certainly reason enough to have a play and try to get it working.

Requirements

You'll need a Mac running Mountain Lion (10.8) that is always on, and a separate iCloud account (Since you can't easily send messages to yourself) from which to send the iMessage.

AppleScript

The first step is to create the AppleScript that will send the iMessage. Open AppleScript Editor (It lives in /Applications/Utilities), and paste in the following code, changing the accounts at the top of the script.

--------------
-- Change these:
--------------
property iMessageSendingAccount : "test@icloud.com"
property iMessageRecipientAccount : "myAccount@gmail.com"
--------------

using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
	set mySender to "Mail Received From: " & sender of eachMessage
	set mySubject to " Subject: " & subject of eachMessage
set myMessage to mySender & mySubject			
sendIMessage(myMessage, iMessageRecipientAccount) of me
end repeat
end tell
end perform mail action with messages
end using terms from

-- Code to send the iMessage
on sendIMessage(myMessage, myRecipient)

-- Open iMessage and wait for it to start if it's not running
tell application "Finder"
set x to name of processes
if (x does not contain "Messages") then
tell application "Messages"
activate
delay 5
end tell
end if
end tell

tell application "Messages"
set serviceName to "E:" & iMessageSendingAccount
send myMessage to buddy myRecipient of service named serviceName
end tell
end sendIMessage

Save the new AppleScript into folder ~/Library/Application Scripts/com.apple.mail

Mail Rules

Now that you've created the script, you need to configure Mail to run it. Open Preferences, and then click on the Rules icon.

Next add a new rule, selecting "Run AppleScript" as the action to perform, and select your new script as the script to run.

Screen Shot 2013-05-06 at 9.49.05 PM
Screen Shot 2013-05-06 at 9.49.05 PM

Once you've saved your mail rule, you might want to ensure that Mail is polling every minute, to ensure the quickest possible delivery.

General
General

And there you have it, any emails the match the Mail Rule will now be sent by Messages to your devices.

Messages-1
Messages-1
/
If you found this post informative, enlightening, entertaining, or it's just helped you solve a problem then please feel free to shout me a coffee for my trouble from extensive menu below:
Today's Menu