Extension on Francesco Germinara's answer, in Swift 4, MacOSX 10.13.2:
extension Bundle { class func bundleIDFor(appNamed appName: String) -> String? { if let appPath = NSWorkspace.shared.fullPath(forApplication: appName) { if let itsBundle = Bundle(path: appPath) { // < in my build this condition fails if we're looking for the ID of the app we're running... if let itsID = itsBundle.bundleIdentifier { return itsID } } else { //Attempt to get the current running app. //This is probably too simplistic a catch for every single possibility if let ownID = Bundle.main.bundleIdentifier { return ownID } } } return nil } }
By placing it in your Swift project, you can call it like this:
let id = Bundle.bundleIDFor(appNamed: "Mail.app")
or
let id = Bundle.bundleIDFor(appNamed: "Mail")
source share