如何使用 AppleScript 在 iMessage 中开始新对话?

问题描述 投票:0回答:3

所以我正在创建一个苹果脚本,它基本上可以自动发送一条消息。我现在的工作是:

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

这在大多数情况下都有效,但在开始新对话时不起作用。当您将脚本运行到消息中未与之对话的号码时,您会收到一条弹出警告,提示“您的消息没有任何收件人”。但是,这会创建与该人的对话,并且当您再次运行相同的脚本时,它会起作用。

我想如果它第二次有效,一定有一种方法可以以某种方式创建一个新的对话,但是我以前从未真正使用过 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

虽然这有效,但我想还有比这个更好/更优雅的解决方案。

macos applescript imessage
3个回答
11
投票

我的解决方案是告诉 Applescript 按“Command + N”,这是“开始新对话”的快捷键

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 将消息发送到电话号码


2
投票

有很多方法可以做到。

第一个例子

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

0
投票

这对我来说非常有用,但是只有当接收器使用 iPhone 时它才能工作,如果另一端使用 Android 则失败。

© www.soinside.com 2019 - 2024. All rights reserved.