Open another Mac app

Tom Coomer picture Tom Coomer · Dec 16, 2014 · Viewed 10.6k times · Source

In my app I would like to open another app that is installed on the User's Mac (such as iPhoto). I am not sure what I should be looking for in the documentation. What is this called and how should I do it? Thank you

Answer

Leo Dabus picture Leo Dabus · Dec 16, 2014

Xcode 8.3.2 • Swift 3.1

import Cocoa

func openPhotos() {
    NSWorkspace.shared().open(URL(fileURLWithPath: "/Applications/Photos.app"))
}

openPhotos()

Or using launchApplication with the app name parameter in the method:

import Cocoa

func openApp(_ named: String) -> Bool {
    return NSWorkspace.shared().launchApplication(named)
}

// Usage

if openApp("Photos") {
    print(true)
}

// Or

print(openApp("Photos") ? true : false)

Swift 4 and later

Use NSWorkspace.shared instead of NSWorkspace.shared()