Manngo
Manngo

Reputation: 16399

Getting the name of the current application on macOS Swift

I’m working on a menubar extra, and I would like to fetch the name of the current application. I have the following code:

var app: NSRunningApplication = NSWorkspace.shared.frontmostApplication!
var bundleIdentifier: String = app.bundleIdentifier!
var name: String = app.localizedName!
var icon: NSImage? = app.icon

The code mostly works, but in a few cases, the localizedName property is an empty string. The other properties aare fine.

I can’t find any other property which has the application name. One such application is SQLPro for SQLite which is a legitmate App Store application. It clearly has name which appears in all the right places except as above.

Is there an alternative way of getting the application name?

A Possible Solution

Thanks to @Sweeper, I’ve got something that works:

let bundle: Bundle = Bundle(url: app.bundleURL!)!
let appName: String = app.localizedName!.isEmpty ? bundle.infoDictionary!["CFBundleName"]! as! String : app.localizedName!

Upvotes: 1

Views: 51

Answers (0)

Related Questions