2013-05-16 26 views
2

我試圖圍繞窗口從編程移動的Python在OS X上OS X:從Python的

我發現的AppleScript here的#2片段,其做此移動窗口,但我想這樣做使用Python或其他「真正」的腳本語言。

這是我的Python腳本,它不起作用。我在他們每個下面寫下了打印命令的輸出。

#!/usr/bin/python 

from Foundation import * 
from ScriptingBridge import * 

app = SBApplication.applicationWithBundleIdentifier_("com.apple.SystemEvents") 

finderProc = app.processes().objectWithName_("Finder") 

print finderProc 
# <SystemEventsProcess @0x74b641f0: SystemEventsProcess "Finder" of application "System Events" (29683)> 

finderWin = finderProc.windows()[0] 

print finderWin 
# <SystemEventsWindow @0x74b670e0: SystemEventsWindow 0 of SystemEventsProcess "Finder" of application "System Events" (29683)> 

print finderWin.name() 
# Macintosh HD 

finderWin.setBounds_([[20,20],[100,100]]) 
# no visible result 

finderWin.setPosition_([20,20]) 

最後一個命令(setPosition_)崩潰,出現以下異常。

Traceback (most recent call last): 
    File "/Users/mw/Projekte/Python/winlist.py", line 17, in <module> 
    finderWin.setPosition_([20,20]) 
AttributeError: 'SystemEventsWindow' object has no attribute 'setPosition_' 

如何使setBounds命令起作用?

+0

這是AppleScript的工作原理:'''告訴應用程序「System Events」; 將申請流程「Finder」的第一個窗口的位置設置爲{20,20} ; end tell ''' –

+0

你只是想在Python代碼中完成它嗎?爲什麼不使用osascript和do和os.system調用? – maranas

+0

因爲我不想使用一個特定的窗口,但顯示用戶可以從中選擇一個窗口的列表。如果我能夠直接在Python中完成這可能會更容易。 –

回答

2

如果你想從Python與OS X的Accessibility API交互,然後嘗試atomac。系統事件只是AppleScriptable封裝各種系統API,但PyObjC和其他Python庫已經爲您提供了廣泛的系統API訪問權限,而無需處理任何AS/SB廢話。

-

P.S您可能需要啓用系統偏好設置輔助功能窗格中的「輔助設備」選項,否則大多數的輔助功能將不可用。

+0

最後,我使用可訪問性API獲取了它,但我切換到本機Objective C並修改了Apple提供的示例([UIElementInspector](https://developer.apple.com/library/mac/#samplecode/UIElementInspector/Introduction/) Intro.html))。 –

+0

atomac目前似乎不適用於python3:https://github.com/pyatom/pyatom/issues/141 –

3

你不必通過系統事件(我懷疑這會工作)。相反,直接在Finder應用上執行:

from ScriptingBridge import * 

app = SBApplication.applicationWithBundleIdentifier_("com.apple.Finder") 
finderWin = app.windows()[0] 
finderWin.setBounds_([[100,100],[100,100]]) 
finderWin.setPosition_([20,20]) 

您不需要基金會導入。

+0

我不能這樣做,因爲我不想依賴目標應用程序腳本支持。它應該適用於所有應用程序。 –