Can I send a computer commands through messages with an Applescript?
Is it possible to make a script that is constantly checking iMessage for a command/message, then if it gets the specific message it runs some code?
MacBook Air
Is it possible to make a script that is constantly checking iMessage for a command/message, then if it gets the specific message it runs some code?
MacBook Air
You don't fill in the arguments, they are filled in by the Messages.app, which calls the handler when a message is received.
using terms from application "Messages"
on message received theMessage from theBuddy for theChat
-- do stuff with theMessage, theBuddy, and theChat parameters/variables
if theMessage = "say hello" then -- test what theMessage (the message received) is
say hello -- or whatever
end if
end message received
end using terms from
For more information about handlers, see the AppleScript Language Guide.
Messages.app has several event handlers (see its scripting dictionary), so I suppose you could do something like that. I would think that just logging in to the remote machine would be more direct and secure, though. Instead of some hypothetical question, exactly what is it you are trying to accomplish?
If you look at the Messages.app scripting dictionary, there are event handlers that will run when a buddy becomes available, when a message is received, etc. The "messer" application would need to be installed and running on the remote machine though, which would require some help from the "messee". If these scripts/applications are voluntarily run, hopefully you are at least running from a standard (non-admin) account.
Open the Script Editor > File > Open Dictionary… menu item and choose the Messages application. Take a look at the Messages Event Handler Suite - one (or more) of the handlers can be placed in a stay-open script or application.
I googled how to do this and a guy did it like this, I have no clue how he made it work though:
using terms from application "Messages"
on message received theMessage from theBuddy for theChat
if theMessage = "say hello" then
say hello
end if
end using terms from
If you look at the documentation, the on message received handler is called when there is an incoming message, and in your example theMessage parameter would be the incoming message, theBuddy would be the buddy who sent the message, and theChat would be the text chat (room/channel, etc). You can use theBuddy and theChat to determine if the message is coming from someone you want, and use theMessage to determine what your command is.
I'm kind of confused, but this is what I understood from that. It doesn't work and I'm not sure why:
using terms from application "Messages"
on message received "say hello" from "James"
if theMessage = "say hello" then
say hello
end if
end using terms from
Did you actually connect your AppleScript to Messages.app so that it knows to run your script? Just having the script open on your machine doesn't do anything if Messages.app doesn't know to call it.
To do this you need to save your script in ~/Library/Application Scripts/com.apple.iChat, then select as the option in Messages.app preferences -> AppleScript handler.
In addition, you need to add dummy handlers for all other other actions that Messages.app can respond to, otherwise it will throw an error when your script doesn't respond.
Finally, there's an error in your script:
say hello -- or whatever
will speak whatever is contained in the variable named 'hello', which is not defined. If you want to speak the literal word hello, then quote it:
say "hello" -- or whatever
You should end up with something like:
use AppleScriptversion "2.4" -- Yosemite (10.10) or later
use scripting additions
using terms from application "Messages"
on message receivedtheMessagefromtheBuddyfortheChat
if theMessage = "say hello" then -- test what theMessage (the message received) is
say "hello" -- or whatever
end if
end message received
# The following are unused but need to be defined to avoid an error
on received text invitationtheTextfromtheBuddyfortheChat
end received text invitation
on received audio invitationtheTextfromtheBuddyfortheChat
end received audio invitation
on received video invitationtheTextfromtheBuddyfortheChat
end received video invitation
on received file transfer invitationtheFileTransfer
end received file transfer invitation
on buddy authorization requestedtheRequest
end buddy authorization requested
on message senttheMessagefortheChat
end message sent
on chat room message receivedtheMessagefromtheBuddyfortheChat
end chat room message received
on active chat message receivedtheMessage
end active chat message received
on addressed chat room message receivedtheMessagefromtheBuddyfortheChat
end addressed chat room message received
on addressed message receivedtheMessagefromtheBuddyfortheChat
end addressed message received
on av chat started
end av chat started
on av chat ended
end av chat ended
on login finishedfortheService
end login finished
on logout finishedfortheService
end logout finished
on buddy became availabletheBuddy
end buddy became available
on buddy became unavailabletheBuddy
end buddy became unavailable
on completed file transfer
end completed file transfer
end using terms from
Alright, I added all of the dummy handlers, I put the script in the com.apple.iChat folder, then I texted myself "say hello" and I didn't get any Hello. I'm starting to think it's not possible with AppleScript.
Me and my friend like to mess with each other and we both like learning new ways to do it. I just randomly thought of this idea and was wondering how I'd go about it.
How do I see the dictionary?
How do I make it stay open?
Save the script as an application and select the "Stay open after run handler" option in the save dialog.
So I ran what you did above and then texted myself "say hello" and nothing happened. Any idea why?
Looks like I had an error in there (forgot to quote the string) - try:
if theMessage = "say hello" then
say "hello"
end if
Can I send a computer commands through messages with an Applescript?