使用Python中的PyObjC和ScriptingBridge在消息中发送消息

布拉克

这可能是一个简单的问题,但是我有点困惑,因为我没有在网上找到很多例子。

我已经能够使用Javascript通过Mac OS中的消息发送消息(使用本教程),但是我不知道如何使用Python和PyObjC来执行消息。

使用Javascript,我会做这样的事情:

var messages = Application('Messages');
var buddy = messages.services["E:%REPLACE_WITH_YOUR_IMESSAGE_EMAIL%"].buddies["%REPLACE_WITH_BUDDYS_EMAIL%"];
messages.send("JavaScript sent this message!", {to: buddy});

我不知道如何buddy使用Python变量设置为相关对象。以下可以正常访问消息

from Foundation import *
from ScriptingBridge import *
Messages = SBApplication.applicationWithBundleIdentifier_("com.apple.iChat")

然后,在Python中,我可以执行类似的操作。

In [182]: s = Messages.services()
In [183]: [x.name() for x in s]
Out[183]: ['E:[email protected]', 'Bonjour', 'SMS']

但是我不确定如何Messages.send_to_在创建Messages对象之后从此跃升为实际使用它发送消息

非常感谢您的帮助,非常感谢!

克里斯·卡明斯

您可以这样做:

from ScriptingBridge import SBApplication

Messages = SBApplication.applicationWithBundleIdentifier_("com.apple.iChat")

# get the first budddy who's name is Chris Cummings
buddy_to_message = [b for b in Messages.buddies() if b.fullName() == "Chris Cummings"][0]

# send text to buddy
Messages.send_to_("sending this from python test", buddy_to_message)

当尝试使用pyobjc中未公开说明的ScriptingBridge模块时,我发现确实有用的事情是搜索我尝试在repl中访问的类上可用的方法

>>>[method for method in dir(Messages) if "bud" in method.lower()]
["buddies", "buddies"]  # found the buddies method
>>>[method for method in dir(Meessages.buddies()[0]) if "name" in method.lower()]
[ ... 'accessibilityParameterizedAttributeNames', 'className',
'elementWithCode_named_', 'entityName', 'firstName', 'fullName',
'fullName', 'lastName', 'name', 'name', 'scriptAccountLegacyName',
'valueWithName_inPropertyWithKey_']

# ... this one had a bunch of other junk but hopefully this illustrates the idea

关于dir的附加说明:当然dir()也可以使用参数,并且您可以获得在对象上定义的与字符串匹配的方法的列表,dir('name')但是ObjectiveC类名几乎从未像我期望的那样大写,所以我认为搜索所有小写字母很有用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在MPI中发送和接收消息时,何时使用标签?

如何在python中的smtplib中发送UNICODE消息

Discord Python Bot - 使用 APScheduler 在 Aux 函数中发送消息

通过套接字在python中发送消息

如何发送消息和嵌入消息中

我们如何使用图形 API 在消息正文的页脚和标题中发送徽标

在Akka中发送控制消息

在Excel中发送cmd消息

在PyZMQ中发送NULL消息

在React Native中发送和接收类似Websockets的消息

如何在python中发送消息并等待回复,在whatsapp中使用twilio

如何使用python套接字在特定端口中发送消息?没有随机端口

ElementNotInteractableException:消息:元素不可交互错误,使用Selenium Python在搜索字段中发送文本

在for循环中发送多个消息Discord python bot

如何在发件箱中的Thunderbird中发送消息?

如何在Express 4.0中发送即时消息?

在Windows Phone 8.1中发送本地广播消息

在 JMeter 中的soap消息中发送动态值

使用键盘打开消息中发送的链接

如何使用php curl在Firefox中发送推送消息

如何使用Flutter在Slack通道中发送消息?

如何使用 ejabberd API 在 muc 组中发送消息?

使用 ${currentBuild.number} 在 jenkins 中发送 slack 消息

如何使用 pycord 在特定频道中发送消息?

如何使用Pinpoint从python中的长代码发送文本消息?

Python - Discord Bot Loop 命令并在私人消息中发送消息

使用 @TransactionalEventListener 在 JPA 事务中发送消息时,消息未提交(丢失)

如何使用discord.py在系统消息通道中发送问候消息

使用WebRequest和Twilio发送消息