AppleScript 消息格式——换行时完全失败

AppleScript Message Format -- Total fail on new line feed

我在链接到 iPhone.

的 MacBook 笔记本电脑上使用 AppleScript 发送了一大堆单独的短信

如果我创建一条消息,将其手动复制粘贴到消息中,然后手动发送,一次一条消息(复制粘贴消息,复制粘贴 phone 号码,发送)一切正常。我可以轻松地在我的草稿中格式化消息,并且保留格式。如果我尝试通过脚本执行此操作,换行符会丢失。

想要的消息:

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

这就是它到达的方式。

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

这里是 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

注:

注#2。哦,请注意,这不是发送的实际消息。它只是 Whosebug 的人为示例。收到消息的这些观众只是不明白当有人回复群消息时会发生什么。他们只是不明白,叹息。所以不,组文本页面不是答案。我们想要单独的短信,每人一条。但是谢谢你的建议。通常,我们使用这种技术一次只能发送不到 100 条消息。

当这个 运行 作为脚本时,我们为什么会丢失 \n 格式?有什么想法吗?如果您 运行 在 Mac 上使用完全相同的脚本,您会看到相同的结果吗?

编辑:我将在 phone 上分享一些屏幕截图。

我想要的(通过手动复制并粘贴到消息应用程序中创建):

这是我使用上面的脚本 (/n/n) 得到的结果:

这就是我使用 RobC & return & 技术得到的结果。 (见评论)

此代码有效(部分改编自 ):

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

只是在处理 'newline' 问题...要获得内联回车 return,您需要键入 control-return。要使用 AppleScript 完成此操作,请将 textMessage 变量分解为段落列表,然后在每个段落中击键,然后按 key code 36 using control down 以打断段落。

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

结果是,当我将 macOS 升级到 Big Sur version 11.0.1 时,Ted Wrigley 提供的脚本崩溃了。这是更正后的版本,可以复制粘贴。

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

我想补充一些在解决这个问题时学到的经验教训:

  • 日志可能是比音频线索更好的跟踪进度的方式。您必须通过顶部菜单 --> 查看 --> 显示日志使日志区域在脚本编辑器中可见。您需要在“消息选项卡”中查看日志条目。
  • key code {57, 36}key code 36 using shift down 不同。不知道这是怎么回事。一种方法有效,另一种无效。
  • 在启动脚本/宏之前,您需要确保消息已打开并且 运行 在 MacOS 上。
  • 您不想在“信息”与上一条信息完成一半时启动宏。奇怪的事情发生了。
  • 在宏 运行 时停止程序是有问题的。如果您在粘贴操作的中间停止操作,则用于消息的内容最终会粘贴在脚本编辑器的中间。那真是一团糟。如果您必须停止,请确保您没有损坏原始脚本。
  • 对于非常长的 phone 数字列表,您可能需要在每一行的末尾使用“选项 L”作为连续字符 (¬) 每 S.O.63927015

非常感谢 Ted Wrigley 对原始问题的投入。