最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Getting the name of the current application on macOS Swift - Stack Overflow

matteradmin6PV0评论

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!
Post a comment

comment list (0)

  1. No comments so far