python调用窗口找到文件,使用Python在Mac OS X中查找当前活动窗口
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调用窗口找到文件,使用Python在Mac OS X中查找当前活动窗口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单的签到代码_PHP实现一个小小的签到
- 下一篇: iphone怎样关闭副屏_小米新设计专利