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.
Creating the AppleScripts
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.











Reader Comments (Page 1 of 3)
4-07-2008 @ 7:35PM
AceyTech said...
Yikes, I think I'd add an additional qualifier in there such as: -> Sender -> Contains -> [myemail]@XYZ.com
Reply
4-07-2008 @ 10:31PM
byaah said...
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.
4-07-2008 @ 7:35PM
ddebor said...
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
4-07-2008 @ 7:37PM
Cory Bohon said...
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.
4-07-2008 @ 7:35PM
Dan said...
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
4-07-2008 @ 7:41PM
BabyGotMac said...
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
4-07-2008 @ 7:59PM
Bender Bending Rodriguez said...
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
4-07-2008 @ 7:59PM
Bender Bending Rodriguez said...
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
4-07-2008 @ 8:06PM
Bender Bending Rodriguez said...
Got it. I was trying 'shut down' and 'close'. :D
• tell application "Mail" to quit
4-08-2008 @ 6:30PM
Brandon said...
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.
4-13-2008 @ 8:56PM
John Reynolds said...
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
4-07-2008 @ 8:02PM
Bender Bending Rodriguez said...
I hit reply once. What gives!
4-07-2008 @ 8:02PM
Beerguy said...
Why not just open the terminal on your phone, ssh into your machine and shut it down?
Reply
4-07-2008 @ 8:03PM
Bender Bending Rodriguez said...
Not everyone has a hacked iPhone, and sending a simple email is much faster.
4-07-2008 @ 8:09PM
Beerguy said...
True - but it's a heck of a lot more secure. Spoofing an email address isn't exactly rocket science.
4-07-2008 @ 9:37PM
Murphy Mac said...
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.
4-07-2008 @ 9:25PM
Timmargh said...
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
4-07-2008 @ 11:00PM
krye said...
Sounds like virus-like activity to me.
Reply
4-07-2008 @ 11:05PM
p2rjade said...
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
4-07-2008 @ 11:06PM
Pascual Cortés-Monroy said...
Hi,
I just wanted to know if this is possible when using Entourage.
If it's possible please tell me how Thanks
Reply