Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

Posted on Mar 10, 2018 9:26 PM

Reply
17 replies

Mar 11, 2018 10:44 PM in response to Dv3v

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.

Mar 11, 2018 11:34 PM in response to Dv3v

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.

Mar 11, 2018 11:01 AM in response to Dv3v

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.

Mar 11, 2018 11:18 PM in response to Dv3v

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

Can I send a computer commands through messages with an Applescript?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.