macOS インストールしたアプリのBundle Identifier確認方法

アプリのbundle identifierを取得

macOSにインストールしたアプリはBundle Identifierというアプリ名以外の固有名を持っています。Xcodeなどで、macOSアプリ開発を行う場合Bundle Identifierを指定しています。

XcodeでのBundle Identifier設定
XcodeでのBundle Identifier設定

macOS上にインストールされたOffice 365 Excelなども、同じくBundle Identifierを持っています。Bundle IdentifierをmacOSから簡単に確認するにはAppleScriptが利用できます。下記はSystem EventsのIdentifierを取得するコード例です。ターミナルで実行します。

osascript -e 'id of app "System Events"'

System Eventsは「com.apple.systemevents」であることが確認できます。

macOSにインストールしたOffice 365 Excelの場合は下記のように確認できます。

osascript -e 'id of app "Microsoft Excel"'

ExcelのBundle Identifierはcom.microsoft.Excelでした

C# .NET6 macOS Dictionary キー があるかどうか

C# .NET6 macOS Dictionary キー一覧

C#Dictionaryでキーがあるかどうかを確認するにはContainsKey()を使います。下記はコード例です

var dic = new Dictionary<string,string>{
    {"名称", "ハインライン"}
};
if(dic.ContainsKey("名称")){ //"名称"というキーがあるかどうか
    Console.WriteLine(dic["名称"]);
}

キーが無い場合dic[“名称”]とすると下記のようなエラーがでました。

例外が発生しました: CLR/System.Collections.Generic.KeyNotFoundException
型 'System.Collections.Generic.KeyNotFoundException' のハンドルされていない例外が System.Private.CoreLib.dll で発生しました: 'The given key '名称' was not present in the dictionary.'

その場合TryGetValue()を使って取得もできるのですがあまりメリットを感じない

var dic = new Dictionary<string,string>{
    {"名称", "ハインライン"}
};
string outstr = "";
if (dic.TryGetValue("名称",out outstr))
    Console.WriteLine(outstr);

Dictionaryキーが無い場合TryGetValue()で試すとout TValueの中身がnullになるようでした。

Dictionary キー一覧取得

var dic = new Dictionary<string,string>{
    {"名称", "ハインライン"}
};
foreach(var a in dic.Keys){
    Console.WriteLine("{0}={1}",a,dic[a]);
}

上記コードのようにKeysを使えばキー一覧(KeyCollection)を取得できます。

2022 MJELD TECHNOLOGIES. ALL RIGHTS RESERVED