Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the foreground?

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

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

like image 320
Luke McCarthy Avatar asked Sep 01 '25 16:09

Luke McCarthy


1 Answers

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.

from Foundation import *
from Cocoa import *
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()
like image 126
snies Avatar answered Sep 04 '25 04:09

snies