【发布时间】:2013-05-16 22:28:08
【问题描述】:
我正在尝试通过 OS X 上的 Python 以编程方式在 Windows 中移动。
我在 Stackoverflow 上找到了 AppleScript here 的 sn-p,它可以做到这一点,但我想用 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 命令起作用?
【问题讨论】:
-
这是有效的 AppleScript:
tell application "System Events" ; set position of first window of application process "Finder" to {20, 20} ; end tell -
你只想用 Python 代码来做吗?为什么不直接使用 osascript 和 do 和 os.system 调用?
-
因为我不想使用一个特定的窗口,而是显示一个列表,用户可以从中选择一个窗口。如果我能够直接在 Python 中完成,那可能会更容易。
标签: python macos pyobjc scripting-bridge