如何使用AppleScript在提供的服务中仅发送iMessage文本?

26

有人可以向我展示如何通过 Messages 应用程序直接向 iMessage 用户发送消息吗?

tell application "Messages"
    if service type of service of buddy whose name is ("Name here" is "iMessage") then set x to 1
    if service type of service of buddy whose name is ("Name here" is "iMessage") then set x to 2
    if service type of service of buddy whose name is ("Name here" is "iMessage") then set x to 3

    send "Message" to buddy "mail of name" of service x
end tell

我需要只通过iMessage向一个账户发送消息,而不是通过google talk、AIM、Bonjour发送。 谢谢!

6个回答

66

不需要硬编码iMessage服务,你可以自动找到它:

  1. 将以下脚本保存为 sendMessage.applescript(注意:确保选择Text选项)。
  2. 运行以下命令来执行它:osascript sendMessage.applescript 1235551234 "Hello there!"
  3. 这将向电话号码为123-555-1234的手机发送一条包含文本“Hello there!”的iMessage。

sendMessage.applescript:

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

@fabian789 谢谢你的提示。你知道绕过这个限制的方法吗?既然你可以手动从消息应用中给自己发送iMessage,那么是不是没有办法通过脚本来实现呢? - Chris
1
@Chris 不,我认为这是不可能的。 - fabian789
@fabian789 是的,你可以。 - Vitim.us
3
我尝试发送至我的iCloud电子邮件,但收到了错误提示“Messages got an error:Can't send a message to yourself.”。因此,我尝试将消息发送至我的iMessage另一个别名,比如我的电话号码,这样就成功了。 - Vitim.us
1
我想吸引你的注意力,这是我提出的一个问题,有500分赏金:https://dev59.com/1FkT5IYBdhLWcg3wIsEd - andilabs
显示剩余8条评论

11

据我所了解,您无法通过AppleScript启动新对话。因此,好友和服务必须匹配,并且必须有正在进行的对话。

如果您知道好友的名称,则可以执行以下操作

tell application "Messages"
    get name of service of buddy "Buddy Name"
end tell

这将返回适合于好友的服务名称。当然,您也可以使用服务ID。但我喜欢使用名称。

最终,您将能够发送一条消息,其中包含

tell application "Messages"
    send "Text of Message" to buddy "+43 555 XXXXXXX" of service "E:example@mac.com"
end tell

1
要开始新的对话,请按照以下步骤操作 - https://dev59.com/1FkT5IYBdhLWcg3wIsEd。我已经尝试过,可以正常工作。 - mac

9
这个脚本将每隔10~30秒发送一条消息。
tell application "Messages"

    set targetBuddy to "+12025551234"
    set targetService to id of 1st service whose service type = iMessage

    repeat

        set textMessage to "Hi there!"

        set theBuddy to buddy targetBuddy of service id targetService
        send textMessage to theBuddy

        delay (random number from 10 to 30)

    end repeat

end tell

你能看一下这个网址吗?https://dev59.com/1FkT5IYBdhLWcg3wIsEd - andilabs

4
tell application "Messages"
    set myid to get id of first service
    set theBuddy to buddy "mybuddy@me.com" of service id myid
    send "Washer Done!" to theBuddy
end tell

我有同样的问题,在搜索一番后找到了答案。为了让“第一个服务”成为iMessage,您需要进入iMessage首选项帐户并重新排列iMessage帐户,使其成为第一个帐户。之后,这将像魅力一样工作。
如果不存在对话,则也会开始对话。
希望可以帮到您!

3

例子:

get services
get name of services
get buddies
get name of buddies 

您的那一行:

send "Test-Message" to buddy "buddy" of service "service"

如果“buddy”和“service”有效,似乎可以正常工作。

我的iMessage已经注册了我的Apple-ID,所以当我执行“获取服务名称”时,我会得到一个字符串形式的服务名称:

"E:myAppleID@host.com"

我可以将其用于“service”。Buddy只是您的好友姓名,也是纯文本。请参阅“获取好友名称”。

希望能起作用!


0

好的,我刚刚将以下内容制作成了一个自动化操作,它可以获取已登录用户的全名,从通讯录中查找匹配的 iPhone 号码、服务名称,最后将传入的文本(来自上一个操作)发送给...我自己的 iMessage。目前对我来说并不是非常有用,但我证明了这种方式是可行的 :)

set input to "Testing 123" **//This line is just for testing**
tell application "System Events"
    set MyName to get full name of current user
end tell
  tell application "Contacts"
      set myPhone to value of phone 1 of (person 1 whose name = MyName)   
      whose label = "iPhone"
   end tell
tell application "Messages"
    set MyService to get name of service of buddy MyName
    send input to buddy myPhone of service MyService
end tell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接