using Automator/AppleScript for iMessage

I am trying to set up an automated calender event in Automator using an AppleScript. I open automator and select calender alarm as workflow type > insert Run AppleScript as my workflow. Here is my AppleScript. This should work?? It does not..




tell application "messages" to activate


delay 1


tell application "System Events"


delay 5


key code 45 using command down


delay 0.5


keystroke "Nick"


delay 0.5


key code 36


delay 0.5


key code 36


delay 0.5


keystroke "test"


delay 0.5


key code 36


delay 0.5



end tell


MacBook Air 13", macOS 10.15

Posted on Mar 2, 2020 9:26 PM

Reply
3 replies

Mar 3, 2020 6:56 PM in response to Camelot

Of course! Apologies this absurd thought process. Why would a computer think like a human... or even mimic one naturally. Thanks, this make so much more sense! One last thing.. I ran this script and it works great but only if the messages app is open previous to compiling script and running it. If not either A. it will fail to open messages and does not run B. it will fail to compile properly. I tried placing a tell application "messages" to open . before the rest of script but then it would not compile. Sorry I've never used AppleScript and am new to programming..


tell application "Messages" to activate

delay 2

tell application "Messages"


set targetService to first service whose service type = iMessage


set targetBuddy to first buddy of targetService whose name is "nick"



send "test" to targetBuddy



end tell

Mar 4, 2020 11:38 AM in response to augie197

> Of course! Apologies this absurd thought process. Why would a computer think like a human... or even mimic one naturally.


It's common when starting out to try to use AppleScript the 'human way'. Conceptually it's easy - "just do what I do", and doing it the 'right' way takes more effort and, let's me honest, isn't always obvious.


> I ran this script and it works great but only if the messages app is open previous to compiling script and running it


Hmm, that doesn't make much sense. The script should be self-contained enough to do what you describe. The OS should automatically open the app if it's not already open.


Is the same true for other apps? I'm trying to understand if this is something amiss with AppleScript or with Messages.app. Try something simple like:


tell application "Preview"

open (choose file)

end tell

Mar 3, 2020 11:48 AM in response to augie197

Umm, what are you trying to do here? send a message to a specific recipient?


Throw away all that key code mess. It's a nightmare to deal with. AppleScript works best when you tell it what you want to do, not when you try to make it replicate human actions such as keystrokes and mouse clicks.


Try this instead:


tell application "Messages"

set targetService to first service whose service type = iMessage

set targetBuddy to first buddy of targetService whose name is "nick"

send "test" to targetBuddy

end tell


It's a little wordier than it might be because

a) Messages supports different messaging platforms, so you need to first identify which one you're using.

b) Buddies can be identified by various different methods - name, phone number, nickname, etc.


but once you have this sorted out, it's far easier (and more reliable) to send this way rather than trying to have Messages.app actually type out your words.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

using Automator/AppleScript for iMessage

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