18

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.

dimsuz
  • 8,969
  • 8
  • 54
  • 88

7 Answers7

35

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];
M Katz
  • 5,098
  • 3
  • 44
  • 66
Nyx0uf
  • 4,609
  • 1
  • 25
  • 26
  • I tried to launch an app this way from a background process, but it seems that this method of launching doesn't work in Lion or later if the process doing it is not a GUI application. – Bob Murphy Mar 08 '13 at 23:29
  • 2
    The `NSApplicationActivateAllWindows` option will cause all of the Application window to come forward, which is almost always not what you want. Instead you can pass in `NSApplicationActivateIgnoringOtherApps` to just activate the window that was most recently active in the app. – thomasfuchs May 10 '14 at 18:03
4

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
  • 2,327
  • 1
  • 18
  • 29
  • 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 :) – dimsuz Feb 25 '10 at 10:21
1

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.

Kevin Grant
  • 5,363
  • 1
  • 21
  • 24
1

Did you look into NSRunningApplication?

Rui Andrada
  • 246
  • 4
  • 9
catlan
  • 25,100
  • 8
  • 67
  • 78
0

For Swift2 version

Launch App:

let task = NSTask()
task.launchPath = "/usr/bin/env"
task.arguments = ["/Applications/TextEdit.app/Contents/MacOS/TextEdit"]
task.launch()

To get the app using bundle Identifier:

    var apps: [AnyObject] = NSRunningApplication.runningApplicationsWithBundleIdentifier("com.apple.TextEdit")
    var MyApp: [NSRunningApplication] = apps as! [NSRunningApplication]        
            for app in MyApp        
            {
            }

I am still trying to find the way to know "active", "hide" etc state of app, But not succeed till now.

Vijay Singh Rana
  • 1,060
  • 14
  • 32
0

In swift 4, you can use NSWorkspace.shared.launchApplication(appName:) to open an app. It also makes the launched app at front in my case.

You also can try:

do {
    try NSWorkspace.shared.launchApplication(at: yourAppURL,
                                             options: .andHideOthers,
                                             configuration: [:])
} catch {
    printError("Failed to launch the app.")
}

Option andHideOthers: Hide all apps except the newly launched one.

Jay Wang
  • 2,650
  • 4
  • 25
  • 51
0

Open App in Mac in Objective C

#import <Cocoa/Cocoa.h>

int main(int argc, const char * argv[]) {

    @autoreleasepool {
        // insert code here...
        [[NSWorkspace sharedWorkspace] launchApplication:@"/Applications/Books.app"];
    }
    return 0;
}
raj
  • 388
  • 5
  • 24