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

Question:

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

Reply
Question marked as Helpful

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

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?

Question marked as Helpful

Mar 11, 2018 11:34 PM in response to Dv3v 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.

There’s more to the conversation

Read all replies

Page content loaded

Question marked as Helpful

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

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?

Mar 11, 2018 11:34 PM

Reply Helpful (1)
Question marked as Helpful

Mar 11, 2018 11:34 PM in response to Dv3v 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:34 PM

Reply Helpful (1)

Mar 11, 2018 12:54 AM in response to Dv3v In response to Dv3v

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.

Mar 11, 2018 12:54 AM

Reply Helpful

Mar 11, 2018 9:58 AM in response to red_menace In response to red_menace

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

Mar 11, 2018 9:58 AM

Reply Helpful

Mar 11, 2018 11:01 AM in response to Dv3v 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:01 AM

Reply Helpful

Mar 11, 2018 10:20 PM in response to red_menace In response to red_menace

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

Mar 11, 2018 10:20 PM

Reply Helpful

Mar 11, 2018 10:44 PM in response to Dv3v 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 10:44 PM

Reply Helpful

Mar 11, 2018 11:18 PM in response to Dv3v 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

Mar 11, 2018 11:18 PM

Reply Helpful
User profile for user: Dv3v

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