【问题标题】:AppleScript Error on Upgrade to Monterey – Mass SMS Text Application升级到 Monterey 时出现 AppleScript 错误 – 群发短信应用程序
【发布时间】:2021-12-22 14:34:47
【问题描述】:

希望有人能提供帮助 - 我在 Apple M1 Pro 笔记本电脑上运行 macOS Monterey。我的 AppleScript 用于发送存储在 Excel 文件中、通过 Messages 应用程序传递的批量文本消息现在无法正常工作——它在我在 Catalina 下运行的旧笔记本电脑上运行良好。

问题似乎在于将电话号码(“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

【问题讨论】:

  • 我没有在 macOS Monterey 中设置 ExcelMessages,所以我无法测试您的 代码 以查看它失败的确切原因。也就是说,基于 code,显而易见的是,当失败的 code 聚焦时,预期的 UI 元素 没有聚焦。您需要进行一些调试。在制作 ⌘N 之后,您首先需要将焦点设置到正确的 UI 元素
  • 感谢您的评论。您能否指出任何详细说明如何将焦点放在 UI 元素上的资源?

标签: macos applescript sms message macos-monterey


【解决方案1】:

我想出了解决办法。对于我基于 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

【讨论】:

    【解决方案2】:

    由于 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
    

    【讨论】:

    • 感谢您的回复。但是,此脚本生成了以下错误:“消息出现错误:无效的密钥形式。无效的密钥形式。”
    • 附加信息 - 我在 Script Editor 'ascr'\'err '{'----':'utxt'("Messages got an error: Invalid key form." ), 'errn':-10002, 'erob':'obj '{ 'form':'name', 'want':'icsv', 'seld':'utxt'("SMS"), 'from':空值() }}。消息中启用了短信转发,所以我不明白我还需要做什么?
    • 首先将代码作为纯脚本进行测试,不涉及 AUtomator 以查看哪些代码行抛出错误
    • 谢谢 - 这是错误消息错误“消息出现错误:无效的密钥形式。”来自帐户“SMS”的号码 -10002
    • 在哪个代码行抛出了这个错误...
    猜你喜欢
    • 1970-01-01
    • 2022-08-08
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 2014-05-07
    相关资源
    最近更新 更多