如何使用 AppleScript 在 iMessage 中开始新对话?
How to start new conversation in iMessage using AppleScript?
所以我正在努力创建一个 applescript,它基本上可以自动发送 imessage。我现在的工作是:
on run {msg, phoneNum}
tell application "Messages"
set serviceID to id of 1st service whose service type = iMessage
send msg to buddy phoneNum of service id serviceID
end tell
end run
除了在开始新对话时不起作用外,这在大多数情况下都有效。当您将脚本 运行 发送到您在消息中没有与之对话的号码时,您会收到一个弹出警告,内容为 "Your message does not have any recipients"。但是,这会创建与该人的对话,并且当您再次 运行 相同的脚本时它会起作用。
我想如果它第二次起作用,一定有办法以某种方式创建新的对话,但是我以前从未真正使用过 applescript 或任何脚本语言,所以我不确定该怎么做.
编辑:发布后我立即想到了一个粗略的解决方法。如果就在您发送消息之前发送了一个空字符串,您可以创建一个新对话,并且它适用于已经存在的对话。
on run {msg, phoneNum}
tell application "Messages"
set serviceID to id of 1st service whose service type = iMessage
send "" to buddy phoneNum of service id serviceID
send msg to buddy phoneNum of service id serviceID
end tell
end run
虽然这行得通,但我想有一个 better/more 比这个更优雅的解决方案。
有很多方法可以做到。
第一个例子:
on run {targetBuddyPhone, targetMessage}
tell application "Messages"
set targetService to 1st service whose service type = iMessage
set targetBuddy to buddy targetBuddyPhone of targetService
send targetMessage to targetBuddy
end tell
end run
第二个例子:
tell application "Messages"
set targetBuddy to "+18001234567"
set targetService to id of 1st service whose service type = iMessage
repeat
set textMessage to "Hello pal!"
set theBuddy to buddy targetBuddy of service id targetService
send textMessage to theBuddy
delay (random number from 10 to 30)
end repeat
end tell
我的解决方案是告诉 Applescript 按 "Command + N",这是 "Start a new conversation"
的快捷键
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 "<replace with phone number>" -- input the phone number
key code 36 -- press Enter to focus on the message area
keystroke "<replace with message>" -- type some message
key code 36 -- press Enter to send
end tell
此脚本将开始新对话并通过 iMessage
将消息发送到 phone 号码
所以我正在努力创建一个 applescript,它基本上可以自动发送 imessage。我现在的工作是:
on run {msg, phoneNum}
tell application "Messages"
set serviceID to id of 1st service whose service type = iMessage
send msg to buddy phoneNum of service id serviceID
end tell
end run
除了在开始新对话时不起作用外,这在大多数情况下都有效。当您将脚本 运行 发送到您在消息中没有与之对话的号码时,您会收到一个弹出警告,内容为 "Your message does not have any recipients"。但是,这会创建与该人的对话,并且当您再次 运行 相同的脚本时它会起作用。
我想如果它第二次起作用,一定有办法以某种方式创建新的对话,但是我以前从未真正使用过 applescript 或任何脚本语言,所以我不确定该怎么做.
编辑:发布后我立即想到了一个粗略的解决方法。如果就在您发送消息之前发送了一个空字符串,您可以创建一个新对话,并且它适用于已经存在的对话。
on run {msg, phoneNum}
tell application "Messages"
set serviceID to id of 1st service whose service type = iMessage
send "" to buddy phoneNum of service id serviceID
send msg to buddy phoneNum of service id serviceID
end tell
end run
虽然这行得通,但我想有一个 better/more 比这个更优雅的解决方案。
有很多方法可以做到。
第一个例子:
on run {targetBuddyPhone, targetMessage}
tell application "Messages"
set targetService to 1st service whose service type = iMessage
set targetBuddy to buddy targetBuddyPhone of targetService
send targetMessage to targetBuddy
end tell
end run
第二个例子:
tell application "Messages"
set targetBuddy to "+18001234567"
set targetService to id of 1st service whose service type = iMessage
repeat
set textMessage to "Hello pal!"
set theBuddy to buddy targetBuddy of service id targetService
send textMessage to theBuddy
delay (random number from 10 to 30)
end repeat
end tell
我的解决方案是告诉 Applescript 按 "Command + N",这是 "Start a new conversation"
的快捷键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 "<replace with phone number>" -- input the phone number
key code 36 -- press Enter to focus on the message area
keystroke "<replace with message>" -- type some message
key code 36 -- press Enter to send
end tell
此脚本将开始新对话并通过 iMessage
将消息发送到 phone 号码