【问题标题】:AppleScript Message Format -- Total fail on new line feedAppleScript 消息格式 - 新换行符完全失败
【发布时间】:2020-08-04 23:17:22
【问题描述】:

我在连接到 iPhone 的 MacBook 笔记本电脑上使用 AppleScript 发送一大堆单独的文本消息。

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

想要的消息:

大家好,

这将是周五上午 10 点举行的一次特别会议。 请拨入组会议,接入线路xxxxxxxxxxx

讨论主题:季度销售。

大家干得好,本季度销售额达到顶峰;所有 得到加薪,yippee。会议上分享的细节。

再次感谢大家

苏珊,
销售经理

这就是它的到达方式。

大家好,这将是一个特别的会议 周五,上午 10 点。请拨入组会议,接入线路 xxxxxxxxxxx 讨论主题:季度销售。大家干得好, 本季度销售额达到顶峰;我们都在加薪, 伊皮。会议上分享的细节。再次感谢所有的苏珊, 销售经理

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

注意:reference

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

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

编辑:我将分享一些手机屏幕截图。

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

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

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

【问题讨论】:

  • 不是每个新行都会导致消息被发送吗?看起来你最终会得到每一行都是它自己的信息。
  • 而不是\n 使用& return & 在您想要换行的地方连接文本。例如:set textMessage to "Hello Everybody," & return & return & "This is going to be..."。或者改用& linefeed &,例如set textMessage to "Hello Everybody," & linefeed & linefeed & "This is going to be..."
  • @Robc 查看我的帖子的编辑。这个也是失败的。欣赏这个想法。还有其他人吗?这里的问题是我的手机一次会发送 100x7 = 700 条短信。你认为这会让我的电话服务提供商大吃一惊吗?
  • 很确定我在七个版本前尝试过。但我肯定会再试一次。失败。它只是回显 html 命令,并显示:<html><p>Hello Everybody,</p><p>This is going to be a special meeting taking place on Friday, 10AM. ...。我在有和没有<html> 的情况下都试过了,都失败了。
  • 是否可以使用诸如'0x2028'(NSLineSeparatorCharacter)之类的字符?又名,软回报?

标签: macos applescript sms message


【解决方案1】:

此代码有效(部分改编自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

【讨论】:

  • 您确实看到我引用了确切的 stackoverflow 帖子作为参考?您是否在 Mac 上尝试过该代码?它适用于您以前未使用过的电话号码吗?看起来您没有测试该代码(我可以看到您添加了两次国家/地区代码,我知道这会失败。)。请注意该 S.O. 中的投票计数和 cmets。发布。我会清理你的代码并尝试一下,但我很确定这是我的尝试 #3(我现在在 #15 左右)。您说“此代码有效”,这意味着您完全按照编写的方式对其进行了测试。这是真的吗?
  • @zipzit 我测试了该代码(尽管我注释掉了数字列表并将我的数字放入测试)并且它按您指定的方式工作。证明:imgur.com/a/zQLASyW
  • 所以是的,我测试了你的代码。输出格式看起来很棒。除了失败。该代码不会向新联系人发送短信,即您以前从未与之交谈或寻呼过的人。与参考文献中的注释和 cmets 相同。这是您错过的注释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。是的,没有它就是失败。感谢您的尝试。
【解决方案2】:

只是处理“换行”问题...要获得内联回车,您需要键入 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 完全破坏了这个脚本。重大失败。不知道为什么。
  • @zipzit:还没有在 Big Sur 上,所以我无法测试它。您收到任何错误消息吗?
  • 不。任何地方都没有消息。它开始正常,丢失上面的换行符,然后跳过第二个电话消息接收者,跳转到第三个#,粘贴电话号码 2 并将消息集中到一个大量文本中,发送,时间从那里往南走。我正在尝试记录并查看发生了什么......等等......问题,我是否应该在 S.O.还是在 AppleMac 堆栈上?
  • @zipzit:让我们从这里开始,看看我是否能轻松搞定。如果持续时间过长,您将不得不发布一个新问题。
  • @zipzit:通常,AppleScript 在主要修订版之间没有太大变化,但我会看看 BS 修订版列表,看看是否有什么特别之处。
【解决方案3】:

所以当我将 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 不同。不知道这是怎么回事。一种方法有效,一种无效。
  • 在启动脚本/宏之前,您需要确保 Messages 在 MacOS 上打开并运行。
  • 您不想在消息完成一半时启动宏。奇怪的事情发生了。
  • 在宏运行时停止程序是有问题的。如果您在粘贴操作中间停止操作,则用于消息的内容最终会粘贴到脚本编辑器的中间。那真是一团糟。如果您必须停止,请确保您没有损坏原始脚本。
  • 对于非常长的电话号码列表,您可能需要根据 S.O.63927015 在每行末尾使用“选项 L”作为续行字符 (¬)

非常感谢 Ted Wrigley 对原始问题的意见。

【讨论】:

    猜你喜欢
    • 2019-10-11
    • 2020-08-28
    • 1970-01-01
    • 2011-11-10
    • 2019-12-28
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多