2012-02-23 12 views

答えて

16

/内容にキーCFBundleIdentifierの値だ:あなたのようなメソッドを呼び出すことができますメールの

- (NSString *) bundleIdentifierForApplicationName:(NSString *)appName 
{ 
    NSWorkspace * workspace = [NSWorkspace sharedWorkspace]; 
    NSString * appPath = [workspace fullPathForApplication:appName]; 
    if (appPath) { 
     NSBundle * appBundle = [NSBundle bundleWithPath:appPath]; 
     return [appBundle bundleIdentifier]; 
    } 
    return nil; 
} 

そう:

NSString * appID = [self bundleIdentifierForApplicationName:@"Mail"]; 

appIDは今com.apple.mail

が含まれています
+0

ありがとうございます!それはまさに私が探していたものです。しかし、私のテストでは 'Mail.app'もうまくいきます。リンゴは' .app'が多くのドキュメントではオプションであると言っても驚くことではありません。しかし、 'MAil.app'と' Mail.APP'も動作しているようですので、おそらくアプリケーション名が大文字と小文字を区別しているとは限りません。 – Tony

+0

OKそうでない旨のメモを削除します – zenopolis

0

それは次のような方法は、名前のアプリケーションのアプリケーションのバンドル識別子を返しますInfo.plistファイル

0

これはスウィフト4で、フランチェスコGerminaraの答えに拡大することができ、迅速な実装

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 "" 
} 
0

で、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 
    } 
} 

それをあなたスウィフトプロジェクトを配置するには、このようにそれを呼び出すことができます:

let id = Bundle.bundleIDFor(appNamed: "Mail.app") 

または

let id = Bundle.bundleIDFor(appNamed: "Mail")