【发布时间】:2012-05-26 04:42:56
【问题描述】:
当我在 Mac OS X 上使用 subprocess.Popen 启动应用程序时,它会在后台启动,您必须单击 Dock 中的图标才能将其带到前面。如何让它在前台启动?
我尝试过使用“打开”,但这会创建不需要的终端窗口。
注意:该应用程序是从使用 wxPython 编写的 GUI 应用程序启动的。
【问题讨论】:
标签: python macos foreground
当我在 Mac OS X 上使用 subprocess.Popen 启动应用程序时,它会在后台启动,您必须单击 Dock 中的图标才能将其带到前面。如何让它在前台启动?
我尝试过使用“打开”,但这会创建不需要的终端窗口。
注意:该应用程序是从使用 wxPython 编写的 GUI 应用程序启动的。
【问题讨论】:
标签: python macos foreground
Snies 的回答对我有用。但是,由于您只需要 NSRunningApplication 和 NSApplicationActivateIgnoringOtherApps,因此您最好不要导入其他所有内容。以下对我有用,而且速度要快得多:
from Cocoa import NSRunningApplication, NSApplicationActivateIgnoringOtherApps
pid = 1456
x = NSRunningApplication.runningApplicationWithProcessIdentifier_(pid)
x.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)
【讨论】:
我认为您将需要使用本机 API 和一些 python 绑定。
NSRunningApplication 和它的方法 activateWithOptions 是你所需要的。这是一个如何使用它的示例:How to launch application and bring it to front using Cocoa api?
查看PyObjC 进行绑定。
from Foundation import *
from Cocoa import *
import objc
pid = 1456
x = NSRunningApplication.runningApplicationWithProcessIdentifier_(pid)
x.activateWithOptions_(NSApplicationActivateAllWindows)
更新:
以下行在激活应用程序时更具侵略性。
x.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)
另外,您可能需要先.unhide() 应用程序。
x.hide()
x.unhide()
【讨论】: