2016-10-22 36 views
0

这可能是一个简单的问题,但我有点困惑,因为我没有在网上找到很多例子。在Python中使用PyObjC和ScriptingBridge发送消息

我已经能够通过使用JavaScript(Using this tutorial)在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_消息。

您的帮助将不胜感激,非常感谢!

回答

1

你可以这样说:

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类名几乎不会像我期望的那样大写,所以我认为搜索它们全部是小写的。

+0

非常感谢! – Blark