I'm very new to a cocoa programming and I can't find the way to do the following:
From what I've found in Carbon API it looks like the calls i should use are launchApplication() and setFrontProcess().
But how to do this in Cocoa? I.e. launch it, get PID, set that PID to be a front process. I tried to google for examples and find nothing...
If any of you can provide a minimalistic sample that would be awesome :)
Thanks in advance.
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];
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.
Did you look into NSRunningApplication?
NSRunningApplication is available on Mac OS X 10.6 or later.
If you have to support earlier systems, this can be done with APIs such as GetCurrentProcess() and SetFrontProcess() and the old ProcessSerialNumber structure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With