Filed under: Features, How-tos
AppleScript: Control your Mac with an e-mail
Have you ever been away from your Mac and wanted to shut down, restart, or needed to open an application remotely, but didn't have access to anything except your iPhone? Well, I am going to show you how to do these tasks with AppleScript and Mail. This process is fairly easy and involves creating a simple AppleScript and some Mail rules. Note that since these rules will cause your Mac to perform the listed action when they run, be careful when testing!
Continue reading to learn how to build this AppleScript.
For this how-to, I am going to show you how to shutdown, restart, and sleep your Mac. Just copy the following scripts into the Script Editor.app (~/Applications/AppleScript/Script Editor.app). These scripts are the Finder scripts that I showed you earlier.
Shutdown
tell application "Finder"
shut down
end tell
Restart
tell application "Finder" to restart
Sleep
tell application "Finder"
sleep
end tell
Saving your AppleScript
Click File > Save and choose "Script" from the "File Format:" drop-down box. Remember where you save the .scpt file.

Setting the Mail rules
This part is fairly simple and settings may vary by user preference. Open Mail.app and navigate your way to Preferences > Rules and click "Add Rule," then follow these instructions for each command you want to use:
- Add a description (this is for your information)
- Select "All" from the "if" drop-down box
- Under "If all of the following conditions are met:", add these things: (you will need to click the "+" to add the second item)
- "Subject" ... "Contains" ... "System"
- "Message Content" ... "Contains" ... "shutdown"
- Under the "Perform following actions:", add these things: (you will need to click the "+" to add the second item)
- "Set Color" ... "of text" ... "Red"
- "Run AppleScript" ...
- Beside the "Run AppleScript" action, you will see a choose box, click it and navigate to where your saved script is located. Click on the script and then on "Choose File"

To run the script, all you have to do is send an e-mail to your account with the subject and message contents as you specified them in creating the rules. For the rule that I created, I would send a message with "System" as the subject and "shutdown" as the contents of the message.
Additional Notes
- You can include unusual characters for message content conditions (i.e. #, @, or ^) in order to avoid accidentally running a script that you didn't want (for example use: #shutdown# instead of just shutdown)
- If you have multiple rules in Mail.app, you might want to consider using "Stop Evaluating Mail Rules" as an additional action
- It is also a good idea to have Mail.app check for new messages often for this to work well
Next week, I will show you how to apply this to iTunes in order to control your party shuffle.

![TUAW [Cafepress]](http://www.blogsmithmedia.com/www.tuaw.com/media/tuaw-cafepress-promo.png)


Reader Comments (Page 1 of 3)
AceyTech said 7:35PM on 4-07-2008
Yikes, I think I'd add an additional qualifier in there such as: -> Sender -> Contains -> [myemail]@XYZ.com
Reply
byaah said 10:31PM on 4-07-2008
That is a must. If anyone found out you had this set up, I can think of a few jokes they would try to pull.
ddebor said 7:35PM on 4-07-2008
Does Mail have to be running for this to work? I usually close Mail when I am done checking it and was wondering if this would still work.
Reply
Cory Bohon said 7:37PM on 4-07-2008
Yes, Mail DOES have to be opened. It is also a good idea to have it check for new messages often for this to work well.
Dan said 7:35PM on 4-07-2008
Combining Mail rules with apple script is great.
You can also Control your Mac with Twitter
Add Automator tasks, save them as apps, and have your apple script launch the app to do more than just start and shutdown your system!
Reply
BabyGotMac said 7:41PM on 4-07-2008
A recommended rule condition to add: make it only work on messages from your email address, lest your friends/coworkers decide to mess with you.
:)
Reply
Bender Bending Rodriguez said 7:59PM on 4-07-2008
When using my iPhone, I have to access All Mail instead of Inbox and I no longer get updates to new mail on my phone if I leave Mail running on my Mac as Gmail will not forward on new mail once it's grabs my a POP account.
What is the AppleScript for closing an app?
Reply
Bender Bending Rodriguez said 7:59PM on 4-07-2008
When using my iPhone, I have to access All Mail instead of Inbox and I no longer get updates to new mail on my phone if I leave Mail running on my Mac as Gmail will not forward on new mail once it's grabs my a POP account.
What is the AppleScript for closing an app?
Reply
Bender Bending Rodriguez said 8:06PM on 4-07-2008
Got it. I was trying 'shut down' and 'close'. :D
• tell application "Mail" to quit
Brandon said 6:30PM on 4-08-2008
You might want to try setting up IMAP on your Mail.app. It's free now with Gmail, and will save you from a lot of POP3 hassles.
John Reynolds said 8:56PM on 4-13-2008
you can configure Gmail to be IMAP on both the iPhone and in mail so that you can get a synchronized copy of everything.
Info for configs.
config for iPhone
http://mail.google.com/support/bin/answer.py?answer=77702&topic=12814
config for mail.app (leopard)
http://mail.google.com/support/bin/answer.py?answer=81379&topic=12814
Bender Bending Rodriguez said 8:02PM on 4-07-2008
I hit reply once. What gives!
Beerguy said 8:02PM on 4-07-2008
Why not just open the terminal on your phone, ssh into your machine and shut it down?
Reply
Bender Bending Rodriguez said 8:03PM on 4-07-2008
Not everyone has a hacked iPhone, and sending a simple email is much faster.
Beerguy said 8:09PM on 4-07-2008
True - but it's a heck of a lot more secure. Spoofing an email address isn't exactly rocket science.
Murphy Mac said 9:37PM on 4-07-2008
I posted a lot of "do stuff by email" scripts last month. Based on an idea from timmargh.net
This process is secure. It looks like an email, nobody knows the email triggers a script. Please tell me the security risk. You set your rule to only act on certain mails and there is no noteworthy risk. Especially if you use secure email.
I posted scripts for retrieving a file by email, getting a list of your files by email, and even selecting a playlist by email.
One of those posts:
http://murphymac.com/retrieve-a-remote-file-by-email/
beerguy - ssh is not always as convenient as email. And why wouldn't you want another way to accomplish something? You want less ways? I wouldn't claim one way is better or worse than ssh. It's just ANOTHER way - which is good.
Timmargh said 9:25PM on 4-07-2008
I did this about two and a half years ago:
http://timmargh.net/archive/2005/11/put_your_mac_to_sleep_by_sending.php
Reply
krye said 11:00PM on 4-07-2008
Sounds like virus-like activity to me.
Reply
p2rjade said 11:05PM on 4-07-2008
I knew you could do these types of things with Rules, however I would really like to setup my Boss's computer with a few scripts.
I want a script that sends me his IP address so that when he needs help, and I need to remotely connect to his computer to bail him out, I can do it without him fumbling through his computer.
I normally have him visit Ipchicken.com to get this info, but if I could do all steps for him, that would be great.
Any ideas Cory or anyone????
Reply
Pascual Cortés-Monroy said 11:06PM on 4-07-2008
Hi,
I just wanted to know if this is possible when using Entourage.
If it's possible please tell me how Thanks
Reply