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

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

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745665990a4639125.html

相关推荐

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

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

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信