假设您有一个应用程序的名称Mail.app
,如何以编程方式从该应用程序名称中获取com.apple.mail
?
发布于 2018-01-12 01:36:15
在Swift 4,macOS 10.13.2中扩展Francesco Germinara的答案:
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
}
}
把它放到你的Swift项目中,你可以这样叫它:
let id = Bundle.bundleIDFor(appNamed: "Mail.app")
或
let id = Bundle.bundleIDFor(appNamed: "Mail")
发布于 2012-02-23 14:42:32
它是Contents/Info.plist中关键字CFBundleIdentifier的值
发布于 2014-06-26 05:29:03
这是一种可能的快速实现。
func bundleIdentifierForApplicationName(appName : String) -> String
{
var workspace = NSWorkspace.sharedWorkspace()
var appPath : String = workspace.fullPathForApplication(appName)
if (appPath != "") {
var appBundle : NSBundle = NSBundle(path:appPath)
return appBundle.bundleIdentifier
}
return ""
}
https://stackoverflow.com/questions/9408293
复制相似问题