Archive for the 'NSBundle' Category

2017/10/14 指定クラスがどのFrameworkに所属しているか検索 v3

文字列として与えたCocoaのClassがどのFrameworkに所属しているかを検索するAppleScriptのShane Stanleyによる改修版に微修正を加えたものです。

Classの所属するバンドルを求めるNSBundle’s bundleForClass:なんてものがあるとは知りませんでした。しかも、野蛮にbridgesupportファイルをオープンして検索して回るわけでもないので、1回あたりの実行時間が0.001秒以下。これはすごい(^ー^;

ただ、そうはいっても万能ではないので、参考程度といったところでしょうか(それでもオリジナル版より高機能)。

AppleScript名:指定クラスがどのFrameworkに所属しているか検索 v3
– Created 2017-10-14 by Shane Stanley
– Modified 2017-10-14 by Takaaki Naganoya
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
–http://piyocast.com/as/archives/4895

set fRes1 to searchClassInFrameworks(“JSContext”) of me
–>  ”JavaScriptCore.framework”

set fRes2 to searchClassInFrameworks(“NSApplication”) of me
–>  ”AppKit.framework”

set fRes3 to searchClassInFrameworks(“NSRect”) of me
–>  false

set fRes4 to searchClassInFrameworks(“PDFPage”) of me
–>  ”Quartz.framework”

set fRes5 to searchClassInFrameworks(“NSUTF8StringEncoding”) of me
–>  false

set fRes6 to searchClassInFrameworks(“CIColor”) of me
–>  ”CoreImage.framework”

on searchClassInFrameworks(aTarget)
  set aClass to current application’s NSClassFromString(aTarget)
  
if aClass = missing value then return false
  
set theComponenents to (current application’s NSBundle’s bundleForClass:aClass)’s bundleURL’s pathComponents()
  
set thePred to current application’s NSPredicate’s predicateWithFormat:“pathExtension == ’framework’”
  
set aRes to (theComponenents’s filteredArrayUsingPredicate:thePred)’s firstObject() as text
  
return aRes
end searchClassInFrameworks

★Click Here to Open This Script 

2017/08/02 指定のアプリケーションのURL Schemeを取得する

指定のアプリケーションに設定してあるURL Schemeを取得するAppleScriptです。

指定のアプリケーションバンドル内のInfo.plistにあるCFBundleURLSchemesにアクセスして、カスタムURLプロトコルスキームを取得します。実行にはShane Stanleyの「Bridge Plus」AppleScriptライブラリを~/Library/Script Librariesフォルダにインストールしておくことが必要です。

たとえば、写真.app(Photos.app)であれば「photos://」というカスタムURLプロトコルが定義されており、本Scriptを実行して写真.app(Photos.app)を選択すると、

–> {appName:”Photos”, appBundleID:”com.apple.Photos”, urlScheme:{”photos”}}

という結果が返ってきます。

実際に調べてみると、OSのメジャーアップデート時に予想外のアプリケーションにカスタムURLスキームが新設されていることがあります。そういう調査のために作成したものです。

もちろん、1つのアプリケーションをいちいちchoose fileで選択して調べるとかそういうことではなく、指定フォルダ以下のアプリケーションをすべて取得して、それらのアプリケーションすべてのURL Schemeを調査するScriptのための部品です。手作業で1つ1つ調べるなんて、意味のないことです。

AppleScript名:指定のアプリケーションのURL Schemeを取得する
– Created 2017-07-23 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use BridgePlus : script “BridgePlus”
–http://piyocast.com/as/archives/4759

load framework

set aP to choose file
set aURLrec to getAppURLSchemes(aP) of me
–> {appName:”Photos”, appBundleID:”com.apple.Photos”, urlScheme:{”photos”}}

on getAppURLSchemes(aP)
  set aURL to current application’s |NSURL|’s fileURLWithPath:(POSIX path of aP)
  
set aBundle to current application’s NSBundle’s bundleWithURL:aURL
  
set aDict to aBundle’s infoDictionary()
  
  
set appNameDat to (aDict’s valueForKey:“CFBundleName”) as string
  
set bundleIDat to (aDict’s valueForKey:“CFBundleIdentifier”) as string
  
  
set urlSchemes to (aDict’s valueForKey:“CFBundleURLTypes”)
  
if urlSchemes is not equal to missing value then
    set urlList to urlSchemes’s valueForKey:“CFBundleURLSchemes”
    
set urlListFlat to (current application’s SMSForder’s arrayByFlattening:urlList) as list
  else
    set urlListFlat to {}
  end if
  
  
set aRec to {appName:appNameDat, appBundleID:bundleIDat, urlScheme:urlListFlat}
  
return aRec
end getAppURLSchemes

★Click Here to Open This Script 

2017/04/18 HelpBookの項目表示、キーワード検索

macOS上でヘルプを表示するHelpViewerはAppleScriptに対応しており、任意の項目の表示、キーワード検索、指定URLのオープンなどを行えるようになっています。

ただし、HelpViewerをAppleScriptから普通に操作したときの動作が怪しいので、Cocoa経由でも操作する方法を調べておきました。

HelpViewerで指定HelpBook中の指定のアンカーを表示させるだけであれば、

AppleScript名:help_sample
tell application “HelpViewer”
  activate
  
try
    lookup anchor “scpedt1126″ in book “com.apple.ScriptEditor.help”
  end try
end tell

★Click Here to Open This Script 

このぐらいの簡素な記述でOKです。これをCocoa経由で操作すると、

AppleScript名:指定アプリケーションのヘルプブック内の指定アンカーを表示する
– Created 2017-04-18 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”
–http://piyocast.com/as/archives/4590

set anAppName to “Script Editor”
set aTargAnchor to “scpedt1126″ —-https://help.apple.com/scripteditor/mac/10.12/index.html?localePath=ja.lproj#/scpedt1126
set hRes to openHelpBook(anAppName, aTargAnchor) of me

–指定アプリケーションのヘルプブックで、指定アンカーを表示する
on openHelpBook(anAppName, aTargAnchor)
  set locBookName to getHelpBook(anAppName) of me
  
if locBookName = false then return false
  
current application’s NSHelpManager’s sharedHelpManager()’s openHelpAnchor:aTargAnchor inBook:locBookName
end openHelpBook

–指定アプリケーションのヘルプブック名称を取得する
on getHelpBook(anAppName)
  set aWorkspace to current application’s NSWorkspace’s sharedWorkspace()
  
set appPath to aWorkspace’s fullPathForApplication:anAppName
  
if appPath is equal to missing value then return false
  
  
set locBookName to (current application’s NSBundle’s bundleWithPath:appPath)’s objectForInfoDictionaryKey:“CFBundleHelpBookName”
  
if locBookName is equal to missing value then return false
  
–> “com.apple.ScriptEditor.help”
  
return locBookName
end getHelpBook

★Click Here to Open This Script 

このぐらいになります。ただし、前者の記述ではHelpBook名称があらかじめわかっていることが前提、後者ではアプリケーション名からHelpBook名を取得して項目表示するという違いがあります。

問題はキーワード検索で、HelpViewerを直接コントロールすると、

AppleScript名:HelpViewerで検索する?(未遂)
tell application “HelpViewer”
  activate
  
search looking for “用語説明”
end tell

★Click Here to Open This Script 

このぐらいの簡素な記述で済むはずですが、結果はHelpViewerに表示されません(単に記述が違っているという可能性もあります)。

これをCocoaの機能を用いて操作すると、きちんと検索結果が表示されます。Webサーバー上にあるヘルプのコンテンツを連続して同一ページを表示させると、5回に1回はエラー(未表示)になるなど、挙動がよくわかりません。

HelpViewerの中身はほぼ(遅い)Webブラウザで、有用性についても疑問が残ります。

AppleScript名:指定アプリケーションのヘルプブック内で指定キーワードを検索する
– Created 2017-04-18 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”
–http://piyocast.com/as/archives/4590

set anAppName to “Script Editor”
set aTargString to “用語説明” –”Script Dictionary” in Japanese
set hRes to searchHelpBook(anAppName, aTargString) of me

–指定アプリケーションのヘルプブックで、指定アンカーを表示する
on searchHelpBook(anAppName, aTargString)
  set locBookName to getHelpBook(anAppName) of me
  
if locBookName = false then return false
  
current application’s NSHelpManager’s sharedHelpManager()’s findString:aTargString inBook:locBookName
end searchHelpBook

–指定アプリケーションのヘルプブック名称を取得する
on getHelpBook(anAppName)
  set aWorkspace to current application’s NSWorkspace’s sharedWorkspace()
  
set appPath to aWorkspace’s fullPathForApplication:anAppName
  
if appPath is equal to missing value then return false
  
  
set locBookName to (current application’s NSBundle’s bundleWithPath:appPath)’s objectForInfoDictionaryKey:“CFBundleHelpBookName”
  
if locBookName is equal to missing value then return false
  
–> “com.apple.ScriptEditor.help”
  
return locBookName
end getHelpBook

★Click Here to Open This Script 

2017/02/14 指定バンドルIDのアプリケーションのアイコンへのパスを求める

指定バンドルIDのアプリケーションのアイコンへのパスを求めるAppleScriptです。

とりあえず、ループで各バンドルIDに該当するアプリアイコンをダイアログ表示します。

bbe.png

twa.png

find.png

AppleScript名:指定バンドルIDのアプリケーションのアイコンへのパスを求める
– Created 2017-02-14 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”
–http://piyocast.com/as/archives/4460

set iconList to {“com.barebones.bbedit”, “com.barebones.textwrangler”, “com.apple.Finder”}
repeat with i in iconList
  set aPath to retIconPath(i) of me
  
if aPath is not equal to false then
    display dialog “ICNS test” with title i with icon ((POSIX file aPath) as alias) buttons {“OK”} default button 1
  end if
end repeat

–指定バンドルIDのアプリケーションのアイコンのフルパスを取得する
on retIconPath(anID)
  set aRes to chkAppIsInstalled(anID) of me
  
if aRes = false then return false
  
set appPathPOSIX to (current application’s NSWorkspace’s sharedWorkspace()’s absolutePathForAppBundleWithIdentifier:anID) as string
  
set anIconPath to getIconFilePath(appPathPOSIX) of me
  
return anIconPath
end retIconPath

–指定バンドルIDのアプリケーションが存在しているかチェック
on chkAppIsInstalled(aBundleID as string)
  set aRes to current application’s NSWorkspace’s sharedWorkspace()’s URLForApplicationWithBundleIdentifier:aBundleID
  
set bRes to (aRes is not equal to missing value)
  
return bRes
end chkAppIsInstalled

–指定ファイルのアイコンのファイルのパスを取得する
on getIconFilePath(aPOSIXPath)
  set anURL to current application’s |NSURL|’s fileURLWithPath:aPOSIXPath
  
set myBundle to current application’s NSBundle’s bundleWithURL:anURL
  
set aBundleID to myBundle’s bundleIdentifier() as text
  
set anIconFileName to (myBundle’s objectForInfoDictionaryKey:“CFBundleIconFile”) as string
  
  
if anIconFileName does not end with “.icns” then
    set anIconFileName to anIconFileName & “.icns”
  end if
  
  
set ifonFileFullPath to aPOSIXPath & “/Contents/Resources/” & anIconFileName
  
return ifonFileFullPath
end getIconFilePath

★Click Here to Open This Script 

2017/02/09 Application Supportフォルダを求める

Application Supportフォルダを求めるAppleScriptです。

ただ、通常環境(Script Editorとか)を前提としたものではなく(動きますけど)、Xcode上のCocoa-AppleScript Applet内でCodeSignしてApp Sandboxを有効にした環境におけるApplication Supportフォルダを求めるためのものです。

一応、Script Editorや他のAppleScript開発環境でも動きますが、Sandbox環境では返ってくるパスが全然違います。そうした一種の極限環境でも値を取得していろいろやるための実験であります。

AppleScript名:Application Supportフォルダを求める
– Created 2017-02-09 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
–http://piyocast.com/as/archives/4450

set fileManager to current application’s NSFileManager’s alloc()’s init()
set bundleID to current application’s NSBundle’s mainBundle()’s bundleIdentifier()
set urlPaths to fileManager’s URLsForDirectory:(current application’s NSApplicationSupportDirectory) inDomains:(current application’s NSUserDomainMask)
set appDirectory to (((urlPaths’s objectAtIndex:0)’s URLByAppendingPathComponent:bundleID isDirectory:true)’s |path|()) as string

–> “/Users/me/Library/Application Support/au.com.myriad-com.ASObjC-Explorer-4″–ASObjC Explorer 4の場合
–> “/Users/me/Library/Application Support/com.apple.ScriptEditor2″–Apple純正のScript Editorの場合

★Click Here to Open This Script 

2017/01/31 DragThing上で選択中のSlotのアイコン画像をデスクトップに書き出す

DragThing上で選択中のSlotのアイコン画像をデスクトップに書き出すAppleScriptです。

ランチャーの老舗、James ThomasonのDragThing(バージョン5.9.17)を愛用しています。macOS標準搭載のDockには最小限のアプリケーションのみ登録しておき、基本的にはDragThingから起動。

dt0.png

dt1.png

さまざまなアプリケーションの紹介を行うKeynote書類を作るために、Finder上で選択しておいたアプリケーションのアイコン画像を書き出すAppleScriptは作ってありました。

dt00_resized.png

その一方で、DragThing上の選択中のアプリケーションのアイコンを書き出すようなものはなかったので、DragThing上で選択→Finderに表示→アイコン書き出しScript実行 と操作していたのですが、あまりに不毛なのでDragThing上から直接書き出せるものを作ってみました。

Dockに大量のアプリケーションを登録し、1個1個が豆粒大になっているユーザーを見かけますが、大量のアプリケーションを登録・管理するのであれば、タブでジャンル分けして管理できるDragThingのほうがおすすめです。

また、DragThingはURLも登録しておけます。DragThingをブックマークとして利用することで、Webブラウザの共通ブックマークとして運用できるため、日常的に複数のWebブラウザを利用している人にとってはたいへん便利でしょう。

dt2_resized.png

dt3.png

macOS標準装備のScript Menuに登録して呼び出しています。

dt4_resized.png

AppleScript名:DragThing上で選択中のSlotのアイコンをデスクトップに書き出し v2
– Created 2017-01-31 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
–http://piyocast.com/as/archives/4419

tell application “DragThing”
  –Version 5.9.17で動作確認
  
set aSel to selection
  
if aSel = {} then return
  
  
set bSelected to {}
  
repeat with i in aSel
    set j to contents of i
    
try
      –ここでエラーが出ないということは、Dockに登録したファイルである(URLではない)と仮定
      
set aPath to file location of j
      
      
—選択したSlotの内容がアプリケーションだったらデスクトップにアイコン画像を書き出す
      
set fRes to detectAppFile(aPath) of me
      
if fRes = true then
        writeOutICNS(aPath) of me
      end if
    end try
  end repeat
  
end tell

on detectAppFile(aPath)
  tell application “System Events”
    set fType to file type of aPath
    
return (fType as string = “APPL”)
  end tell
end detectAppFile

–指定ファイルのアイコンをCFBundleIdentifierの名称でtmpにPNGで書き出す
on writeOutICNS(aFile)
  set aPOSIXpath to POSIX path of aFile
  
set anURL to current application’s |NSURL|’s fileURLWithPath:aPOSIXpath
  
  
set myBundle to current application’s NSBundle’s bundleWithURL:anURL
  
set aBundleID to myBundle’s bundleIdentifier() as text
  
set anIconFileName to (myBundle’s objectForInfoDictionaryKey:“CFBundleIconFile”) as string
  
  
–アイコン名には拡張子を省略してもよいので、ない場合には補う
  
if anIconFileName does not end with “.icns” then
    set anIconFileName to anIconFileName & “.icns”
  end if
  
  
set ifonFileFullPath to aPOSIXpath & “Contents/Resources/” & anIconFileName
  
  
set sText to “cp “ & quoted form of ifonFileFullPath & ” ~/Desktop/” & quoted form of (aBundleID & “.icns”)
  
do shell script sText
  
end writeOutICNS

★Click Here to Open This Script 

2016/02/08 アプリケーションのBundle IDからアプリアイコンへのフルパスを取得する

指定のバンドルID(com.apple.Safariとか)からアプリケーションへのパスを取得し、アプリケーションのInfo.plistを読み込んでアイコンファイル名を取得。アプリケーションバンドル内のファイル名とタイプを指定して、アプリケーションアイコンへのフルパスを取得するAppleScriptです。

Pure AppleScriptで書くとけっこうめんどくさくて長くなる内容ですが、逆にCocoaの機能を使うと短く簡潔に書けます。

AppleScript名:アプリケーションのBundle IDからアプリアイコンへのフルパスを取得する
– Created 2016-02-08 by Takaaki Naganoya
– 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aBundleID to "com.apple.Safari"
set aPath to retAppIconPathFromBundleID(aBundleID) of me
–>  "/Applications/Safari.app/Contents/Resources/compass.icns"

–Bundle IDからアプリケーションアイコンのフルパスをPOSIX pathで返す
on retAppIconPathFromBundleID(aBundleID)
  –Bundle IDからアプリケーションへのパスを取得する
  
set appPath to current application’s NSWorkspace’s sharedWorkspace()’s absolutePathForAppBundleWithIdentifier:aBundleID
  
if appPath = missing value then return false
  
  
–Info.plistを読み込んで、iconのファイル名を取得
  
set aDict to (current application’s NSBundle’s bundleWithPath:appPath)’s infoDictionary()
  
set iconFile to aDict’s valueForKey:"CFBundleIconFile"
  
if iconFile = missing value then return false
  
  
–パスからバンドルを取得して、バンドル内の指定ファイルタイプのファイル名のパスを取得する
  
set aBundle to current application’s NSBundle’s bundleWithPath:appPath
  
set iconFullPath to aBundle’s pathForResource:iconFile ofType:"icns"
  
if iconFullPath = missing value then return false
  
  
return iconFullPath as string
end retAppIconPathFromBundleID

★Click Here to Open This Script 

2015/11/09 Safariと同じUser Agent文字列を組み立てる

Cocoaの機能を用いて、SafariのUser Agent名が返す文字列と同じものを生成するAppleScriptです。

さまざまな用途のために、AppleScriptでカスタムWebブラウザを作ることがあります。その際に、Safariと同じUser Agent名をWebサーバー側に返すことができるとメリットがあります。正確にいえば、Safariのフリをしてブラウズを行うことで、さまざまなトラブルを回避できます。

取得にあたっては、Safariを起動してSafariからUser Agent名を取るのではなく、WebKitが返すUser Agent情報に(未起動状態の)SafariのバージョンやビルドNo.をつないで生成しています。ビルドNo.についてはメジャーNo.の下3桁だけが出力されており、少々不思議なところですが、実物のSafariと合うように調整しています。

一応本ルーチンが正常に文字列を生成できているかのテストのために、SafariからUser Agent名を取得して比較を行い、同一の文字列が生成できていることを確認しています(実際に使うときには削除)。

AppleScript名:SafariのUser Agent文字を組み立てる
– Created 2015-11-09 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “Webkit”

set aRes to getSafariUserAgentString() of me
–>  ”Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.6 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.6″

–ほんまもんのSafariからUser Agentを取得してテスト
tell application “Safari”
  tell document 1
    set dRes to do JavaScript “navigator.userAgent”
    
–>  ”Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.6 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.6″
  end tell
end tell

return (aRes = dRes)
–>  true

–Safariと同じUser Agentの文字列を取得する
on getSafariUserAgentString()
  set aBundle to current application’s NSBundle’s bundleWithIdentifier:“com.apple.Safari”
  
set aVer to (aBundle’s infoDictionary()’s objectForKey:“CFBundleShortVersionString”) as text
  
–>  ”9.0.2″
  
  
set aBuildNo to (aBundle’s infoDictionary()’s objectForKey:“CFBundleVersion”) as text
  
–>  ”11601.3.6″
  
set {v1, v2, v3} to parseVersionNumber(aBuildNo) of me
  
set aV1 to current application’s NSString’s stringWithString:v1
  
set bV1 to aV1’s substringFromIndex:2 –3 characters from tail –?????
  
–> “601″
  
  
set bStr to retStrFromArrayWithDelimiter({bV1, v2, v3}, “.”) of me
  
  
set aWebView to current application’s WebView’s alloc()’s init()
  
set aRes to (aWebView’s stringByEvaluatingJavaScriptFromString:“navigator.userAgent”) as text
  
–>  ”Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.6 (KHTML, like Gecko)”
  
  
return (aRes & ” Version/” & aVer & ” Safari/” & bStr)
  
end getSafariUserAgentString

–バージョン番号文字列からメジャーバージョンを取り出し数値として返す
on parseVersionNumber(a)
  set aStr to current application’s NSString’s stringWithString:a
  
set aRes to (aStr’s componentsSeparatedByString:“.”)
  
set bRes to aRes’s allObjects()
  
return bRes as list
end parseVersionNumber

–リストを指定デリミタをはさんでテキスト化
on retStrFromArrayWithDelimiter(aList, aDelim)
  set anArray to current application’s NSArray’s arrayWithArray:aList
  
set aRes to anArray’s componentsJoinedByString:aDelim
  
return aRes as text
end retStrFromArrayWithDelimiter

★Click Here to Open This Script 

2015/09/08 Bundle関連のじっけん v2

Cocoaの機能を用いて、実行中のプログラムのBundle IDを取得するAppleScriptです。

「path to me」を実行できない環境(Cocoa-AppleScriptアプレット)で実行中のアプレットのBundle IDを取得するために調べたものです。

Cocoa-AppleScriptアプレットは滅多に使わないものですが、実際に使ってみるとAppleScriptアプレットの常識が通じない(外部から内部のハンドラを呼べない。外部からイベントハンドラを呼ぶといろいろ制約が大きい)ので、なかなか困りものです。

AppleScript名:ASOCでBundle関連のじっけん v2
– Created 2015-09-07 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

set myBundle to current application’s NSBundle’s mainBundle()
set aBundleID to myBundle’s bundleIdentifier() as text
–>  ”au.com.myriad-com.ASObjC-Explorer-4″

set aPath to (myBundle’s bundlePath()) as text
–>  ”/Applications/ASObjC Explorer 4.app”
–> “/Applications/Utilities/Script Editor.app”

★Click Here to Open This Script 

2015/09/02 Bundle IDの取得

Cocoaの機能を用いて、実行中のプログラム(path to meで取得されるもの)のBundle IDを取得するAppleScriptです。

AppleScript名:ASOCでBundle関連のじっけん
– Created 2015-09-01 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

set mePath to POSIX path of (path to me)

set myBundle to current application’s NSBundle’s bundleWithPath:mePath
set aBundle to myBundle’s bundleIdentifier() as text
–> “jp.piyomarusoft.xxxxxxx”

★Click Here to Open This Script 

2015/01/22 指定したアプリケーションの各lproj内の各stringsファイルのパス一覧を取得する

指定したアプリケーションの各言語のlprojフォルダ内にある、各stringsファイルへのパスを取得するAppleScriptです。

各アプリのローカライズ情報を取り出すプログラムはいろいろと試してはいるものの、作り方とかローカライズ方法が全然違うアプリケーションが紛れていることがあり、イレギュラーを除去すべきなのかも(Apple純正でもSafariあたりは全然違うし)。

AppleScript名:指定したアプリケーションの各lproj内の各stringsファイルのパス一覧を取得する
– Created 2015-01-22 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "ASObjCExtras"

set apPath1 to (path to applications folder) –dialog’s default directory
set anAlias to choose file of type "com.apple.application-bundle" default location apPath1 with prompt "ローカライズ情報を取得するアプリケーションを選択"
set aRes to getStringsPathWithinEachLanguageInApp(anAlias)
–> {{langStr:"English", stringsPath:{"/Applications/mi.app/Contents/Resources/English.lproj/Localizable.strings"}}, {langStr:"Japanese", stringsPath:{"/Applications/mi.app/Contents/Resources/Japanese.lproj/Localizable.strings"}}}

–指定したアプリケーションの各lproj内の各stringsファイルのパス一覧を取得する
on getStringsPathWithinEachLanguageInApp(anAlias)
  –アプリケーションのローカリゼーションを取得する(言語の重複を許容)
  
set locList to getSpecifiedAppFilesLocalizationListWithDuplication(anAlias) of me
  
  
set lprojList to {}
  
  
repeat with ii in locList
    
    
set jj to contents of ii
    
    
set appPath to POSIX path of anAlias
    
set appBundle to (current application’s NSBundle’s bundleWithPath:appPath)
    
set resPath to ((appBundle’s pathForResource:jj ofType:"lproj") as string) & "/"
    
    
if resPath is not equal to missing value then
      
      
set aLangBundle to (current application’s NSBundle’s bundleWithPath:resPath)
      
if aLangBundle is not equal to missing value then
        
        
set stringsList to (aLangBundle’s pathsForResourcesOfType:"strings" inDirectory:"") as list
        
        
if stringsList is not equal to {} then
          set the end of lprojList to {langStr:jj, stringsPath:stringsList}
        end if
        
      end if
    end if
    
  end repeat
  
  
return lprojList
  
end getStringsPathWithinEachLanguageInApp

–指定アプリケーションファイルの、指定Localeにおけるローカライズ言語リストを求める。重複を許容
on getSpecifiedAppFilesLocalizationListWithDuplication(anAppAlias as alias)
  
  
set aURL to (current application’s SMSFord’s URLFrom:anAppAlias)
  
set aBundle to current application’s NSBundle’s bundleWithURL:aURL
  
  
set theNSLocale to current application’s NSLocale’s localeWithLocaleIdentifier:"en" –Output Locale Name in English (en)
  
  
–Get Localization Info
  
set locList to aBundle’s localizations()
  
–> {"de", "English", "fr", "French", "German", "ja", "Japanese"}
  
  
return locList as list
  
end getSpecifiedAppFilesLocalizationListWithDuplication

★Click Here to Open This Script 

2014/12/25 アプリケーションごとのローカライズ言語数を求める

/Applicationsフォルダ以下の全アプリケーションのローカライズ言語数を求めて多い順にソートして返すAppleScriptです。

便利な部品ができてきたので、本来の用途以外に転用するのも簡単になってきました。

ただ、こういう「調査活動」以外にこの処理が生きるかどうかは未知数です、、、

OS X自体のローカライズ言語数=35ということが見て取れますが(ただし、対応言語は幾つかのレベルに分けられており、ユーザー数によって対応度が違うはず)、それ以上のローカライズ言語を持っているアプリケーションの存在が気になります。

AppleScript名:アプリケーションごとのローカライズ言語数を求める
– Created 2014-12-25 by Takaaki Naganoya

use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”

–/Applications以下のアプリケーションのパスをすべて求める
set apPath1 to (path to applications folder) as string
set aList to getFileListWithSpotLight(“kMDItemContentType”, “com.apple.application-bundle”, apPath1) of me

–各アプリケーションのローカリゼーション情報を取得(重複情報はカウントしつつ保持)
set aaList to {}

repeat with i in aList
  set j to contents of i
  
  
–アプリケーションのローカリゼーションを取得する(重複言語の除去ずみ)
  
set locList to getSpecifiedAppFilesLocalizationList(j) of me
  
  
tell application “System Events”
    set aName to name of j
  end tell
  
  
set the end of aaList to {aName, length of locList}
  
end repeat

–Sort Result (Many->Little)
set sortIndexes to {1} –Key Item id: begin from 0
set sortOrders to {false} –Descending Sort Order
set sortTypes to {“compare:”}
set rList to (current application’s SMSFord’s subarraysIn:aaList sortedByIndexes:sortIndexes ascending:sortOrders sortTypes:sortTypes |error|:(missing value)) as list

return rList
–> {{”VLC.app”, 93}, {”Creative Cloud.app”, 61}, {”Adobe Application Manager.app”, 61}, {”Google Drive.app”, 53}, {”Google Chrome.app”, 53}, {”Uninstall Product.app”, 43}, {”Uninstall Product.app”, 43}, {”Google Earth.app”, 42}, {”VueScan.app”, 40}, {”Evernote.app”, 40}, {”Adobe Muse CC 2014.app”, 37}, {”iTunes.app”, 35}, {”Digital Color Meter.app”, 35}, {”Image Capture.app”, 35}, {”Grab.app”, 35}, {”Game Center.app”, 35}, {”iBooks.app”, 35}, {”Boot Camp Assistant.app”, 35}, {”Bluetooth File Exchange.app”, 35}, {”System Information.app”, 35}, {”Safari.app”, 35}, {”Terminal.app”, 35}, {”Contacts.app”, 35}, {”Preview.app”, 35}, {”App Store.app”, 35}, {”Automator.app”, 35}, ……

–指定アプリケーションファイルの、指定Localeにおけるローカライズ言語リストを求める。重複を排除
on getSpecifiedAppFilesLocalizationList(anAppAlias as alias)
  
  
set aURL to (current application’s SMSFord’s URLFrom:anAppAlias)
  
set aBundle to current application’s NSBundle’s bundleWithURL:aURL
  
  
set theNSLocale to current application’s NSLocale’s localeWithLocaleIdentifier:“en” –Output Locale Name in English (en)
  
  
–Get Localization Info
  
set locList to aBundle’s localizations()
  
–> {”de”, “English”, “fr”, “French”, “German”, “ja”, “Japanese”}
  
set theEnumerator to locList’s objectEnumerator()
  
  
set theSet to current application’s NSMutableSet’s |set|()
  
  
repeat
    set aValue to theEnumerator’s nextObject()
    
if aValue is missing value then exit repeat
    
    
set theName to (theNSLocale’s displayNameForKey:(current application’s NSLocaleIdentifier) value:aValue)
    
    
if theName is missing value then
      –”English”や”French”などは、missing valueが返ってくるので、”English”や”French”などをそのまま追加
      
theSet’s addObject:aValue
    else
      –”de”や”fr”などは、”German”や”French”が返ってくるので、それを追加
      
theSet’s addObject:theName
    end if
  end repeat
  
  
–NSCountedSetをNSMutableArrayに変換
  
set theArray to current application’s NSMutableArray’s array()
  
set theEnumerator to theSet’s objectEnumerator()
  
  
–NSMutableArrayから順次取り出して、NSArrayに出力
  
repeat
    set aValue to theEnumerator’s nextObject()
    
if aValue is missing value then exit repeat
    
theArray’s addObject:aValue
  end repeat
  
  
return theArray as list
  
end getSpecifiedAppFilesLocalizationList

–指定階層下で、指定メタデータが指定パラメータであるファイルを取得(alias list)
on getFileListWithSpotLight(aMetaDataItem as string, aParam as string, startDir as {alias, string})
  set sDirText to quoted form of POSIX path of startDir
  
set shellText to “/usr/bin/mdfind ’” & aMetaDataItem & ” == \”" & aParam & “\”’ -onlyin “ & sDirText
  
try
    set aRes to do shell script shellText
  on error
    return {}
  end try
  
set pList to paragraphs of aRes
  
set aList to {}
  
repeat with i in pList
    set aPath to POSIX file i
    
set aPath to aPath as alias
    
set the end of aList to aPath
  end repeat
  
return aList
end getFileListWithSpotLight

★Click Here to Open This Script 

2014/12/25 アプリケーションのローカライズ分布を取得する v5

/Applicationsフォルダ内に入っているアプリケーションのローカライズ言語の度数分布を取得するAppleScriptのバージョンアップ版です。

1つのアプリケーションが複数の重複するローカライズ情報を持っているケース({”de”, “English”, “fr”, “French”, “German”, “ja”, “Japanese”})があり、1つのアプリケーションから情報を取得する段階で重複を除去するルーチンを作成。重複を排除して({”German”, “English”, “French”,”Japanese”})のように整理して取得するように変更しました。

また、/Applicationsフォルダの直下のアプリケーションのファイルしか取得していなかったので、mdfindで/Applicationsフォルダ以下すべてのアプリケーションを取得するように変更。

また、言語名ではなく出現頻度でソートするように変更しました。

途中で、mdfindでアプリケーションのファイルを抽出する処理を試していたら、サブフォルダ内のアプリケーションが出てこないという問題があり・・・開発マシンのSSD内のspotlightの検索辞書が壊れていたようで、再生成したら正常に動作するようになりました。mdfindの処理の落とし穴です。

マイナー言語(失礼!)をこまかくフォローしているのはGoogleアプリかと思っていたら、VLCでした。OSがサポートしていない言語用のローカライズを行っても意味はないのですが、ムービーの字幕表示などの関係でしょうか?

AppleScript名:アプリケーションのローカライズ分布を取得する v5
– Created 2014-12-23 by Takaaki Naganoya
– Changed 2014-12-24 by Shane Stanley
– Changed 2014-12-25 by Takaaki Naganoya

use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”

–mdfindで指定フォルダ以下にあるすべてのアプリケーションファイルを取得
set apPath1 to (path to applications folder) as string
set apList to getFileListWithSpotLight(“kMDItemContentType”, “com.apple.application-bundle”, apPath1) of me

set theCountedSet to current application’s NSCountedSet’s |set|()

–各アプリケーションのローカリゼーション情報を取得する
repeat with i in apList
  set j to contents of i
  
  
–指定アプリケーションのローカライズ言語を取得。重複を除去
  
set locList to getSpecifiedAppFilesLocalizationList(j) of me
  
–> {”de”, “English”, “fr”, “French”, “German”, “ja”, “Japanese”}
  
—-> {”German”, “English”, “French”,”Japanese”}
  
  
set locArray to (current application’s SMSFord’s Cocoaify:locList)
  
  
set theEnumerator to locArray’s objectEnumerator()
  
repeat
    set aValue to theEnumerator’s nextObject()
    
if aValue is missing value then exit repeat
    
    (
theCountedSet’s addObject:aValue)
  end repeat
end repeat

–NSCountedSetをNSMutableArrayに変換
set theArray to current application’s NSMutableArray’s array()
set theEnumerator to theCountedSet’s objectEnumerator()

–NSMutableArrayから順次取り出して、NSArrayに対してDictionaryを追加
repeat
  set aValue to theEnumerator’s nextObject()
  
if aValue is missing value then exit repeat
  
theArray’s addObject:(current application’s NSDictionary’s dictionaryWithObjects:{aValue, (theCountedSet’s countForObject:aValue)} forKeys:{“theName”, “numberOfTimes”})
end repeat

–出現回数(numberOfTimes)で降順ソート
set theDesc to current application’s NSSortDescriptor’s sortDescriptorWithKey:“numberOfTimes” ascending:false
theArray’s sortUsingDescriptors:{theDesc}

return theArray as list

–> {{theName:”English”, numberOfTimes:733}, {theName:”Japanese”, numberOfTimes:404}, {theName:”French”, numberOfTimes:266}, {theName:”German”, numberOfTimes:265}, {theName:”Italian”, numberOfTimes:219}, {theName:”Spanish”, numberOfTimes:203}, {theName:”Dutch”, numberOfTimes:165}, {theName:”Chinese (China)”, numberOfTimes:149}, {theName:”Swedish”, numberOfTimes:133}, {theName:”Korean”, numberOfTimes:131}, {theName:”Russian”, numberOfTimes:129}, {theName:”Chinese (Taiwan)”, numberOfTimes:127}, {theName:”Portuguese”, numberOfTimes:124}, {theName:”Polish”, numberOfTimes:118}, {theName:”Danish”, numberOfTimes:117}, {theName:”Finnish”, numberOfTimes:110}, {theName:”Norwegian”, numberOfTimes:100}, {theName:”Czech”, numberOfTimes:92}, {theName:”Turkish”, numberOfTimes:90}, {theName:”Hungarian”, numberOfTimes:87}, {theName:”Greek”, numberOfTimes:76}, {theName:”Ukrainian”, numberOfTimes:75}, {theName:”Romanian”, numberOfTimes:74}, {theName:”Portuguese (Portugal)”, numberOfTimes:70}, {theName:”Slovak”, numberOfTimes:69}, {theName:”Catalan”, numberOfTimes:63}, {theName:”Croatian”, numberOfTimes:63}, {theName:”Arabic”, numberOfTimes:61}, {theName:”Thai”, numberOfTimes:61}, {theName:”Hebrew”, numberOfTimes:60}, {theName:”Indonesian”, numberOfTimes:58}, {theName:”Vietnamese”, numberOfTimes:57}, {theName:”Malay”, numberOfTimes:56}, {theName:”Spanish (Mexico)”, numberOfTimes:49}, {theName:”Base”, numberOfTimes:46}, {theName:”Portuguese (Brazil)”, numberOfTimes:22}, {theName:”Norwegian Bokmål”, numberOfTimes:21}, {theName:”Chinese (Simplified)”, numberOfTimes:19}, {theName:”Chinese”, numberOfTimes:14}, {theName:”English (United Kingdom)”, numberOfTimes:12}, {theName:”Bulgarian”, numberOfTimes:12}, {theName:”Chinese (Traditional)”, numberOfTimes:11}, {theName:”Latvian”, numberOfTimes:10}, {theName:”Lithuanian”, numberOfTimes:9}, {theName:”Slovenian”, numberOfTimes:9}, {theName:”Japanese (Japan)”, numberOfTimes:9}, {theName:”Czech (Czech Republic)”, numberOfTimes:9}, {theName:”Estonian”, numberOfTimes:8}, {theName:”Ukrainian (Ukraine)”, numberOfTimes:8}, {theName:”Spanish (Spain)”, numberOfTimes:7}, {theName:”Turkish (Turkey)”, numberOfTimes:7}, {theName:”Hungarian (Hungary)”, numberOfTimes:6}, {theName:”Danish (Denmark)”, numberOfTimes:6}, {theName:”Russian (Russia)”, numberOfTimes:6}, {theName:”French (XM)”, numberOfTimes:6}, {theName:”Swedish (Sweden)”, numberOfTimes:6}, {theName:”Serbian”, numberOfTimes:6}, {theName:”Norwegian Bokmål (Norway)”, numberOfTimes:6}, {theName:”Dutch (Netherlands)”, numberOfTimes:6}, {theName:”Persian”, numberOfTimes:5}, {theName:”Korean (South Korea)”, numberOfTimes:5}, {theName:”Finnish (Finland)”, numberOfTimes:5}, {theName:”Italian (Italy)”, numberOfTimes:5}, {theName:”German (Germany)”, numberOfTimes:5}, {theName:”French (Canada)”, numberOfTimes:5}, {theName:”French (France)”, numberOfTimes:5}, {theName:”Polish (Poland)”, numberOfTimes:5}, {theName:”Hindi”, numberOfTimes:5}, {theName:”English (United States)”, numberOfTimes:5}, {theName:”Hebrew (Israel)”, numberOfTimes:4}, {theName:”Arabic (United Arab Emirates)”, numberOfTimes:4}, {theName:”Spanish (Latin America)”, numberOfTimes:4}, {theName:”Romanian (Romania)”, numberOfTimes:4}, {theName:”Spanish (Namibia)”, numberOfTimes:4}, {theName:”Filipino”, numberOfTimes:4}, {theName:”Spanish (Laos)”, numberOfTimes:4}, {theName:”empty”, numberOfTimes:4}, {theName:”Greek (Greece)”, numberOfTimes:4}, {theName:”Telugu”, numberOfTimes:3}, {theName:”Malayalam”, numberOfTimes:3}, {theName:”Icelandic”, numberOfTimes:3}, {theName:”Kannada”, numberOfTimes:3}, {theName:”Galician”, numberOfTimes:3}, {theName:”Bengali”, numberOfTimes:3}, {theName:”Gujarati”, numberOfTimes:3}, {theName:”Marathi”, numberOfTimes:3}, {theName:”Tamil”, numberOfTimes:3}, {theName:”Belarusian”, numberOfTimes:3}, {theName:”Albanian”, numberOfTimes:2}, {theName:”Mongolian”, numberOfTimes:2}, {theName:”French (Morocco)”, numberOfTimes:2}, {theName:”Georgian”, numberOfTimes:2}, {theName:”Sinhala”, numberOfTimes:2}, {theName:”jp”, numberOfTimes:2}, {theName:”ua”, numberOfTimes:2}, {theName:”Amharic”, numberOfTimes:2}, {theName:”English (United Arab Emirates)”, numberOfTimes:2}, {theName:”Finnish (FL)”, numberOfTimes:2}, {theName:”Catalan (Spain)”, numberOfTimes:2}, {theName:”Basque”, numberOfTimes:2}, {theName:”English (Israel)”, numberOfTimes:2}, {theName:”Norwegian (NB)”, numberOfTimes:2}, {theName:”Oriya”, numberOfTimes:1}, {theName:”Punjabi”, numberOfTimes:1}, {theName:”Serbo-Croatian”, numberOfTimes:1}, {theName:”Walloon”, numberOfTimes:1}, {theName:”Corsican”, numberOfTimes:1}, {theName:”Sorani Kurdish”, numberOfTimes:1}, {theName:”Chinese (Hong Kong SAR China)”, numberOfTimes:1}, {theName:”Breton”, numberOfTimes:1}, {theName:”Cornish”, numberOfTimes:1}, {theName:”Chiga”, numberOfTimes:1}, {theName:”Aragonese”, numberOfTimes:1}, {theName:”Macedonian”, numberOfTimes:1}, {theName:”Japan”, numberOfTimes:1}, {theName:”Bosnian”, numberOfTimes:1}, {theName:”Ganda”, numberOfTimes:1}, {theName:”Tetum”, numberOfTimes:1}, {theName:”Welsh”, numberOfTimes:1}, {theName:”Armenian”, numberOfTimes:1}, {theName:”Kyrgyz”, numberOfTimes:1}, {theName:”Scottish Gaelic”, numberOfTimes:1}, {theName:”Afrikaans”, numberOfTimes:1}, {theName:”Zulu”, numberOfTimes:1}, {theName:”Swahili”, numberOfTimes:1}, {theName:”Burmese”, numberOfTimes:1}, {theName:”Chinese (Traditional, Taiwan)”, numberOfTimes:1}, {theName:”ct”, numberOfTimes:1}, {theName:”Tagalog”, numberOfTimes:1}, {theName:”Kazakh”, numberOfTimes:1}, {theName:”Bengali (India)”, numberOfTimes:1}, {theName:”Pashto”, numberOfTimes:1}, {theName:”Nepali”, numberOfTimes:1}, {theName:”Acoli”, numberOfTimes:1}, {theName:”Friulian”, numberOfTimes:1}, {theName:”Norwegian Nynorsk”, numberOfTimes:1}, {theName:”Asturian”, numberOfTimes:1}, {theName:”Thai (Thailand)”, numberOfTimes:1}, {theName:”Khmer”, numberOfTimes:1}, {theName:”Irish”, numberOfTimes:1}, {theName:”American”, numberOfTimes:1}, {theName:”Fulah”, numberOfTimes:1}, {theName:”Chinese (Traditional, Hong Kong SAR China)”, numberOfTimes:1}, {theName:”Occitan”, numberOfTimes:1}, {theName:”Interlingua”, numberOfTimes:1}, {theName:”Uzbek”, numberOfTimes:1}, {theName:”Azerbaijani”, numberOfTimes:1}}

–指定アプリケーションファイルの、指定Localeにおけるローカライズ言語リストを求める。重複を排除
on getSpecifiedAppFilesLocalizationList(anAppAlias as alias)
  
  
set aURL to (current application’s SMSFord’s URLFrom:anAppAlias)
  
set aBundle to current application’s NSBundle’s bundleWithURL:aURL
  
  
set theNSLocale to current application’s NSLocale’s localeWithLocaleIdentifier:“en” –Output Locale Name in English (en)
  
  
–Get Localization Info
  
set locList to aBundle’s localizations()
  
–> {”de”, “English”, “fr”, “French”, “German”, “ja”, “Japanese”}
  
set theEnumerator to locList’s objectEnumerator()
  
  
set theSet to current application’s NSMutableSet’s |set|()
  
  
repeat
    set aValue to theEnumerator’s nextObject()
    
if aValue is missing value then exit repeat
    
    
set theName to (theNSLocale’s displayNameForKey:(current application’s NSLocaleIdentifier) value:aValue)
    
    
if theName is missing value then
      –”English”や”French”などは、missing valueが返ってくるので、”English”や”French”などをそのまま追加
      
theSet’s addObject:aValue
    else
      –”de”や”fr”などは、”German”や”French”が返ってくるので、それを追加
      
theSet’s addObject:theName
    end if
  end repeat
  
  
–NSCountedSetをNSMutableArrayに変換
  
set theArray to current application’s NSMutableArray’s array()
  
set theEnumerator to theSet’s objectEnumerator()
  
  
–NSMutableArrayから順次取り出して、NSArrayに対してDictionaryを追加
  
repeat
    set aValue to theEnumerator’s nextObject()
    
if aValue is missing value then exit repeat
    
theArray’s addObject:aValue
  end repeat
  
  
return theArray as list
  
end getSpecifiedAppFilesLocalizationList

–指定階層下で、指定メタデータが指定パラメータであるファイルを取得(alias list)
on getFileListWithSpotLight(aMetaDataItem as string, aParam as string, startDir as {alias, string})
  set sDirText to quoted form of POSIX path of startDir
  
set shellText to “/usr/bin/mdfind ’” & aMetaDataItem & ” == \”" & aParam & “\”’ -onlyin “ & sDirText
  
try
    set aRes to do shell script shellText
  on error
    return {}
  end try
  
set pList to paragraphs of aRes
  
set aList to {}
  
repeat with i in pList
    set aPath to POSIX file i
    
set aPath to aPath as alias
    
set the end of aList to aPath
  end repeat
  
return aList
end getFileListWithSpotLight

★Click Here to Open This Script 

2014/12/24 アプリケーションのローカライズ分布を取得する v4

/Applicationsフォルダ内に入っているアプリケーションのローカライズ言語の度数分布を取得するAppleScriptのバージョンアップ版です。

Shane Stanleyからのクリスマスプレゼントともいえるもので、実に処理内容がクールです。いや、これはいい。

v3で言語表記の「ゆらぎ」をカバーしようとしましたが、このv4はOSのサービスを用いて、”jp”や”fr”といった短縮表記からフルネームの”Japanese”や”French”といった名称を展開し、そのうえで重複を排除しています。

AppleScript名:アプリケーションのローカライズ分布を取得する v4
– v1 Created 2014-12-24 By Takaaki Naganoya
– v2 Changed 2014-12-24 by Shane Stanley
– v3 Changed 2014-12-24 by Takaaki Naganoya
– v4 Changed 2014-12-24 by Shane Stanley

use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”

set apPath to (path to applications folder) as string

tell application “Finder”
  tell folder apPath
    set aList to (every application file) as alias list
  end tell
end tell

set theCountedSet to current application’s NSCountedSet’s |set|()
set theNSLocale to current application’s NSLocale’s localeWithLocaleIdentifier:“en” –Output Locale Name in English (en)
–set theNSLocale to current application’s NSLocale’s currentLocale() –Output Locale Name in current Locale Language (ex. Japanese)

repeat with i in aList
  set j to contents of i
  
set aURL to (current application’s SMSFord’s URLFrom:j)
  
set aBundle to (current application’s NSBundle’s bundleWithURL:aURL)
  
  
–アプリケーションのローカリゼーションを取得する
  
set locList to aBundle’s localizations()
  
set theEnumerator to locList’s objectEnumerator()
  
repeat
    set aValue to theEnumerator’s nextObject()
    
if aValue is missing value then exit repeat
    
set theName to (theNSLocale’s displayNameForKey:(current application’s NSLocaleIdentifier) value:aValue)
    
if theName is missing value then
      (theCountedSet’s addObject:aValue)
    else
      (theCountedSet’s addObject:theName)
    end if
  end repeat
  
  
log {“theCountedSet”, theCountedSet} –ASObjC Explorer 4だとCocoaのObjectも読める形式でログ出力可能
  
end repeat

–NSCountedSetをNSMutableDictionaryに変換してからASのrecordに変換
set theArray to current application’s NSMutableArray’s array()
set theEnumerator to theCountedSet’s objectEnumerator()
repeat
  set aValue to theEnumerator’s nextObject()
  
if aValue is missing value then exit repeat
  
theArray’s addObject:(current application’s NSDictionary’s dictionaryWithObjects:{aValue, (theCountedSet’s countForObject:aValue)} forKeys:{“theName”, “numberOfTimes”})
end repeat
set theDesc to current application’s NSSortDescriptor’s sortDescriptorWithKey:“theName” ascending:true
theArray’s sortUsingDescriptors:{theDesc}

return theArray as list
–> {{theName:”Acoli”, numberOfTimes:1}, {theName:”Afrikaans”, numberOfTimes:1}, {theName:”Albanian”, numberOfTimes:1}, {theName:”American”, numberOfTimes:1}, {theName:”Amharic”, numberOfTimes:2}, {theName:”Arabic”, numberOfTimes:36}, {theName:”Aragonese”, numberOfTimes:1}, {theName:”Armenian”, numberOfTimes:1}, {theName:”Asturian”, numberOfTimes:1}, {theName:”Azerbaijani”, numberOfTimes:1}, {theName:”Base”, numberOfTimes:30}, {theName:”Basque”, numberOfTimes:2}, {theName:”Belarusian”, numberOfTimes:2}, {theName:”Bengali”, numberOfTimes:3}, {theName:”Bengali (India)”, numberOfTimes:1}, {theName:”Bosnian”, numberOfTimes:1}, {theName:”Breton”, numberOfTimes:1}, {theName:”Bulgarian”, numberOfTimes:8}, {theName:”Burmese”, numberOfTimes:1}, {theName:”Catalan”, numberOfTimes:39}, {theName:”Chiga”, numberOfTimes:1}, {theName:”Chinese”, numberOfTimes:8}, {theName:”Chinese (China)”, numberOfTimes:69}, {theName:”Chinese (Hong Kong SAR China)”, numberOfTimes:1}, {theName:”Chinese (Simplified)”, numberOfTimes:14}, {theName:”Chinese (Taiwan)”, numberOfTimes:64}, {theName:”Chinese (Traditional)”, numberOfTimes:8}, {theName:”Chinese (Traditional, Hong Kong SAR China)”, numberOfTimes:1}, {theName:”Cornish”, numberOfTimes:1}, {theName:”Corsican”, numberOfTimes:1}, {theName:”Croatian”, numberOfTimes:40}, {theName:”Czech”, numberOfTimes:48}, {theName:”Danish”, numberOfTimes:62}, {theName:”Danish (Denmark)”, numberOfTimes:1}, {theName:”Dutch”, numberOfTimes:77}, {theName:”Dutch (Netherlands)”, numberOfTimes:1}, {theName:”English”, numberOfTimes:307}, {theName:”English (United Kingdom)”, numberOfTimes:7}, {theName:”English (United States)”, numberOfTimes:1}, {theName:”Estonian”, numberOfTimes:6}, {theName:”Filipino”, numberOfTimes:4}, {theName:”Finnish”, numberOfTimes:56}, {theName:”French”, numberOfTimes:130}, {theName:”Friulian”, numberOfTimes:1}, {theName:”Fulah”, numberOfTimes:1}, {theName:”Galician”, numberOfTimes:2}, {theName:”Ganda”, numberOfTimes:1}, {theName:”Georgian”, numberOfTimes:2}, {theName:”German”, numberOfTimes:135}, {theName:”Greek”, numberOfTimes:43}, {theName:”Gujarati”, numberOfTimes:3}, {theName:”Hebrew”, numberOfTimes:36}, {theName:”Hindi”, numberOfTimes:5}, {theName:”Hungarian”, numberOfTimes:44}, {theName:”Icelandic”, numberOfTimes:3}, {theName:”Indonesian”, numberOfTimes:38}, {theName:”Interlingua”, numberOfTimes:1}, {theName:”Irish”, numberOfTimes:1}, {theName:”Italian”, numberOfTimes:98}, {theName:”Japanese”, numberOfTimes:147}, {theName:”Japanese (Japan)”, numberOfTimes:1}, {theName:”Kannada”, numberOfTimes:3}, {theName:”Kazakh”, numberOfTimes:1}, {theName:”Khmer”, numberOfTimes:1}, {theName:”Korean”, numberOfTimes:67}, {theName:”Kyrgyz”, numberOfTimes:1}, {theName:”Latvian”, numberOfTimes:7}, {theName:”Lithuanian”, numberOfTimes:6}, {theName:”Macedonian”, numberOfTimes:1}, {theName:”Malay”, numberOfTimes:36}, {theName:”Malayalam”, numberOfTimes:3}, {theName:”Marathi”, numberOfTimes:3}, {theName:”Mongolian”, numberOfTimes:2}, {theName:”Nepali”, numberOfTimes:1}, {theName:”Norwegian”, numberOfTimes:49}, {theName:”Norwegian Bokmål”, numberOfTimes:10}, {theName:”Norwegian Bokmål (Norway)”, numberOfTimes:1}, {theName:”Norwegian Nynorsk”, numberOfTimes:1}, {theName:”Occitan”, numberOfTimes:1}, {theName:”Oriya”, numberOfTimes:1}, {theName:”Pashto”, numberOfTimes:1}, {theName:”Persian”, numberOfTimes:4}, {theName:”Polish”, numberOfTimes:60}, {theName:”Portuguese”, numberOfTimes:61}, {theName:”Portuguese (Brazil)”, numberOfTimes:12}, {theName:”Portuguese (Portugal)”, numberOfTimes:44}, {theName:”Punjabi”, numberOfTimes:1}, {theName:”Romanian”, numberOfTimes:42}, {theName:”Russian”, numberOfTimes:70}, {theName:”Russian (Russia)”, numberOfTimes:1}, {theName:”Scottish Gaelic”, numberOfTimes:1}, {theName:”Serbian”, numberOfTimes:5}, {theName:”Serbo-Croatian”, numberOfTimes:1}, {theName:”Sinhala”, numberOfTimes:2}, {theName:”Slovak”, numberOfTimes:42}, {theName:”Slovenian”, numberOfTimes:7}, {theName:”Sorani Kurdish”, numberOfTimes:1}, {theName:”Spanish”, numberOfTimes:95}, {theName:”Spanish (Latin America)”, numberOfTimes:2}, {theName:”Spanish (Mexico)”, numberOfTimes:28}, {theName:”Spanish (Spain)”, numberOfTimes:2}, {theName:”Swahili”, numberOfTimes:1}, {theName:”Swedish”, numberOfTimes:64}, {theName:”Swedish (Sweden)”, numberOfTimes:1}, {theName:”Tagalog”, numberOfTimes:1}, {theName:”Tamil”, numberOfTimes:3}, {theName:”Telugu”, numberOfTimes:3}, {theName:”Tetum”, numberOfTimes:1}, {theName:”Thai”, numberOfTimes:40}, {theName:”Thai (Thailand)”, numberOfTimes:1}, {theName:”Turkish”, numberOfTimes:46}, {theName:”Ukrainian”, numberOfTimes:39}, {theName:”Ukrainian (Ukraine)”, numberOfTimes:2}, {theName:”Uzbek”, numberOfTimes:1}, {theName:”Vietnamese”, numberOfTimes:37}, {theName:”Walloon”, numberOfTimes:1}, {theName:”Welsh”, numberOfTimes:1}, {theName:”Zulu”, numberOfTimes:1}, {theName:”empty”, numberOfTimes:3}}

–> {{theName:”American”, numberOfTimes:1}, {theName:”Base”, numberOfTimes:30}, {theName:”Chinese”, numberOfTimes:2}, {theName:”Czech”, numberOfTimes:1}, {theName:”Danish”, numberOfTimes:4}, {theName:”Dutch”, numberOfTimes:38}, {theName:”English”, numberOfTimes:212}, {theName:”Finnish”, numberOfTimes:1}, {theName:”French”, numberOfTimes:67}, {theName:”German”, numberOfTimes:72}, {theName:”Greek”, numberOfTimes:1}, {theName:”Hungarian”, numberOfTimes:1}, {theName:”Italian”, numberOfTimes:50}, {theName:”Japanese”, numberOfTimes:80}, {theName:”Korean”, numberOfTimes:3}, {theName:”Polish”, numberOfTimes:3}, {theName:”Portuguese”, numberOfTimes:2}, {theName:”Russian”, numberOfTimes:3}, {theName:”Spanish”, numberOfTimes:45}, {theName:”Swedish”, numberOfTimes:5}, {theName:”empty”, numberOfTimes:3}, {theName:”アイスランド語”, numberOfTimes:3}, {theName:”アイルランド語”, numberOfTimes:1}, {theName:”アストゥリアス語”, numberOfTimes:1}, {theName:”アゼルバイジャン語”, numberOfTimes:1}, {theName:”アチョリ語”, numberOfTimes:1}, {theName:”アフリカーンス語”, numberOfTimes:1}, {theName:”アムハラ語”, numberOfTimes:2}, {theName:”アラゴン語”, numberOfTimes:1}, {theName:”アラビア語”, numberOfTimes:36}, {theName:”アルバニア語”, numberOfTimes:1}, {theName:”アルメニア語”, numberOfTimes:1}, {theName:”イタリア語”, numberOfTimes:48}, {theName:”インターリングア”, numberOfTimes:1}, {theName:”インドネシア語”, numberOfTimes:38}, {theName:”ウェールズ語”, numberOfTimes:1}, {theName:”ウクライナ語”, numberOfTimes:39}, {theName:”ウクライナ語 (ウクライナ)”, numberOfTimes:2}, {theName:”ウズベク語”, numberOfTimes:1}, {theName:”エストニア語”, numberOfTimes:6}, {theName:”オック語”, numberOfTimes:1}, {theName:”オランダ語”, numberOfTimes:39}, {theName:”オランダ語 (オランダ)”, numberOfTimes:1}, {theName:”オリヤー語”, numberOfTimes:1}, {theName:”ガリシア語”, numberOfTimes:2}, {theName:”ガンダ語”, numberOfTimes:1}, {theName:”カザフ語”, numberOfTimes:1}, {theName:”カタロニア語”, numberOfTimes:39}, {theName:”カンナダ語”, numberOfTimes:3}, {theName:”ギリシャ語”, numberOfTimes:42}, {theName:”キルギス語”, numberOfTimes:1}, {theName:”グジャラート語”, numberOfTimes:3}, {theName:”グルジア語”, numberOfTimes:2}, {theName:”クメール語”, numberOfTimes:1}, {theName:”クルド語(ソラニー)”, numberOfTimes:1}, {theName:”クロアチア語”, numberOfTimes:40}, {theName:”コルシカ語”, numberOfTimes:1}, {theName:”コーンウォール語”, numberOfTimes:1}, {theName:”シンハラ語”, numberOfTimes:2}, {theName:”ズールー語”, numberOfTimes:1}, {theName:”スウェーデン語”, numberOfTimes:59}, {theName:”スウェーデン語 (スウェーデン)”, numberOfTimes:1}, {theName:”スコットランド・ゲール語”, numberOfTimes:1}, {theName:”スペイン語”, numberOfTimes:50}, {theName:”スペイン語 (スペイン)”, numberOfTimes:2}, {theName:”スペイン語 (メキシコ)”, numberOfTimes:28}, {theName:”スペイン語 (ラテンアメリカ)”, numberOfTimes:2}, {theName:”スロバキア語”, numberOfTimes:42}, {theName:”スロベニア語”, numberOfTimes:7}, {theName:”スワヒリ語”, numberOfTimes:1}, {theName:”セルビア語”, numberOfTimes:5}, {theName:”セルボ・クロアチア語”, numberOfTimes:1}, {theName:”タイ語”, numberOfTimes:40}, {theName:”タイ語 (タイ)”, numberOfTimes:1}, {theName:”タガログ語”, numberOfTimes:1}, {theName:”タミル語”, numberOfTimes:3}, {theName:”チェコ語”, numberOfTimes:47}, {theName:”チガ語”, numberOfTimes:1}, {theName:”デンマーク語”, numberOfTimes:58}, {theName:”デンマーク語 (デンマーク)”, numberOfTimes:1}, {theName:”テトゥン語”, numberOfTimes:1}, {theName:”テルグ語”, numberOfTimes:3}, {theName:”ドイツ語”, numberOfTimes:63}, {theName:”トルコ語”, numberOfTimes:46}, {theName:”ネパール語”, numberOfTimes:1}, {theName:”ノルウェー語”, numberOfTimes:49}, {theName:”ノルウェー語(ニーノシュク)”, numberOfTimes:1}, {theName:”ノルウェー語(ブークモール)”, numberOfTimes:10}, {theName:”ノルウェー語(ブークモール) (ノルウェー)”, numberOfTimes:1}, {theName:”バスク語”, numberOfTimes:2}, {theName:”パシュトゥー語”, numberOfTimes:1}, {theName:”パンジャブ語”, numberOfTimes:1}, {theName:”ハンガリー語”, numberOfTimes:43}, {theName:”ビルマ語”, numberOfTimes:1}, {theName:”ヒンディー語”, numberOfTimes:5}, {theName:”ブルガリア語”, numberOfTimes:8}, {theName:”ブルトン語”, numberOfTimes:1}, {theName:”フィリピノ語”, numberOfTimes:4}, {theName:”フィンランド語”, numberOfTimes:55}, {theName:”フラニ語”, numberOfTimes:1}, {theName:”フランス語”, numberOfTimes:63}, {theName:”フリウリ語”, numberOfTimes:1}, {theName:”ベトナム語”, numberOfTimes:37}, {theName:”ベラルーシ語”, numberOfTimes:2}, {theName:”ベンガル語”, numberOfTimes:3}, {theName:”ベンガル語 (インド)”, numberOfTimes:1}, {theName:”ヘブライ語”, numberOfTimes:36}, {theName:”ペルシア語”, numberOfTimes:4}, {theName:”ボスニア語”, numberOfTimes:1}, {theName:”ポルトガル語”, numberOfTimes:59}, {theName:”ポルトガル語 (ブラジル)”, numberOfTimes:12}, {theName:”ポルトガル語 (ポルトガル)”, numberOfTimes:44}, {theName:”ポーランド語”, numberOfTimes:57}, {theName:”マケドニア語”, numberOfTimes:1}, {theName:”マラヤーラム語”, numberOfTimes:3}, {theName:”マラーティー語”, numberOfTimes:3}, {theName:”マレー語”, numberOfTimes:36}, {theName:”モンゴル語”, numberOfTimes:2}, {theName:”ラトビア語”, numberOfTimes:7}, {theName:”リトアニア語”, numberOfTimes:6}, {theName:”ルーマニア語”, numberOfTimes:42}, {theName:”ロシア語”, numberOfTimes:67}, {theName:”ロシア語 (ロシア)”, numberOfTimes:1}, {theName:”ワロン語”, numberOfTimes:1}, {theName:”中国語”, numberOfTimes:6}, {theName:”中国語 (中国)”, numberOfTimes:69}, {theName:”中国語 (中華人民共和国香港特別行政区)”, numberOfTimes:1}, {theName:”中国語 (台湾)”, numberOfTimes:64}, {theName:”中国語 (簡体字)”, numberOfTimes:14}, {theName:”中国語 (繁体字)”, numberOfTimes:8}, {theName:”中国語 (繁体字、中華人民共和国香港特別行政区)”, numberOfTimes:1}, {theName:”日本語”, numberOfTimes:67}, {theName:”日本語 (日本)”, numberOfTimes:1}, {theName:”英語”, numberOfTimes:95}, {theName:”英語 (アメリカ合衆国)”, numberOfTimes:1}, {theName:”英語 (イギリス)”, numberOfTimes:7}, {theName:”韓国語”, numberOfTimes:64}}

★Click Here to Open This Script 

動作原理を理解するために、指定アプリ1つのローカライズ言語を取得するScriptを単体で動作するようにしてみました。

AppleScript名:選択したアプリケーションのローカライズ言語名称を求める
– Created 2014-12-23 by Takaaki Naganoya
– Changed 2014-12-24 by Shane Stanley
– Created 2014-12-24 by Takaaki Naganoya
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”

set a to choose file of type {“com.apple.application-bundle”}
set aRes to getSpecifiedAppFilesLocalizationList(a) of me
–> {”German”, “English”, “French”, “Japanese”}

–指定アプリケーションファイルの、指定Localeにおけるローカライズ言語リストを求める
on getSpecifiedAppFilesLocalizationList(anApp)
  
  
set aURL to (current application’s SMSFord’s URLFrom:anApp)
  
set aBundle to current application’s NSBundle’s bundleWithURL:aURL
  
  
set theNSLocale to current application’s NSLocale’s localeWithLocaleIdentifier:“en” –Output Locale Name in English (en)
  
  
–Get Localization Info
  
set locList to aBundle’s localizations()
  
–> {”de”, “English”, “fr”, “French”, “German”, “ja”, “Japanese”}
  
  
set outList to {}
  
  
repeat with i in locList
    set j to contents of i
    
set theName to (theNSLocale’s displayNameForKey:(current application’s NSLocaleIdentifier) value:j)
    
    
if theName is missing value then
      –”English”や”French”などは、missing valueが返ってくるので、”English”や”French”などをそのまま追加
      
if (j as string) is not in outList then
        set the end of outList to j as string
      end if
    else
      –”de”や”fr”などは、”German”や”French”が返ってくるので、それを追加
      
if theName as string is not in outList then
        set the end of outList to theName as string
      end if
    end if
  end repeat
  
  
return outList
  
end getSpecifiedAppFilesLocalizationList

★Click Here to Open This Script 

2014/12/24 アプリケーションのローカライズ分布を取得する v3

/Applicationsフォルダ内に入っているアプリケーションのローカライズ言語の度数分布を取得するAppleScriptのバージョンアップ版です。

言語の識別子がアプリケーションごとにゆらいでおり(Japanese/ja/ja_JPなど)、ゆらぎを吸収する処理を付加してみました。

通常のAppleScriptであれば、2D Listをループで回して、is in演算子で複数の値にヒットするかどうか処理を行うところですが、Cocoaの機能とASObjCExtras.frameworkの機能を活用して処理しました。おかげで、開発環境(MacBook Pro Retina 2012)では1秒以下で処理が終了します。

これは、処理速度もさることながら・・・AppleScriptのrecordに変換するとハイフン(「ー」)を含むようなラベルは処理がまったくできなくなってしまう(エラーになる)ため、それを回避する意味もあります。

AppleScript名:アプリケーションのローカライズ分布を取得する v3
– Created 2014-12-23 by Takaaki Naganoya
– Changed 2014-12-24 by Shane Stanley
– Changed 2014-12-24 by Takaaki Naganoya

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "ASObjCExtras"

–/Applications以下のアプリケーションのパスをすべて求める
set apPath to (path to applications folder) as string

tell application "Finder"
  tell folder apPath
    set aList to (every application file) as alias list
  end tell
end tell

–各アプリケーションのローカリゼーション情報を取得(重複情報はカウントしつつ保持)
set aDict to current application’s NSMutableDictionary’s alloc()’s init()
set theCountedSet to current application’s NSCountedSet’s |set|()

repeat with i in aList
  set j to contents of i
  
set aURL to (current application’s SMSFord’s URLFrom:j)
  
set aBundle to (current application’s NSBundle’s bundleWithURL:aURL)
  
  
–アプリケーションのローカリゼーションを取得する
  
set locList to aBundle’s localizations()’s ASify() as list
  (
theCountedSet’s addObjectsFromArray:locList)
  
  
–log {"theCountedSet", theCountedSet} –ASObjC Explorer 4だとCocoaのObjectも読める形式でログ出力可能
end repeat

–NSCountedSetをNSMutableDictionaryに変換
set theEnumerator to theCountedSet’s objectEnumerator()
repeat
  set aValue to theEnumerator’s nextObject()
  
if aValue is missing value then exit repeat
  
aDict’s setObject:(theCountedSet’s countForObject:aValue) forKey:aValue
end repeat

–言語区分リスト(実際には同一視してまとめる)。ものすごくざっくり。Googleのアプリだけものすごく狭い言語へのローカライズを行っているもよう
set langList to {{"Japanese", "ja", "ja_JP"}, {"English", "en", "Base", "American"}, {"Arabic", "ar"}, {"Arabic", "ar"}, {"Catalan", "ca"}, {"Czech", "cs"}, {"Danish", "da"}, {"Dutch", "nl"}, {"Greek", "el"}, {"Spanish", "es"}, {"Spanish@Latin America", "es_419"}, {"Finnish", "fi"}, {"French", "fr"}, {"German", "de"}, {"Hebrew", "he"}, {"Croatian", "hr"}, {"Hungarian", "hu"}, {"Italian", "it"}, {"Korean", "ko"}, {"Norwegian", "no"}, {"Polish", "pl"}, {"Portuguese@Brazil", "pt", "pt-BR"}, {"Portuguese@Portugal", "pt_PT"}, {"Romanian", "ro"}, {"Russian", "ru"}, {"Slovak", "sk"}, {"Swedish", "sv"}, {"Thai", "th"}, {"Setswana", "tr"}, {"Ukrainian", "uk"}, {"Chinese@China", "Chinese", "zh_CN", "zh", "zh-Hans"}, {"Chinese@HongKong", "zh_HK"}, {"Chinese@Taiwan", "zh_TW"}, {"Gujarati@India", "Gujarati", "gu"}, {"Bulgarian", "bg"}, {"Amharic@Ethiopia", "am"}, {"Asturian", "ast"}, {"Kazakh@Kazakhstan", "kk"}, {"Zulu, Bantu language spoken in South Africa", "zu"}, {"Filipino", "fil"}, {"Luganda", "lg"}, {"Danish@Denmark", "da_DK"}}
–すべてではないので、ちょっといい加減

–NSMutableDictionaryをArrayに入れて、複数ラベルの同一視処理を行いローカライズ言語ごとの実数の集計を行う
set bList to current application’s NSArray’s arrayWithObject:aDict
set cList to {}

repeat with i in langList
  set j to contents of i
  
  
set aArray to (current application’s SMSFord’s subarraysFrom:bList usingKeys:j outKeys:j |error|:(missing value)) –指定ラベルの項目の数値のみ抽出
  
set bArray to aArray’s lastObject() –2D Arrayで返ってくるので、1Dに
  
set cArray to (current application’s SMSFord’s arrayByReplacingNullsIn:bArray withItem:0) –Hitしなかった値がmissing valueで返るので、0に置換
  
  
–ヒットした数を合計
  
set sumNum to (cArray’s valueForKeyPath:"@sum.self")’s integerValue() as number
  
set the end of cList to {contents of first item of j, sumNum}
  
end repeat

–Sort Result (Many->Little)
set sortIndexes to {1} –Key Item id: begin from 0
set sortOrders to {false} –Descending Sort Order
set sortTypes to {"compare:"}
set rList to (current application’s SMSFord’s subarraysIn:cList sortedByIndexes:sortIndexes ascending:sortOrders sortTypes:sortTypes |error|:(missing value)) as list

return rList

–> {{"English", 338}, {"Japanese", 148}, {"German", 135}, {"French", 130}, {"Italian", 98}, {"Spanish", 95}, {"Chinese@China", 91}, {"Dutch", 77}, {"Russian", 70}, {"Korean", 67}, {"Swedish", 64}, {"Chinese@Taiwan", 64}, {"Danish", 62}, {"Portuguese@Brazil", 62}, {"Polish", 60}, {"Finnish", 56}, {"Norwegian", 49}, {"Czech", 48}, {"Setswana", 46}, {"Hungarian", 44}, {"Greek", 43}, {"Romanian", 42}, {"Slovak", 42}, {"Portuguese@Portugal", 41}, {"Croatian", 40}, {"Thai", 40}, {"Catalan", 39}, {"Ukrainian", 39}, {"Arabic", 36}, {"Arabic", 36}, {"Hebrew", 36}, {"Bulgarian", 8}, {"Filipino", 4}, {"Gujarati@India", 3}, {"Amharic@Ethiopia", 2}, {"Spanish@Latin America", 1}, {"Chinese@HongKong", 1}, {"Asturian", 1}, {"Kazakh@Kazakhstan", 1}, {"Zulu, Bantu language spoken in South Africa", 1}, {"Luganda", 1}, {"Danish@Denmark", 1}}

★Click Here to Open This Script 

2014/12/24 アプリケーションのローカライズ分布を取得するv2

/Applicationsフォルダ内に入っているアプリケーションのローカライズ言語の度数分布を取得するAppleScriptのバージョンアップ版です。

Shane Stanleyから指摘があり、データ数が増えた場合にはNSCountedSetを利用した方が有利とのこと。NSCountedSetの内容を(前バージョンよりも読みやすい形式で)ログ出力して確認したりと、ASObjCExplorer 4の新バージョンの4.1の威力を実感できました(^ー^)

asoe4.png

AppleScript名:アプリケーションのローカライズ分布を取得する v2
– Created 2014-12-23 by Takaaki Naganoya
– Changed 2014-12-24 by Shane Stanley
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”

set apPath to (path to applications folder) as string

tell application “Finder”
  tell folder apPath
    set aList to (every application file) as alias list
  end tell
end tell

set aDict to current application’s NSMutableDictionary’s alloc()’s init()
set theCountedSet to current application’s NSCountedSet’s |set|()

repeat with i in aList
  set j to contents of i
  
set aURL to (current application’s SMSFord’s URLFrom:j)
  
set aBundle to (current application’s NSBundle’s bundleWithURL:aURL)
  
  
–アプリケーションのローカリゼーションを取得する
  
set locList to aBundle’s localizations()’s ASify() as list
  (
theCountedSet’s addObjectsFromArray:locList)
  
  
log {“theCountedSet”, theCountedSet} –ASObjC Explorer 4だとCocoaのObjectも読める形式でログ出力可能
  
end repeat

–NSCountedSetをNSMutableDictionaryに変換してからASのrecordに変換
set theEnumerator to theCountedSet’s objectEnumerator()
repeat
  set aValue to theEnumerator’s nextObject()
  
if aValue is missing value then exit repeat
  
aDict’s setObject:(theCountedSet’s countForObject:aValue) forKey:aValue
end repeat

set bRec to aDict as record
–> {zh-Hant-HK:1, pt_BR:9, Finnish:1, Portuguese:2, ja:67, af:1, English:212, gu:3, es_419:1, zh:6, ps:1, Italian:50, uk_UA:1, pt:59, hr:40, ja_JP:1, bn_IN:1, Czech:1, ka:2, be:2, fur:1, en_GB:6, Russian:3, Korean:3, hu:43, bg:8, am:2, ca:39, an:1, ta:3, sh:1, si:2, is:3, es-419:1, hy:1, ro:42, it:48, ar:36, te:3, sk:42, da:58, Hungarian:1, pt_PT:41, sl:7, bn:3, ast:1, pt-BR:3, kk:1, zu:1, fil:4, th:40, ru:67, lg:1, de:63, zh_CN:69, da_DK:1, km:1, Base:30, br:1, kn:3, sq:1, ko:64, en_US:1, bs:1, tl:1, zh-Hant:8, sr:5, az:1, uk-UA:1, ach:1, co:1, nb-NO:1, Greek:1, en-GB:1, sv:59, nb:10, Dutch:38, ru-RU:1, uk:39, sw:1, cs:47, ckb:1, fa:4, Danish:4, tr:46, wa:1, kw:1, mk:1, ne:1, ml:3, vi:37, ky:1, mn:2, pt-PT:3, el:42, Spanish:45, th_TH:1, oc:1, cy:1, ga:1, lt:6, ff:1, lv:7, en:96, sv_SE:1, nl_NL:1, fi:55, mr:3, gd:1, nl:39, pa:1, ms:36, nn:1, German:72, es_MX:28, no:49, es:50, Polish:3, es_ES:2, French:67, et:6, eu:2, Swedish:5, uz:1, American:1, tet:1, my:1, he:36, cgg:1, fr:63, gl:2, ia:1, Chinese:2, zh_HK:1, zh-Hans:14, empty:3, hi:5, Japanese:80, pl:57, id:38, or:1, zh_TW:64}

★Click Here to Open This Script 

2014/12/23 アプリケーションのローカライズ分布を取得する

/Applicationsフォルダ内に入っているアプリケーションのローカライズ言語の度数分布を取得するAppleScriptです。

こうしてLocalize情報を取得すると、「empty」「base」とか「id」とかいう怪しい言語情報が返ってくることがあり、一筋縄で行かない感じがします。これでも、Carbonバイナリのアプリケーションが混ざっている頃には(Xcode以外で開発されたアプリケーションが流通していた時代には)こうした情報がキレイに取得できず(とくにバージョン情報)、楽にはなってきてはいるのですが、、、、

AppleScript名:アプリケーションのローカライズ分布を取得する
– Created 2014-12-23 by Takaaki Naganoya
– 2014 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”

set apPath to (path to applications folder) as string

tell application “Finder”
  tell folder apPath
    set aList to (every application file) as alias list
  end tell
end tell

set aDict to current application’s NSMutableDictionary’s alloc()’s init()

repeat with i in aList
  set j to contents of i
  
set aURL to (current application’s SMSFord’s URLFrom:j)
  
set aBundle to (current application’s NSBundle’s bundleWithURL:aURL)
  
  
–アプリケーションのローカリゼーションを取得する
  
set locList to aBundle’s localizations()’s ASify() as list
  
log {locList}
  
repeat with ii in locList
    set jj to contents of ii
    
try
      set cVal to (aDict’s valueForKeyPath:jj)
      
set bVal to cVal as integer
      
set aVal to bVal + 1
    on error
      set aVal to 1
    end try
    
    (
aDict’s setValue:aVal forKeyPath:jj)
    
  end repeat
  
end repeat

set bRec to aDict’s ASify() as record
–> {zh-Hant-HK:1, pt_BR:9, Finnish:1, Portuguese:2, ja:67, af:1, English:212, gu:3, es_419:1, zh:6, ps:1, Italian:50, uk_UA:1, pt:59, hr:40, ja_JP:1, bn_IN:1, Czech:1, ka:2, be:2, fur:1, en_GB:6, Russian:3, Korean:3, hu:43, bg:8, am:2, ca:39, an:1, ta:3, sh:1, si:2, is:3, es-419:1, ro:42, hy:1, it:48, ar:36, te:3, sk:42, da:58, Hungarian:1, pt_PT:41, sl:7, bn:3, ast:1, pt-BR:3, kk:1, zu:1, fil:4, th:40, ru:67, zh_CN:69, de:63, km:1, da_DK:1, lg:1, Base:30, br:1, kn:3, sq:1, ko:64, en_US:1, bs:1, sr:5, zh-Hant:8, az:1, tl:1, nb-NO:1, ach:1, co:1, uk-UA:1, Greek:1, en-GB:1, sv:59, nb:10, Dutch:38, ru-RU:1, uk:39, sw:1, cs:47, ckb:1, fa:4, Danish:4, tr:46, wa:1, kw:1, mk:1, ne:1, ml:3, vi:37, ky:1, mn:2, pt-PT:3, el:42, Spanish:45, th_TH:1, lt:6, cy:1, ga:1, ff:1, sv_SE:1, lv:7, en:96, oc:1, nl_NL:1, fi:55, mr:3, gd:1, nl:39, ms:36, pa:1, nn:1, German:72, es_MX:28, no:49, es:50, Polish:3, es_ES:2, French:67, et:6, eu:2, Swedish:5, uz:1, American:1, tet:1, my:1, he:36, cgg:1, fr:63, gl:2, ia:1, Chinese:2, zh_HK:1, zh-Hans:14, empty:3, hi:5, Japanese:80, pl:57, id:38, or:1, zh_TW:64}

★Click Here to Open This Script 

2014/12/23 選択したアプリケーションの実行ファイルを求める

選択したアプリケーションの実行ファイルを求め、ローカリゼーションの言語種別を求めるAppleScriptです。

AppleScript名:選択したアプリケーションの実行ファイルを求める
– Created 2014-12-23 by Takaaki Naganoya
– 2014 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”

set anApp to choose file of type {“com.apple.application-bundle”}
set aURL to (current application’s SMSFord’s URLFrom:anApp)
set aBundle to current application’s NSBundle’s bundleWithURL:aURL

–実行ファイル本体のパスを求める
set theBundlePath to aBundle’s executablePath()
–> “/Applications/Transmit.app/Contents/MacOS/Transmit”

–アプリケーションのローカリゼーションを取得する
set localizationList to aBundle’s localizations()’s ASify() as list
–> {”de”, “English”, “fr”, “French”, “German”, “ja”, “Japanese”}

★Click Here to Open This Script 

2014/12/12 バージョン番号文字列からメジャーバージョンを求める v2v3

バージョン番号文字列(”10.10.1”とか”4.1.1”とか)から、メジャーバージョン番号の数値(10とか4とか)を求めるAppleScriptです。

最初のバージョンでは、アプリケーション名称(display name)と本当の名称(プロセス名)が異なるものについては、AppleScript側から「これはなんですか?」といちいち聞かれていたので、nameではなくBundle IDを取得してバージョン番号を求めるようにしてみました(Safariだけ確認したかったので、最初のバージョンは手抜き感が、、、visible processのみ処理していたのは、手抜きゆえに ^ー^;;)。

しかも、本ルーチン・・・作っていたシステムで「バージョンを確認しなくてもいい処理方法」を見つけたので、おクラ入りしたモノだったり。いつか、使うことになる日もくるでしょう、きっと。

AppleScript名:バージョン番号文字列からメジャーバージョンを求める v2
tell application “System Events”
  set bList to bundle identifier of every process whose visible is true
  
set nList to name of every process whose visible is true
end tell

set nnList to somelistAdd({nList, bList}) of me

set vList to {}
repeat with i in nnList
  set {aName, aBundleID} to contents of i
  
tell application id aBundleID
    set aVerStr to version
  end tell
  
set the end of vList to {aName, getMajorVersionNum(aVerStr) of me}
end repeat

return vList
–> {{”Finder”, 10}, {”Activity Monitor”, 10}, {”DragThing”, 5}, {”Keynote”, 6}, {”Safari”, 8}, {”Calendar”, 8}, {”Mail”, 8}, {”ASObjC Explorer 4″, 4}, {”iTunes”, 12}, {”Script Editor”, 2}, {”UI Browser”, 2}, {”Skim”, 1}}

–バージョンNo文字列のメジャー番号を取得する
on getMajorVersionNum(vString)
  set aPos to offset of “.” in vString
  
if aPos = 0 then
    set vStr to vString
  else
    set vStr to text 1 thru (aPos - 1) of vString
  end if
  
  
try
    set vNum to vStr as number
  on error
    return false
  end try
  
  
return vNum
end getMajorVersionNum

–複数のリストを合成
–すべて同じ要素数である必要がある。また、複数のリストを{aList,bList,cList}とペアにして
–本ルーチンに渡す必要がある
on somelistAdd(aList)
  set aCount to count every item of aList
  
–> 3
  
  
–渡された入れ子リスト内の各要素の数が同じかどうかチェック
  
set countList to {}
  
repeat with i from 1 to aCount
    set bCount to count every item of item i of aList
    
set the end of countList to bCount
  end repeat
  
–> {3, 3, 3}
  
set {aResF, aResN} to retSameEveryItem(countList) of me
  
if aResF = false then return –合成する要素内の要素数が合っていなかったら処理を行わない
  
  
–合成を行う
  
set newList to {}
  
repeat with i from 1 to aResN
    set anItem to {}
    
repeat with ii from 1 to aCount
      set jj to contents of (item i of item ii of aList)
      
set the end of anItem to jj
    end repeat
    
set the end of newList to anItem
  end repeat
  
  
return newList
end somelistAdd

–リスト中の全要素が同じかどうかをbooleanで返す
on retSameEveryItem(aList)
  set a to item 1 of aList
  
set aList to rest of aList
  
set aFlag to true
  
repeat with i in aList
    if a is not equal to contents of i then
      set aFlag to false
      
exit repeat
    end if
  end repeat
  
return {aFlag, a} –Booleanとそのデータを返す
end retSameEveryItem

★Click Here to Open This Script 

で、Shaneからツッコミがあって「こっちのほうがいいよ」と送ってもらったのがこちらです(^ー^;

AppleScript名:バージョン番号文字列からメジャーバージョンを求める v3
– Created 2014-12-12 by Shane Stanley
– 2014 Shane Stanley
use AppleScript version “2.3.1″
use scripting additions
use framework “Foundation”
use framework “AppKit”

set theProcesses to current application’s NSWorkspace’s sharedWorkspace()’s runningApplications() as list
set theResult to {}

repeat with aProcess in theProcesses
  set anNSURL to aProcess’s bundleURL()
  
  
if anNSURL is not missing value then
    set theNSBundle to (current application’s NSBundle’s bundleWithURL:anNSURL)
    
    
if theNSBundle is not missing value then
      set theVersionString to ((theNSBundle’s infoDictionary())’s objectForKey:“CFBundleShortVersionString”)
      
      
if theVersionString is not missing value then
        set end of theResult to {aProcess’s localizedName() as text, (theVersionString’s componentsSeparatedByString:“.”)’s firstObject()’s integerValue()}
      end if
      
    end if
  end if
  
end repeat

return theResult

–> {{”loginwindow”, 9}, {”SystemUIServer”, 1}, {”Dock”, 1}, {”AirPlayUIAgent”, 2}, {”Finder”, 10}, {”Spotlight”, 1}, {”com.apple.internetaccounts”, 1}, {”CoreServicesUIAgent”, 134}, {”com.apple.dock.extra”, 1}, {”日本語入力プログラム”, 5}, {”アクティビティモニタ”, 10}, {”Folder Actions Dispatcher”, 1}, {”", 1}, {”QuickRes”, 4}, {”TISwitcher”, 1}, {”通知センター”, 1}, {”Keychain Circle Notification”, 1}, {”DragThing”, 5}, {”Colors for Hue”, 1}, {”EventScripts”, 1}, {”AAM Updates Notifier”, 8}, {”iCloud フォト”, 2}, {”AdobeIPCBroker”, 5}, {”universalAccessAuthWarn”, 1}, {”Wi-Fi”, 1}, {”System Events”, 1}, {”DragThing Helper”, 1}, {”Core Sync”, 1}, {”LaterAgent”, 1}, {”メール”, 8}, {”Mail Web コンテンツ”, 10600}, {”com.apple.MailServiceAgent”, 8}, {”AppleSpell.service”, 2}, {”Mail Web コンテンツ”, 10600}, {”Safari”, 8}, {”Safari Web コンテンツ”, 10600}, {”Safari Networking”, 10600}, {”スクリプトエディタ”, 2}, {”ASObjC Explorer 4″, 4}, {”ASObjCExplorerUIRunner”, 1}}

★Click Here to Open This Script 

2012/06/13 おかえり(シンプル版)

読者の方(edama2さん)からの投稿です。以前に私がAppleScript Studioで作成して公開していた(る?)、スリープ解除検知&ユーザー通知アプリケーション「おかえり」の簡略版をMac OS X 10.7から作れるようになったAppleScriptエディタベースのAppleScriptObjCで作られました。

yyyeyoyycyaye-2012-06-13-02922.jpeg

yyyeyoyycyaye-2012-06-10-235739.jpeg

投稿していただいてから公開するまでに時間がかかってしまいましたが……実は、AppleScriptエディタベースのASOCのHTML書き出しツールを用意していなかったためで……ようやく、書き出し用のツール(AppleScriptで記述)を準備しました。

——
<edama2さんより>
今日のネタは「おかえり(シンプル版)」です。なかなか更新されない「おかえり」にいらだちを覚え、ついに自分で作り……というのは冗談で、「Cocoa-applescript aplet」の習作として作ってみました。

スリープの復帰の通知を受け取り、半透明のウインドウの表示と音を鳴らすだけです。誕生日とか正月だけの特別なウィンドウはありません。

ウインドウの色と音を変更できるようにしたいところですが、Xcodeを使わずコードだけでウィンドウを作るのは結構面倒だったのとシンプル版ということで省略しました。オリジナルの「おかえり」から「Twentieth Anniversary Macintosh.aiff」だけ使用させていただきました。

最初、同じスプリクト内にカスタムビューのコード(MyView.scpt)を書いていたのですが、初期化の際にエラーが出て難儀しました。どうやら別ファイルにしないと上手く初期化してくれないようです。

作成は10.7.4、テストは10.7.4と10.6.8でしました。ASOC Scriptの新規作成は10.7.4でないとできませんが、編集は書き方によっては10.6.8でもできました。

→ アプレット(編集可能)のダウンロード
</edama2さんより>

スクリプト名:おかえり(シンプル版)
property _sound_name : “Twentieth Anniversary Macintosh”

on run
  #通知の登録
  
tell current application’s NSWorkspace’s sharedWorkspace()
    tell notificationCenter()
      addObserver_selector_name_object_(me, “okaeri:”, “NSWorkspaceScreensDidWakeNotification”, missing value)
    end tell
  end tell
end run

on quit
  current application’s NSNotificationCenter’s defaultCenter()’s removeObserver_(me)
  
continue quit
end quit

#ウィンドウを表示→待機→閉じる
on okaeri_(sender)
  set pool to current application’s NSAutoreleasePool’s new() –>意味があるかよくわからないけどとりあえず。
  
  
copy makeWin() to okaeriWindow
  
  
set filePath to current application’s NSBundle’s mainBundle()’s pathForResource_ofType_(_sound_name, “aiff”)
  
current application’s NSSound’s alloc()’s initWithContentsOfFile_byReference_(filePath, true)’s play()
  
  
delay 5
  
  
closeWin_(okaeriWindow)
  
  
pool’s drain() –release()
end okaeri_

#ウィンドウを作成
on makeWin()
  set aScreen to current application’s NSScreen’s mainScreen()
  
set aFrame to {{0, 0}, {800, 250}}
  
set aBacking to current application’s NSBorderlessWindowMask
  
set aDefer to current application’s NSBackingStoreBuffered
  
  
tell current application’s NSWindow’s alloc()
    tell initWithContentRect_styleMask_backing_defer_screen_(aFrame, aBacking, aDefer, false, aScreen)’s autorelease()
      
      
#カスタムビュー
      
tell current application’s MyView’s alloc()
        tell initWithFrame_(aFrame)
          setNeedsDisplay_(true)
          
set aView to it
        end tell
      end tell
      
      
set {origin:{x:x, y:y}, |size|:{width:w, height:h}} to contentView()’s |bounds|()
      
set titleHeight to 26
      
set y2 to h - titleHeight - (titleHeight / 6)
      
      
#タイトルバー
      
set messageText to “スリープ復帰を検出しました(” & (current date) & “)” as text
      
tell current application’s NSTextView’s alloc()
        tell initWithFrame_({{0, y2}, {w, titleHeight}})
          insertText_(messageText)
          
setAlignment_(current application’s NSCenterTextAlignment)
          
setDrawsBackground_(false)
          
setEditable_(false)
          
setFont_(current application’s NSFont’s titleBarFontOfSize_(13))
          
setSelectable_(false)
          
setTextColor_(current application’s NSColor’s whiteColor())
          
aView’s addSubview_(it)
        end tell
      end tell
      
      
set y3 to h - titleHeight
      
set y3 to h - y3 - (y3 / 6)
      
      
#メッセージビュー
      
set myName to current application’s NSProcessInfo’s processInfo()’s environment()’s valueForKey_(”LOGNAME”) as text
      
set messageText to myName & “さん” & return & “おかえりなさい”
      
tell current application’s NSTextView’s alloc()
        tell initWithFrame_({{0, y3}, {w, y2}})
          insertText_(messageText)
          
setAlignment_(current application’s NSCenterTextAlignment)
          
setDrawsBackground_(false)
          
setEditable_(false)
          
setFont_(current application’s NSFont’s fontWithName_size_(”HiraMinPro-W3″, 72))
          
setSelectable_(false)
          
setTextColor_(current application’s NSColor’s whiteColor())
          
aView’s addSubview_(it)
        end tell
      end tell
      
      
#ウィンドウの設定
      
setBackgroundColor_(current application’s NSColor’s clearColor())
      
setContentView_(aView)
      
setDelegate_(me)
      
setDisplaysWhenScreenProfileChanges_(true)
      
setHasShadow_(true)
      
setIgnoresMouseEvents_(not false)
      
setLevel_((current application’s NSScreenSaverWindowLevel) + 10000)
      
setOpaque_(false)
      
setReleasedWhenClosed_(true)
      
|center|()
      
makeKeyAndOrderFront_(me)
      
      
return it
    end tell
  end tell
end makeWin

#ウィンドウを閉じる
on closeWin_(aWindow)
  tell aWindow
    repeat with n from 10 to 1 by -1
      setAlphaValue_(n / 10)
      
delay 0.02
    end repeat
    
|close|()
  end tell
end closeWin_

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

スクリプト名:MyView
script MyView
  property parent : class “NSView”
  
  
on drawRect_(rect)
    
    
–log my |bounds|()
    
set {origin:{x:x, y:y}, |size|:{width:w, height:h}} to my |bounds|()
    
set titleHeight to 26
    
    
set thePath to current application’s NSBezierPath’s bezierPath()
    
    
#タイトルバー
    
set aFrame to {{0, h - titleHeight}, {w, titleHeight}}
    
set aPath to current application’s NSBezierPath’s bezierPathWithRect_(aFrame)
    
set strartColor to current application’s NSColor’s blackColor()’s colorWithAlphaComponent_(0.4)
    
set endColor to current application’s NSColor’s blackColor()’s colorWithAlphaComponent_(0.8)
    
tell current application’s NSGradient’s alloc()
      tell initWithStartingColor_endingColor_(strartColor, endColor)
        drawInBezierPath_angle_(aPath, 270)
      end tell
    end tell
    
thePath’s appendBezierPath_(aPath)
    
    
#メッセージ
    
set aFrame to {{0, 0}, {w, h - titleHeight}}
    
set aPath to current application’s NSBezierPath’s bezierPathWithRect_(aFrame)
    
set strartColor to current application’s NSColor’s blueColor()’s colorWithAlphaComponent_(0.4)
    
set endColor to current application’s NSColor’s blueColor()’s colorWithAlphaComponent_(0.8)
    
tell current application’s NSGradient’s alloc()
      tell initWithStartingColor_endingColor_(strartColor, endColor)
        drawInBezierPath_angle_(aPath, 270)
      end tell
    end tell
    
thePath’s appendBezierPath_(aPath)
    
  end drawRect_
  
end script

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

スクリプト名:CocoaAppletAppDelegate.scpt

– CocoaAppletAppDelegate.applescript
– Cocoa-AppleScript Applet

– Copyright 2011 {Your Company}. All rights reserved.

– This application delegate emulates the OSA script applet by loading “main.scpt” from the
– “Scripts” folder in the application resources and invoking the traditional run/open/reopen/quit
– handlers in response to Cocoa application delegate methods being called.

– This is provided in source form so that you may customize or replace it if your needs go
– beyond the basic applet handlers.

– Some of these methods must guard against re-entrancy, because invoking the main.scpt
– handler may end up invoking the event handler inherited from the current application,
– which calls the application delegate’s method again.

script CocoaAppletAppDelegate
  property parent : class “NSObject”
  
property mainScript : missing value – the applet’s main.scpt
  
property didOpenFiles : false – true = the application opened documents during startup
  
property isOpeningFiles : false – re-entrancy guard: true = in the process of opening files
  
property isReopening : false – re-entrancy guard: true = in the process of re-opening
  
property isQuitting : false – re-entrancy guard: true = in the process of quitting
  
  
on applicationWillFinishLaunching_(aNotification)
    – Insert code here to initialize your application before any files are opened
    
    
– Emulate an OSA Applet: Load the main script from the Scripts resource folder.
    
try
      set my mainScript to load script (path to resource “main.scpt” in directory “Scripts”)
    on error errMsg number errNum
      – Perhaps this should silently fail if it can’t load the script; that way, a Cocoa applet
      
– can just have Cocoa classes and no main.scpt.
      
display alert “Could not load main.scpt” message errMsg & ” (” & errNum & “)” as critical
    end try
  end applicationWillFinishLaunching_
  
  
on applicationDidFinishLaunching_(aNotification)
    – Insert code here to do startup actions after your application has initialized
    
    
if mainScript is missing value then return
    
    
– Emulate an OSA Applet: Invoke the “run” handler.
    
    
– If we have already opened files during startup, don’t invoke the run handler.
    
if didOpenFiles then return
    
    
try
      tell mainScript to run
    on error errMsg number errNum
      if errNum is not -128 then
        display alert “An error occurred while running” message errMsg & ” (” & errNum & “)” as critical
      end if
    end try
    
    
– TODO: Read the applet’s “stay open” flag and quit if it’s false or unspecified.
    
– For now, all Cocoa Applets stay open and require the run handler to explicitly quit,
    
– which is arguably more correct for a Cocoa application, anyway.
    
(* if not shouldStayOpen then
      quit
    end if *)
  end applicationDidFinishLaunching_
  
  
on applicationShouldHandleReopen_hasVisibleWindows_(sender, flag)
    – Insert code here to perform actions in response to a “reopen” event
    
    
if mainScript is missing value then return true
    
    
– Guard against re-entrancy.
    
if not isReopening then
      set isReopening to true
      
      
– Emulate an OSA Applet: Invoke the “reopen” handler. If there isn’t one, let the application object
      
– handle reopen (this is different from an OSA applet, which would do nothing if there is no handler;
      
– this way, the application will perform the usual “create untitled document” behavior by default).
      
try
        tell mainScript to reopen
        
set isReopening to false
        
        
return false
      on error errMsg number errNum
        if errNum is not -128 then
          display alert “An error occurred while reopening” message errMsg & ” (” & errNum & “)” as critical
        end if
      end try
      
      
set isReopening to false
    end if
    
    
return true
  end applicationShouldHandleReopen_hasVisibleWindows_
  
  
on application_openFiles_(sender, filenames)
    – Insert code here to perform actions in response to an “open documents” event
    
    
– Remember that we opened files, to avoid invoking the “run” handler later.
    
set didOpenFiles to true
    
    
– Guard against re-entrancy.
    
if not isOpeningFiles and mainScript is not missing value then
      set isOpeningFiles to true
      
      
try
        – Convert all the filenames from NSStrings to script strings
        
set theFilenameStrings to {}
        
repeat with eachFile in filenames
          set theFilenameStrings to theFilenameStrings & (eachFile as text)
        end repeat
        
        
tell mainScript to open theFilenameStrings
        
set isOpeningFiles to false
        
        
tell sender to replyToOpenOrPrint_(current application’s NSApplicationDelegateReplySuccess)
      on error errMsg number errNum
        if errNum = -128 then
          tell sender to replyToOpenOrPrint_(current application’s NSApplicationDelegateReplyCancel)
        else
          display alert “An error occurred while opening file(s)” message errMsg & ” (” & errNum & “)” as critical
          
tell sender to replyToOpenOrPrint_(current application’s NSApplicationDelegateReplyFailure)
        end if
      end try
      
      
set isOpeningFiles to false
    else
      tell sender to replyToOpenOrPrint_(current application’s NSApplicationDelegateReplyFailure)
    end if
  end application_openFiles_
  
  
on applicationShouldTerminate_(sender)
    – Insert code here to do any housekeeping before your application quits
    
    
– Guard against re-entrancy.
    
if not isQuitting and mainScript is not missing value then
      set isQuitting to true
      
      
– Emulate an OSA Applet: Invoke the “quit” handler; if the handler returns, it has fully
      
– handled the quit message and we should not quit, otherwise, it calls “continue quit”,
      
– which returns error -10000.
      
try
        tell mainScript to quit
        
set isQuitting to false
        
        
return current application’s NSTerminateCancel
      on error errMsg number errNum
        – -128 means there is no quit handler
        
– -10000 means the handler did “continue quit”
        
if errNum is not -128 and errNum is not -10000 then
          display alert “An error occurred while quitting” message errMsg & ” (” & errNum & “)” as critical
        end if
      end try
      
      
set isQuitting to false
    end if
    
    
return current application’s NSTerminateNow
  end applicationShouldTerminate_
  
end script

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

オリジナルの「おかえり」のコアコードは10行ぐらいで、このサンプルよりもはるかに単純な原理で動いています。
いまから10年近く前にすでに動いていたもので、オリジナルコードはAppleScriptだけで記述。公開版は透明ウィンドウを付けるため(だけ)にAppleScript Studioで開発しました。

「おかえり」のアップデートが止まっていたのは、自分が欲しい機能をてんこ盛りに盛り込んで……画面をInterface Builder上でデザインした時点で「こんなに大量のGUI部品のコードを書くのは大変」なことに気づき……そのまま放置状態に陥ってしまったためでしょう(Piyocastをはじめ、個人的な優先順位の高い自作の未公開アプリがいくつもあるので)。

アプリが自分自身のコード量で自滅した、といえなくもありません。

当時は、Mac AppStoreも存在していませんでしたし、フリーで配布するにはバージョンアップに手がかかりすぎる自作ソフト群にうんざりしていた、ということもあります。

いまだったら、AppleScriptObjCならGUI部品のサポートコードを書くのもそれほど手間ではないので、バージョンアップして公開してみてもよいかもしれません。