Bundle IDで指定したアプリケーションのバンドル中(/Contents/Resources/)の現在のLocalizationに存在する指定のstringsファイル内で、指定キーの値を取得するAppleScriptです。
アプリケーションバンドル内の各ローカライズフォルダの名称は一意に決定されているものでもなく、ある程度の「ゆらぎ」が生じています。
/Contents/Resources/Japanese.lproj/ /Contents/Resources/ja.lproj/
NSLocaleからcurrentLocale()を取得すると、「ja-JP」のような識別子が得られます。これと上記のパターンのフォルダとの付け合わせのためのメソッドがどこかに存在しているはずなのですが、現状では見つけられていません(ないと各アプリケーションが正しくメニュー表示できていないので)。
まだいまひとつ、しっくりきていません。
AppleScript名:指定アプリケーション内の現在のLocaleの指定stringsファイル内の指定キーの値を取得する.scptd |
— – Created by: Takaaki Naganoya – Created on: 2019/11/15 — – Copyright © 2019 Piyomaru Software, All Rights Reserved — use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" property |NSURL| : a reference to current application’s |NSURL| property NSBundle : a reference to current application’s NSBundle property NSDictionary : a reference to current application’s NSDictionary property NSWorkspace : a reference to current application’s NSWorkspace property NSFileManager : a reference to current application’s NSFileManager property NSLocaleCountryCode : a reference to current application’s NSLocaleCountryCode property NSLocaleLanguageCode : a reference to current application’s NSLocaleLanguageCode property NSLocaleCollatorIdentifier : a reference to current application’s NSLocaleCollatorIdentifier set targID to "com.apple.iWork.Keynote" set targFile to "Keynote" –"Keynote.strings" file set targKey to "Arrange" set vStr to getAValInASpecifiedStringFileOfAllLocaleInABundle(targID, targFile, targKey) of me –> "配置" on getAValInASpecifiedStringFileOfAllLocaleInABundle(targID, targFile, targKey) set aPath to retPathFromBundleID(targID) of me set aURL to (|NSURL|’s fileURLWithPath:aPath) set aBundle to NSBundle’s bundleWithURL:aURL set aLocale to (current application’s NSLocale’s preferredLanguages()’s firstObject()) as string set curLocale to current application’s NSLocale’s currentLocale() set aDS11 to (curLocale’s objectForKey:(NSLocaleLanguageCode)) as string set aDS12 to (curLocale’s objectForKey:(NSLocaleCountryCode)) as string set aDS5 to (curLocale’s objectForKey:(NSLocaleCollatorIdentifier)) as string –set bID to (current application’s NSBundle’s mainBundle()’s preferredLocalizations()’s firstObject()) as string set langIDList to {aDS11, aLocale, aDS12, aDS5} –> {"ja", "ja-JP", "JP", "ja-JP"} repeat with i in langIDList set j to contents of i set aRes to (aBundle’s pathForResource:targFile ofType:"strings" inDirectory:"" forLocalization:(j)) set aDict to (NSDictionary’s alloc()’s initWithContentsOfFile:aRes) if aDict is not equal to missing value then set aVal to (aDict’s valueForKeyPath:(targKey)) if aVal is not equal to missing value then return aVal as string end if end if end repeat return false end getAValInASpecifiedStringFileOfAllLocaleInABundle on getLocalizationsFromBundleID(aBundleID) set aRes to retPathFromBundleID(aBundleID) of me if aRes = false then error "Wrong Bundle ID." return getSpecifiedAppFilesLocalizationListWithDuplication(aRes) of me end getLocalizationsFromBundleID –指定アプリケーションファイルの、指定Localeにおけるローカライズ言語リストを求める。重複を許容 on getSpecifiedAppFilesLocalizationListWithDuplication(appPOSIXpath) set aURL to (|NSURL|’s fileURLWithPath:appPOSIXpath) set aBundle to NSBundle’s bundleWithURL:aURL set locList to aBundle’s localizations() return locList as list end getSpecifiedAppFilesLocalizationListWithDuplication on retPathFromBundleID(aBundleID) set aURL to NSWorkspace’s sharedWorkspace()’s URLForApplicationWithBundleIdentifier:aBundleID if aURL = missing value then return false –Error return aURL’s |path|() as string end retPathFromBundleID |