0

Hoping someone can help with this – I'm running macOS Monterey on an Apple M1 Pro laptop. My AppleScript to send out batch text messages, stored on an Excel file, delivered through the Messages app is now not working – it worked fine on my old laptop operating under Catalina.

The problem appears to be in delivering the phone number ("targetBuddyPhone") into the proper location in the Messages app. Instead, the text message ("targetMessage") is being dropped into the recipient location in the app. Does anyone have any ideas on possible solutions?

Thanks in advance.

on run {input, parameters}
    
    set phoneCol to "B"
    set messageCol to "C"
    set startRow to 1
    set counter to "D"
    
    set xlsFilePath to (path to desktop as text) & "texttest.xlsx"
    tell application "Microsoft Excel" to open file xlsFilePath
    tell application "Microsoft Excel"
        set endRow to value of cell (counter & startRow) as number
    end tell
    repeat with thisRow from startRow to endRow
        tell application "Microsoft Excel"
            set targetBuddyPhone to string value of cell (phoneCol & thisRow) as string
            set targetMessage to value of cell (messageCol & thisRow) as string
        end tell
        
        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 targetBuddyPhone -- input the phone number
            key code 36 -- press Enter to focus on the message area 
            delay 3
            keystroke targetMessage -- type some message
            key code 36 -- press Enter to send
        end tell
        
        delay 6
    end repeat
    
    
    return input
end run
Steve1754a
  • 197
  • 1
  • 9
  • I have neither **Excel** or **Messages** setup in **macOS Monterey**, so I can't test your _code_ to see exactly why it's failing. That said, the obvious, based on the _code_, is that the expected **UI element** is not focused when the _code_ that fails does. You'll need to do a little debugging. You will first need to set focus to the proper **UI element** after the **⌘N** is made. – user3439894 Nov 09 '21 at 22:53
  • Thanks for the comment. Can you possibly point me to any resource(s) that detail how to set focus on a UI element? – Steve1754a Nov 09 '21 at 23:37

2 Answers2

0

Since GUI scripting is always tightly tied to the version of the application, I recommend getting rid of it once and for all and using the following more durable solution:

on run {input, parameters}
    set phoneCol to "B"
    set messageCol to "C"
    set startRow to 1
    set counter to "D"
    
    set xlsFilePath to (path to desktop as text) & "texttest.xlsx"
    tell application "Microsoft Excel"
        open file xlsFilePath
        set endRow to value of cell (counter & startRow) as number
    end tell
    tell application "Messages"
        activate
        set SMSService to service named "SMS" -- YOU NEED THIS SERVICE
    end tell
    
    repeat with thisRow from startRow to endRow
        tell application "Microsoft Excel"
            set targetBuddyPhone to string value of cell (phoneCol & thisRow) as string
            set targetMessage to value of cell (messageCol & thisRow) as string
        end tell
        tell application "Messages"
            set theBuddy to buddy targetBuddyPhone of SMSService
            send targetMessage to theBuddy
        end tell
    end repeat
    
    return input
end run
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
  • Thanks for the reply. This script generated the following error, however: "Messages got an error: Invalid key form. Invalid key form." – Steve1754a Nov 11 '21 at 12:29
  • Additional information – I see the following error detail in Script Editor 'ascr'\'err '{'----':'utxt'("Messages got an error: Invalid key form."), 'errn':-10002, 'erob':'obj '{ 'form':'name', 'want':'icsv', 'seld':'utxt'("SMS"), 'from':null() }}. SMS forwarding is enabled in Messages, so I don't understand what further I need to do? – Steve1754a Nov 11 '21 at 13:27
  • Test firstly the code as plain script without involving the AUtomator to see what code line throws error – Robert Kniazidis Nov 11 '21 at 15:39
  • Thanks – here's the error message error "Messages got an error: Invalid key form." number -10002 from account "SMS" – Steve1754a Nov 11 '21 at 17:07
  • In which code line is this error thrown... – Robert Kniazidis Nov 11 '21 at 19:05
  • Try to login to services running command **log in** before the code line **set SMSService to service named "SMS"**. I updated my answer – Robert Kniazidis Nov 11 '21 at 19:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239131/discussion-between-robert-kniazidis-and-steve1754a). – Robert Kniazidis Nov 11 '21 at 19:20
  • Negative – same error persists. – Steve1754a Nov 11 '21 at 19:20
0

I figured out the solution. To my GUI-based approach, I inserted delays into the script, which solved the problems.

on run {input, parameters}

set phoneCol to "B"
set messageCol to "C"
set startRow to 1
set counter to "D"

set xlsFilePath to (path to desktop as text) & "texttest.xlsx"
tell application "Microsoft Excel" to open file xlsFilePath
tell application "Microsoft Excel"
    set endRow to value of cell (counter & startRow) as number
end tell
repeat with thisRow from startRow to endRow
    tell application "Microsoft Excel"
        set targetBuddyPhone to string value of cell (phoneCol & thisRow) as string
        set targetMessage to value of cell (messageCol & thisRow) as string
    end tell
    
    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
        delay 3
        keystroke targetBuddyPhone -- input the phone number
        delay 3
        key code 36
        delay 3
        key code 36 -- press Enter to focus on the message area 
        delay 3
        keystroke targetMessage -- type some message
        delay 3
        key code 36 -- press Enter to send
    end tell
    
    delay 6
end repeat


return input
end run

To Robert's solution, I changed one line (set SMSService to ...) and this script now works properly.

on run {input, parameters}
set phoneCol to "B"
set messageCol to "C"
set startRow to 1
set counter to "D"

set xlsFilePath to (path to desktop as text) & "texttest.xlsx"
tell application "Microsoft Excel"
    open file xlsFilePath
    set endRow to value of cell (counter & startRow) as number
end tell
tell application "Messages"
    activate
    set SMSService to 1st account whose service type = SMS -- YOU NEED THIS SERVICE
end tell

repeat with thisRow from startRow to endRow
    tell application "Microsoft Excel"
        set targetBuddyPhone to string value of cell (phoneCol & thisRow) as string
        set targetMessage to value of cell (messageCol & thisRow) as string
    end tell
    tell application "Messages"
        set theBuddy to participant targetBuddyPhone of SMSService
        send targetMessage to theBuddy
    end tell
end repeat

return input
end run
Steve1754a
  • 197
  • 1
  • 9