如何使用Python在Mac OS X上的前台启动应用程序? [英] How to start an app in the foreground on Mac OS X with Python?

查看:80
本文介绍了如何使用Python在Mac OS X上的前台启动应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Mac OS X上使用subprocess.Popen启动应用程序时,该应用程序在后台启动,您必须单击扩展坞中的图标将其显示在前面.如何使它从前台开始?

When I start an app with subprocess.Popen on Mac OS X, it starts in the background and you have to click the icon in the dock to bring it to the front. How can I make it start in the foreground?

我尝试使用打开",但这会创建不需要的终端窗口.

I have tried using "open", but that creates and unwanted terminal window.

注意:该应用程序是从使用wxPython编写的GUI应用程序启动的.

Note: the app is being started from a GUI app written using wxPython.

推荐答案

我认为您将需要使用本机API和一些python绑定.

I think you will need to use the native API and some python bindings.

NSRunningApplication 和它是如何启动应用程序并使用Cocoa api将其展示在前端?

NSRunningApplication and it's method activateWithOptions is what you need. Here is an example how to use it: 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)

更新:

以下行在激活应用程序时更具攻击性.

The following line is more aggressive in activating the app.

x.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)

另外,您可能需要先对应用程序进行 .unhide().

Also you might need to .unhide() the app first.

x.hide()     
x.unhide()

这篇关于如何使用Python在Mac OS X上的前台启动应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆