David Garces
Oct 21, 2015 · 2 min read

This is a short article about my small and quick app that execute another app. If you know the Bundle identifier of the external app you want to launch, you can easily test if the app is running, and launch it using NSWorkspace.sharedWorkspace().


Implementation

There are two main steps you should add to your application:

  1. You should make sure the other application is not running
  2. If it is running you can send a message to the user informing them about this, otherwise you can launch the app and send the result to the user (if required).

This is a snapshot of the code I used to accomplish this, but you can easily download a sample app from: https://github.com/gbdavid2/DavidCodesOSX/tree/master/LaunchAppExample

// Check if main app is already running; if yes, do nothing and terminate helper app
var isAlreadyRunning = false
let running = NSWorkspace.sharedWorkspace().runningApplicationsfor app in running {
if app.bundleIdentifier == bundleIdentifierTextField.stringValue {
isAlreadyRunning = true
}
}
if !isAlreadyRunning {
let path = NSWorkspace.sharedWorkspace().absolutePathForAppBundleWithIdentifier(bundleIdentifierTextField.stringValue)let result = NSWorkspace.sharedWorkspace().launchApplication(path!)appPathTextField.stringValue = path!resultTextField.stringValue = result ? "App launched!" : "App didn't launch :("
} else {
resultTextField.stringValue = "App is already running"
}

The app will ask you to write your bundle identifier and will search for another app that uses this bundle identifier before launching it:

LaunchAppExample
LaunchAppExample

Getting Bundle Identifiers

For this sample App I used Apple's default bundle identifier for the Maps App. If you don't know the bundle identifier of the app you need to execute, you may be able to get it by running a script as detailed in this question in StackOverflow.

David codes (swift)

My stories about programming with Swift

David Garces

Written by

Developer, Technologist and Blogger

David codes (swift)

My stories about programming with Swift

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade