如何使用AppleScript在iMessage中启动新对话? [英] How to start new conversation in iMessage using AppleScript?

查看:520
本文介绍了如何使用AppleScript在iMessage中启动新对话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在创建一个applescript,它实际上是自动发送消息的.我现在正在工作的是:

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或任何脚本语言,所以我不确定该怎么做. /p>

发布后,我立即想到了一个粗略的解决方法.如果在发送消息之前恰好发送了一个空字符串,则可以创建一个新对话,并且该对话可以与已经存在的对话一起使用.

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

虽然可行,但我想比这个解决方案更好/更优雅.

解决方案

有很多方法可以做到这一点.

第一个示例 :

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

So I'm working on creating an applescript which essentially automates sending an imessage. What I have working now is:

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

This works for the most part except it doesn't work when starting a new conversation. When you run the script to a number which you don't have a conversation with in messages, you get a popup warning saying "Your message does not have any recipients". However, this creates a conversation with that person, and when you run the same script again it works.

I figured if it works the second time, there must be a way to create a new conversation somehow, however I have never really used applescript or really any script languages before so I'm not sure how to go about that.

Edit: Immediately after posting I thought of a rough workaround. If right before you send the message you send an empty string, you can create a new conversation, and it works with a already existing conversation.

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

While this works, I'd imagine there is a better/more elegant solution than this one.

解决方案

There are many ways to do it.

First example:

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

Second example:

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在iMessage中启动新对话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆