使用Python在Mac OS X中查找当前活动窗口 [英] Finding the Current Active Window in Mac OS X using Python
问题描述
有没有办法在Mac OS X上使用Python在给定时间查找当前活动窗口的应用程序名称。
这应该工作:
#!/ usr / bin / python
从AppKit导入NSWorkspace
activeAppName = NSWorkspace.sharedWorkspace()。activeApplication()['NSApplicationName']
print activeAppName
b $ b
只适用于Leopard或Tiger,如果你安装了PyObjC,并且正好指向一个python二进制文件(如果你安装了通用的MacPython,你可能想要做在老虎)。但是Peter的回答和Carbon的做法可能会更快一些,因为从AppKit在Python中导入任何东西需要一段时间,或者更准确地说,在Python进程中第一次导入来自AppKit的东西需要一段时间。 / p>
如果你在PyObjC应用程序中需要这个,我所描述的将工作得很好,快速,因为你只遇到一次导入AppKit的滞后。如果你需要这个工具作为命令行工具,你会注意到性能打击。如果这与你相关,你最好使用Peter的代码作为起点在Xcode中构建一个10行的Foundation命令行工具。
Is there a way to find the application name of the current active window at a given time on Mac OS X using Python?
This should work:
#!/usr/bin/python
from AppKit import NSWorkspace
activeAppName = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationName']
print activeAppName
Only works on Leopard, or on Tiger if you have PyObjC installed and happen to point at the right python binary in line one (not the case if you've installed universal MacPython, which you'd probably want to do on Tiger). But Peter's answer with the Carbon way of doing this will probably be quite a bit faster, since importing anything from AppKit in Python takes a while, or more accurately, importing something from AppKit for the first time in a Python process takes a while.
If you need this inside a PyObjC app, what I describe will work great and fast, since you only experience the lag of importing AppKit once. If you need this to work as a command-line tool, you'll notice the performance hit. If that's relevant to you, you're probably better off building a 10 line Foundation command line tool in Xcode using Peter's code as a starting point.
这篇关于使用Python在Mac OS X中查找当前活动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!