Send SMS to Messages and run commands with AppleScript

Oct 16, '12 07:30:00AM

Contributed by: dborup

It is possible to send an SMS command to your Mac using an AppleScript. I've made one which takes a picture when I send the command /photo to iMessages on my Mac.

The script is very simple; it must be set up Messages' Alerts preferences. Choose Event > Message Received, then check Run an Apple Script. This will check each message you get for the command; in this case, /photo.

The script will turn the volume all the way down (and up again) before taking a picture with PhotoBooth so it does not make any noise.

Then you can make a symbolic link from the PhotoBooth folder to your Dropbox so you can check the photos from your mobile device.

Here's the script:

using terms from application "Messages"
	on message received theMessage from theBuddy for theChat
		if (theMessage begins with "/") then
			if theMessage begins with "Photo" then
				beep 3
				set volume 0
				tell application "Photo Booth"
				end tell
				tell application "System Events"
					tell process "Photo Booth"
						delay 3
						tell application "Finder"
							set visible of process "Photo Booth" to false
						end tell
						tell menu bar 1
							tell menu bar item "File"
								tell menu "File"
									click menu item "Take Photo"
								end tell
							end tell
						end tell
					end tell
				end tell
				delay 5
				tell application "Photo Booth"
				end tell
				set volume 3
			end if
		end if
	end message received
end using terms from

[kirkmc adds: Note that this only works with iMessages (I tried for a while to get it to work with regular chats, and it wouldn't). It's worth noting that the ability to send a text message and set off a script is something very useful, and I can imagine plenty of scripters figuring out other types of remote commands they can send to their Macs. Feel free to submit them as hints.]

Comments (6)

Mac OS X Hints