How to start new conversation in iMessage using AppleScript?
Asked Answered
L

2

19

So I'm working on creating an applescript which essentially automates sending an imessage. What I have working now is:

on run {msg, phoneNum}
    tell application "Messages"
        set serviceID to id of 1st service whose service type = iMessage
        send msg to buddy phoneNum of service id serviceID
    end tell
end run

This works for the most part except it doesn't work when starting a new conversation. When you run the script to a number which you don't have a conversation with in messages, you get a popup warning saying "Your message does not have any recipients". However, this creates a conversation with that person, and when you run the same script again it works.

I figured if it works the second time, there must be a way to create a new conversation somehow, however I have never really used applescript or really any script languages before so I'm not sure how to go about that.

Edit: Immediately after posting I thought of a rough workaround. If right before you send the message you send an empty string, you can create a new conversation, and it works with a already existing conversation.

on run {msg, phoneNum}
    tell application "Messages"
        set serviceID to id of 1st service whose service type = iMessage
        send "" to buddy phoneNum of service id serviceID
        send msg to buddy phoneNum of service id serviceID
    end tell
end run

While this works, I'd imagine there is a better/more elegant solution than this one.

Louvain answered 18/8, 2016 at 21:32 Comment(1)
this is not working... for starting new conversationBarmecide
C
11

My solution is to tell Applescript to press "Command + N", which is the shortkey for "Start a new conversation"

activate application "Messages"
   tell application "System Events" to tell process "Messages"
   key code 45 using command down           -- press Command + N to start a new window
   keystroke "<replace with phone number>"  -- input the phone number
   key code 36                              -- press Enter to focus on the message area 
   keystroke "<replace with message>"       -- type some message
   key code 36                              -- press Enter to send
end tell

This script will start a new conversation and send the message to the phone number through iMessage

Champollion answered 4/6, 2019 at 21:38 Comment(4)
Have confirmed this working in Mojave. Required me to add Terminal/iTerm to System Preferences > Security and Privacy > AccessibiltyHindenburg
What is the key code for tab? Seems enter does not go the the text area anymoreBujumbura
This script is working for me on Catalina (10.15.4).Redwine
I tried this and had some issues. I had to send "Enter" twice after the phone number – one to select the number, one to focus the message area. I also had (?) to sprinkle "delay 0.1" between commands since otherwise it would sometimes e.g. write the message in the recipient field. And it also sometimes misses letters in the phone number or name…Jaclynjaco
I
2

There are many ways to do it.

First example:

on run {targetBuddyPhone, targetMessage}
    tell application "Messages"
        set targetService to 1st service whose service type = iMessage
        set targetBuddy to buddy targetBuddyPhone of targetService
        send targetMessage to targetBuddy
    end tell
end run

Second example:

tell application "Messages"
    set targetBuddy to "+18001234567"
    set targetService to id of 1st service whose service type = iMessage
    repeat
        set textMessage to "Hello pal!"
        set theBuddy to buddy targetBuddy of service id targetService
        send textMessage to theBuddy
        delay (random number from 10 to 30)
    end repeat
end tell
Ithnan answered 17/9, 2016 at 15:21 Comment(2)
note: apple script does not allow to start new thread in Message.app. You can only write in already open conversations.Barmecide
Hey guys i'm confused.. why is this marked as the answer?.. it doesn't start a new conversation.Tarantass

© 2022 - 2024 — McMap. All rights reserved.