tags:

views:

470

answers:

3

Hello fellows!

I'm very new to a cocoa programming and I can't find the way to do the following:

  • Start a particular application by name
  • Do some work
  • Later bring that application I've started to be the front process

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.

A: 

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.

ashcatch
Thanks. That's the right function, yes, I saw it, but i can't find a way to find pid and reuse it later. Let's wait maybe someone knows the answer :)
dpimka
+1  A: 

Did you look into NSRunningApplication?

catlan
Thanks! This seems to be it :)
dpimka
+2  A: 

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];
Benj