swift – Open another Mac app

XCode 11 • MacOS Catalina 10.15 • Swift 5

NSWorkspace.shared.launchApplication is deprecated and starting from the MacOS 10.15 the new function NSWorkspace.shared.openApplication shall be used.


Example – open terminal application by its bundle id

guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.apple.Terminal") else { return }

let path = "/bin"
let configuration = NSWorkspace.OpenConfiguration()
configuration.arguments = [path]
NSWorkspace.shared.openApplication(at: url,
                                   configuration: configuration,
                                   completionHandler: nil)

Example – open terminal application by its path

let url = NSURL(fileURLWithPath: "/System/Applications/Utilities/Terminal.app", isDirectory: true) as URL

let path = "/bin"
let configuration = NSWorkspace.OpenConfiguration()
configuration.arguments = [path]
NSWorkspace.shared.openApplication(at: url,
                                   configuration: configuration,
                                   completionHandler: nil)

Loading...

(Visited 73 times, 1 visits today)