How to launch application and bring it to front using Cocoa api?

Did you look into NSRunningApplication?


To start an application, use the NSWorkspace class: NSWorkspace Reference

Specifically, the launchApplication: function.

I don't know the answer of the activation part off my head. You can activate your own application with -[NSApplication activateIgnoringOtherApps:], but I don't know how to do it for other apps.


To launch an application :

[[NSWorkspace sharedWorkspace] launchApplication:@"/Applications/Safari.app"];

To activate an app :

NSRunningApplication* app = [NSRunningApplication
                             runningApplicationWithProcessIdentifier: PID];
[app activateWithOptions: NSApplicationActivateAllWindows];
// or
NSArray* apps = [NSRunningApplication
                 runningApplicationsWithBundleIdentifier:@"com.bla.blah"];
[(NSRunningApplication*)[apps objectAtIndex:0]
 activateWithOptions: NSApplicationActivateAllWindows];

Tags:

Macos

Cocoa