升级到 Monterey 时出现 AppleScript 错误 – 群发短信应用程序

AppleScript Error on Upgrade to Monterey – Mass SMS Text Application

希望有人能帮助解决这个问题 – 我是 运行 苹果 M1 Pro 笔记本电脑上的 macOS Monterey。我用于发送批量文本消息的 AppleScript,存储在 Excel 文件中,通过消息应用程序发送,现在无法正常工作——它在我的 Catalina 下运行的旧笔记本电脑上运行良好。

问题似乎在于将 phone 号码(“targetBuddyPhone”)传送到“信息”应用中的正确位置。相反,文本消息(“targetMessage”)被放入应用程序中的收件人位置。有人对可能的解决方案有任何想法吗?

提前致谢。

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

由于GUI 脚本 总是与应用程序的版本紧密相关,我建议一劳永逸地摆脱它使用以下更持久的解决方案:

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

我想出了解决办法。对于我基于 GUI 的方法,我在脚本中插入了延迟,这解决了问题。

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

对于 Robert 的解决方案,我更改了一行(将 SMSService 设置为 ...),此脚本现在可以正常工作。

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