Skip to content Skip to sidebar Skip to footer

How To Start An App In The Foreground On Mac Os X With Python?

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 t

Solution 1:

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

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?

Look at PyObjC for bindings.

fromFoundationimport *
fromCocoaimport *
import objc

pid = 1456 
x = NSRunningApplication.runningApplicationWithProcessIdentifier_(pid)
x.activateWithOptions_(NSApplicationActivateAllWindows)

Update:

The following line is more aggressive in activating the app.

x.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)

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

x.hide()     
x.unhide()

Solution 2:

Snies' answer worked for me. However, since you only need NSRunningApplication and NSApplicationActivateIgnoringOtherApps, you might as well not import everything else. The following worked for me and was considerably faster:

from Cocoa import NSRunningApplication, NSApplicationActivateIgnoringOtherApps
pid = 1456 
x = NSRunningApplication.runningApplicationWithProcessIdentifier_(pid)
x.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)

Post a Comment for "How To Start An App In The Foreground On Mac Os X With Python?"