Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

macos

cocoa

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.

like image 457
dimsuz Avatar asked Feb 25 '10 09:02

dimsuz


4 Answers

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];
like image 130
Nyx0uf Avatar answered Sep 22 '22 18:09

Nyx0uf


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.

like image 4
ashcatch Avatar answered Sep 23 '22 18:09

ashcatch


Did you look into NSRunningApplication?

like image 1
catlan Avatar answered Sep 21 '22 18:09

catlan


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.

like image 1
Kevin Grant Avatar answered Sep 21 '22 18:09

Kevin Grant