这可能是一个简单的问题,但是我有点困惑,因为我没有在网上找到很多例子。
我已经能够使用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] 删除。
我来说两句