2016-10-22 29 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