1

I'm sending out a whole bunch of individual text messaged using AppleScript on a MacBook laptop linked to an iPhone.

If I create a message, copy paste it manually into Messages, and send that out manually, one message at a time (copy paste message, copy paste phone number, send) things works just fine. I can easily format the message in my draft, and the formatting is retained. If I try to do this via script, the linefeeds get lost.

Desired message:

Hello Everybody,

This is going to be a special meeting taking place on Friday, 10AM. Please call in to the group meeting, access line xxxxxxxxxxx

Topic of Discussion: Quarterly Sales.

Great Job everybody, Sales are thru the roof this quarter; We're all getting pay raises, yippee. Details shared at the meeting.

Again, thanks to all

Susan,
Sales Manager

And this is how it arrives.

Hello Everybody,This is going to be a special meeting taking place on Friday, 10AM. Please call in to the group meeting, access line xxxxxxxxxxxTopic of Discussion: Quarterly Sales. Great Job everybody, Sales are thru the roof this quarter; We're all getting pay raises, yippee. Details shared at the meeting. Again, thanks to all Susan, Sales Manager

And here is the appleScript:

set textMessage to "Hello Everybody,\n\nThis is going to be a special meeting taking place
    on Friday, 10AM.  Please call in to the group meeting, access line xxxxxxxxxxx\n\nTopic
    of Discussion: Quarterly Sales.  \n\nGreat Job everybody, Sales are thru the roof this 
    quarter;  We're all getting pay raises, yippee. Details shared at the meeting. 
    \n\nAgain, thanks to all \n\nSusan, \nSales Manager\n"

set phonelist to {"1999-555-6850", "1999-555-9496", "1999-555-7170", "1999-555-4445", 
    "1999-555-1182", "1999-555-7463", "1999-555-1809", "1999-555-8916", "1999-555-5139", 
    "1999-555-5252", "1999-555-6646", "1999-555-3642", "1999-555-2437", "1999-555-0755", 
    "1999-555-8732", "1999-555-6202", "1999-555-0310", "1999-555-7410", "1999-555-3300", 
    "1999-555-0655"}
set i to 0
activate application "Messages"
tell application "System Events" to tell process "Messages"
    repeat with indPhone in phonelist
        set i to i + 1
        key code 45 using command down -- press Command + N to start a new window
        keystroke indPhone   -- input the phone number
        delay 1
        key code 36
        key code 36           -- press Enter to focus on the message area 
        keystroke textMessage -- type some message
        delay 1
        key code 36           -- press Enter to send
        say i
        delay 5 -- Audio plus delay = success tracking. 
                -- If for some reason something goes wrong, I know where I am.  
                -- e.g. phone rings during the process.
    end repeat
end tell

Note: reference.

Note#2. Oh, and note this isn't the actual message being sent. It's just a contrived sample for StackOverflow. This audience receiving the messages just doesn't understand what happens when someone replies to a group message. They just don't get it, sigh. So no, group text pages are NOT the answer. We want individual text messages, one per person. But thanks for that suggestion. Typically we're sending out just less than 100 messages with this technique, at one time.

Any thoughts on why we're losing the \n formatting when this runs as a script? If you run this exact script on your Mac, do you see the same results?

Edit: I'm going to share some screen shots off the phone.

What I want (created via manual copy & paste into Messages app):

What I want to see

Here's what I get with the script above (/n/n):

Here's what I get with script including /n

And here's what I get with the RobC & return & technique. (See comments)

Not what I want & return technique

zipzit
  • 3,778
  • 4
  • 35
  • 63
  • Wouln't each new line cause the message to be sent? It seems like you'd end up with each line being it's own message. – l'L'l Apr 22 '20 at 06:09
  • Instead of `\n` use `& return &` to concatenate the text where you want a linebreak. For example: `set textMessage to "Hello Everybody," & return & return & "This is going to be..."`. Or use `& linefeed &` instead, e.g. `set textMessage to "Hello Everybody," & linefeed & linefeed & "This is going to be..."` – RobC Apr 22 '20 at 07:13
  • @Robc See the edits to my posting. This one is also a fail. Appreciate the thought. Got any others? The problem here is that my phone will be sending out 100x7 = 700 text messages at one time. Do you think that will raise eyebrows at my phone service provider? – zipzit Apr 22 '20 at 16:41
  • Pretty sure I tried that seven versions ago. But sure I'll try again. Fail. It just echos the html commands, and displays: `

    Hello Everybody,

    This is going to be a special meeting taking place on Friday, 10AM. ...`. I tried it both with and without ``, fail.

    – zipzit Apr 22 '20 at 17:03
  • Is it possible to a character such as '0x2028' (NSLineSeparatorCharacter)? AKA, a soft return? – Mockman Apr 22 '20 at 17:59

3 Answers3

1

Just dealing with the 'newline' problem... To get an inline carriage return you need to type control-return. To accomplish that with AppleScript, break the textMessage variable up into a list of paragraphs, then keystroke in each paragraph followed by key code 36 using control down to make the paragraph break.

set textMessageParts to {"Hello Everybody,", "", "This is going to be a special meeting taking place on Friday, 10AM.  Please call in to the group meeting, access line xxxxxxxxxxx", "", "Topic of Discussion: Quarterly Sales.", "", "Great Job everybody, Sales are thru the roof this quarter;  We're all getting pay raises, yippee. Details shared at the meeting. ", "", "Again, thanks to all", "", "Susan,", "Sales Manager"}
-- empty strings are added above to make two sequential line breaks

set phonelist to {"1999-555-6850", "1999-555-9496", "1999-555-7170", "1999-555-4445", "1999-555-1182", "1999-555-7463", "1999-555-1809", "1999-555-8916", "1999-555-5139", "1999-555-5252", "1999-555-6646", "1999-555-3642", "1999-555-2437", "1999-555-0755", "1999-555-8732", "1999-555-6202", "1999-555-0310", "1999-555-7410", "1999-555-3300", "1999-555-0655"}
set i to 0
activate application "Messages"
tell application "System Events" to tell process "Messages"
    repeat with indPhone in phonelist
        set i to i + 1
        keystroke "n" using command down -- press Command + N to start a new window
        keystroke indPhone -- input the phone number
        delay 1
        key code 36
        key code 36 -- press Enter to focus on the message area 
        repeat with thisPara in textMessageParts
            keystroke thisPara -- type one paragraph from the list
            key code 36 using control down -- type an inline line break
        end repeat
        delay 1
        key code 36 -- press Enter to send
        say i
        delay 5 -- Audio plus delay = success tracking. 
                -- If for some reason something goes wrong, I know where I am.  
                -- e.g. phone rings during the process.
    end repeat
end tell
Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17
  • Ouch. MacOS update to Big Sur `version 11.0.1` totally breaks this script. Major fail. Not sure why. – zipzit Nov 25 '20 at 20:30
  • @zipzit: not on Big Sur yet, so I can't test it. Do you get any error messages? – Ted Wrigley Nov 25 '20 at 20:35
  • Nope. No messages anywhere. It starts okay, loses the line breaks above, then skips the second phone msg recipient, jumps to the third #, pastes phone number 2 and the message lumped into one mass text, sends that, and the timing goes way south from there. I'm trying to record and see what's going on... etc... question, should I pull a new concern either here on S.O. or over at the AppleMac stack? – zipzit Nov 25 '20 at 20:39
  • @zipzit: Let's start here and see if I can figure it out without too much trouble. If it goes on too long, you'll have to post a new question. – Ted Wrigley Nov 25 '20 at 21:23
  • @zipzit: usually, AppleScript doesn't change much between major revisions, but i'll take a look at the BS revision list and see if anything jumps out at me. – Ted Wrigley Nov 25 '20 at 21:25
  • I thought I'd go back to basics, record my creation of a simple message and see what's going on. I can't even see the results of the record process. [(new question pulled on Apple S.O)](https://apple.stackexchange.com/questions/407313/how-do-i-use-the-record-button-in-applescript) One thing that is sort of helpful. I've sprinkled lots of `say` messages in there so I can follow along with the process flow. (JS equivalent is console.log..)_ – zipzit Nov 25 '20 at 21:39
  • I got the basic script running (without the nice empty space formatting) by adding small delays and changing the send command to: `key code {55, 35} --Press Command + Enter to send` – zipzit Nov 25 '20 at 22:25
0

This code works (partially adapted from here):

set textMessage to "Hello Everybody,\n\nThis is going to be a special meeting taking place
on Friday, 10AM.  Please call in to the group meeting, access line xxxxxxxxxxx\n\nTopic
of Discussion: Quarterly Sales.  \n\nGreat Job everybody, Sales are thru the roof this 
quarter;  We're all getting pay raises, yippee. Details shared at the meeting. 
\n\nAgain, thanks to all \n\nSusan, \nSales Manager\n"

set phonelist to {"1999-555-6850", "1999-555-9496", "1999-555-7170", "1999-555-4445", 
"1999-555-1182", "1999-555-7463", "1999-555-1809", "1999-555-8916", "1999-555-5139", 
"1999-555-5252", "1999-555-6646", "1999-555-3642", "1999-555-2437", "1999-555-0755", 
"1999-555-8732", "1999-555-6202", "1999-555-0310", "1999-555-7410", "1999-555-3300", 
"1999-555-0655"}

repeat with indPhone in phonelist
    tell application "Messages"
        set targetService to (id of 1st service whose service type = iMessage)
        set theBuddy to buddy ("+1" & indPhone) of service id targetService
        send textMessage to theBuddy
    end tell
end repeat
  • You did see that I referenced that exact stackoverflow posting as a reference? Did you try that code on your Mac? Did it work for phone numbers you’ve not used previously? It looks like you didn’t test that code (I can see you adding the country code twice and I know that will fail.). Notice the vote count and the comments in that S.O. Posting. I wii clean up you code and try that, but I’m pretty sure that was my attempt #3 (I’m at #15 or so now). You said “this code works” which implies you tested it exactly as written. Is that true? – zipzit Apr 22 '20 at 22:59
  • @zipzit I tested that code (Although I commented out the list of numbers and put my number in to test) and it worked as you specified. Proof: https://imgur.com/a/zQLASyW –  Apr 23 '20 at 00:11
  • so yes, I tested your code. The output format looks great. Except it's a fail. That code will NOT send out a text message to a new contact, someone you've never spoke with or paged before. Same as the notes and comments in the reference. Here's the note you missed `Hey guys i'm confused.. why is this marked as the answer?.. it doesn't start a new conversation. – jon Nov 21 '18 at 19:01`. And yes without that it's a fail. Thx for trying though. – zipzit Apr 23 '20 at 17:09
0

So it turns out when I upgraded the macOS to Big Sur version 11.0.1 the script provided by Ted Wrigley crashed. Here is a corrected version, ready for copy paste.

set textMessageParts to {"Hello Everybody,", "", "This is going to be a special meeting taking place on Friday, 10AM.  Please call in to the group meeting, access line xxxxxxxxxxx", "", "Topic of Discussion: Quarterly Sales.", "", "Great Job everybody, Sales are thru the roof this quarter;  We're all getting pay raises, yippee. Details shared at the meeting. ", "", "Again, thanks to all", "", "Susan,", "Sales Manager"}
-- empty strings are added above to make two sequential line breaks

set phonelist to {"1999-555-6850", "1999-555-9496", "1999-555-7170", "1999-555-4445", "1999-555-1182", "1999-555-7463", "1999-555-1809", "1999-555-8916", "1999-555-5139", "1999-555-5252", "1999-555-6646", "1999-555-3642", "1999-555-2437", "1999-555-0755", "1999-555-8732", "1999-555-6202", "1999-555-0310", "1999-555-7410", "1999-555-3300", "1999-555-0655"}
set i to 0
activate application "Messages"
tell application "System Events" to tell process "Messages"
    repeat with indPhone in phonelist
        set i to i + 1
        keystroke "n" using command down -- press Command + N to start a new window
        delay 1
        keystroke indPhone -- input the phone number
        delay 1
        key code 36
        key code 36 -- press Enter twice to focus on the message area 
        repeat with thisPara in textMessageParts
            keystroke thisPara -- paste one paragraph from the list
            key code 36 using shift down -- insert an inline line break
        end repeat
        delay 1
        key code 36 using command down -- press Command Enter to Send
        log ("SMS completed: " & indPhone) -- Text completed to this phone #
        say i   -- audible progress feedback  
        delay 3 -- Delay to provide an opportunity to stop the script here.
                -- If for some reason something goes wrong, I know where I am.  
                -- e.g. phone rings during the process.
    end repeat
end tell

I wanted to add a few lessons learned in fixing this thing:

  • The log is probably a better way to track progress than an audio clue. You do have to make the log area visible in the Script Editor via top menu --> View --> Show Log. You will want to be in the "Messages Tab" to see the log entries.
  • key code {57, 36} is not the same as key code 36 using shift down. Not sure what's up with that. One method works, one does not.
  • Before you start the script / macro you need to ensure Messages is open and running on the MacOS.
  • You don't want to start the macro while Messages is half way complete with a previous message. Odd things happen.
  • Stopping the program while the macro is running is problematic. If you are stopping the action during the middle of a paste operation, the content intended for the Messages ends up pasted in the middle of the Script Editor. That was a total mess. If you must stop, ensure you haven't corrupted your original script.
  • For really long lists of phone numbers, you may need to use "option L" at end of each line as continuation character (¬) per S.O.63927015

Many thanks to Ted Wrigley for his input on the original question.

zipzit
  • 3,778
  • 4
  • 35
  • 63