Archive for the 'NSLocale' Category

2017/06/21 日の出、日没時刻を計算するv2

オープンソースのプロジェクト「EDSunriseSet」(By Ernesto García)をFramework化した「EDSunriseSet.framework」を呼び出して日の出、日没時刻を計算するAppleScriptのアップデート版です。

本AppleScriptのテストのためには、EDSunriseSet.frameworkを~/Library/Frameworksフォルダに入れておく必要があります。バイナリを用意しておきましたので、自己責任でおためしください。

→ Download Binary (26KB)

EDSunriseSetが用意している各種の日の出、日没情報を取得するようにしてみました。

日没や日の出前後の「薄明」の情報についても取得します。

1024px-twilight_subcategoriessvg_resized.png

ただ、白夜などの現象が発生しない地域でこれらの薄明の情報はとくに必要はないように感じます(個人の見解です)。

AppleScript名:日の出、日没時刻を計算するv2
– Created 2017-06-19 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “EDSunriseSet” –erndev/EDSunriseSet
–https://github.com/erndev/EDSunriseSet
–http://piyocast.com/as/archives/4699

set cityRecList to {{kCityName:“Tokyo”, kCityLatitude:(35.6894875), kCityLongitude:(139.6917064), kCityTimeZone:“Asia/Tokyo”}}

set dList to {}

repeat with i in cityRecList
  set the end of dList to getSunriseSunset(i) of me
end repeat
return dList
–>  {{sunrise:date “2017年6月21日水曜日 4:25:37″, sunset:date “2017年6月21日水曜日 19:00:21″, civilTwilightStart:date “2017年6月21日水曜日 3:55:35″, civilTwilightEnd:date “2017年6月21日水曜日 19:30:23″, nauticalTwilightStart:date “2017年6月21日水曜日 3:18:15″, nauticalTwilightEnd:date “2017年6月21日水曜日 20:07:44″, astronomicalTwilightStart:date “2017年6月21日水曜日 2:36:45″, astronomicalTwilightEnd:date “2017年6月21日水曜日 20:49:13″, cityname:”Tokyo”}}

on getSunriseSunset(cityRec)
  set curLocale to current application’s NSLocale’s currentLocale()
  
set curDate to current application’s NSDate’s |date|()
  
  
set aTZ to current application’s NSTimeZone’s alloc()’s initWithName:(kCityName of cityRec)
  
set aSunrizeSunset to current application’s EDSunriseSet’s alloc()’s initWithDate:curDate timezone:aTZ latitude:(kCityLatitude of cityRec) longitude:(kCityLongitude of cityRec)
  
  
–日の出、日没  
  
set aSunRiseDate to (aSunrizeSunset’s sunrise) as date
  
set aSunSetDate to (aSunrizeSunset’s sunset) as date
  
  
–https://en.wikipedia.org/wiki/Twilight
  
–https://ja.wikipedia.org/wiki/薄明
  
  
–市民薄明(常用薄明、第三薄明)
  
set aCivilTwilightStart to (aSunrizeSunset’s civilTwilightStart) as date
  
set aCivilTwilightEnd to (aSunrizeSunset’s civilTwilightEnd) as date
  
  
–航海薄明(第二薄明)
  
set aNauticalTwilightStart to (aSunrizeSunset’s nauticalTwilightStart) as date
  
set aNauticalTwilightEnd to (aSunrizeSunset’s nauticalTwilightEnd) as date
  
  
–天文薄明(第一薄明)
  
set anAstronomicalTwilightStart to (aSunrizeSunset’s astronomicalTwilightStart) as date
  
set anAstronomicalTwilightEnd to (aSunrizeSunset’s astronomicalTwilightEnd) as date
  
  
return {sunrise:aSunRiseDate, sunset:aSunSetDate, civilTwilightStart:aCivilTwilightStart, civilTwilightEnd:aCivilTwilightEnd, nauticalTwilightStart:aNauticalTwilightStart, nauticalTwilightEnd:aNauticalTwilightEnd, astronomicalTwilightStart:anAstronomicalTwilightStart, astronomicalTwilightEnd:anAstronomicalTwilightEnd, cityname:kCityName of cityRec}
end getSunriseSunset

★Click Here to Open This Script 

2017/06/19 日の出、日没時刻を計算する

オープンソースのプロジェクト「EDSunriseSet」(By Ernesto García)をFramework化した「EDSunriseSet.framework」を呼び出して日の出、日没時刻を計算するAppleScriptです。

edsunriseset.png
▲もともとのGUI版サンプルプログラム

本AppleScriptのテストのためには、EDSunriseSet.frameworkを~/Library/Frameworksフォルダに入れておく必要があります。バイナリを用意しておきましたので、自己責任でおためしください。

→ Download Binary (26KB)

本Scriptは単にEDSunriseSetのサンプル中に書かれていたサンプルデータをそのまま利用したものであり、本来であればCoreLocationを呼び出して現在位置を取得し、システム設定からタイムゾーン情報を取得し位置情報から逆住所ジオコーダーで場所の住所情報を取得するといった処理になることでしょう。もちろん、任意の年月日の日付オブジェクトを作って指定するとか。

AppleScript名:日の出、日没時刻を計算する
– Created 2017-06-19 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “EDSunriseSet” –erndev/EDSunriseSet
–https://github.com/erndev/EDSunriseSet
–http://piyocast.com/as/archives/4696

set cityRecList to {{kCityName:“Madrid”, kCityLatitude:(40.4165), kCityLongitude:(-3.70256), kCityTimeZone:“Europe/Madrid”}, {kCityName:“Beijing”, kCityLatitude:(39.9075), kCityLongitude:(116.39723), kCityTimeZone:“Asia/Shanghai”}, {kCityName:“Cupertino”, kCityLatitude:(37.3229978), kCityLongitude:(-122.0321823), kCityTimeZone:“America/Los_Angeles”}, {kCityName:“New York”, kCityLatitude:(40.7127837), kCityLongitude:(-74.0059413), kCityTimeZone:“America/New_York”}, {kCityName:“Tokyo”, kCityLatitude:(35.6894875), kCityLongitude:(139.6917064), kCityTimeZone:“Asia/Tokyo”}, {kCityName:“Sydney”, kCityLatitude:(-33.8674869), kCityLongitude:(151.2069902), kCityTimeZone:“Australia/Sydney”}}

set dList to {}

repeat with i in cityRecList
  set the end of dList to getSunriseSunset(i) of me
end repeat
return dList
–>  {{sunrise:date “2017年6月19日月曜日 13:44:22″, sunset:date “2017年6月20日火曜日 4:48:04″, cityname:”Madrid”}, {sunrise:date “2017年6月19日月曜日 5:45:35″, sunset:date “2017年6月19日月曜日 20:45:54″, cityname:”Beijing”}, {sunrise:date “2017年6月19日月曜日 21:47:30″, sunset:date “2017年6月20日火曜日 12:31:43″, cityname:”Cupertino”}, {sunrise:date “2017年6月19日月曜日 18:24:37″, sunset:date “2017年6月20日火曜日 9:30:19″, cityname:”New York”}, {sunrise:date “2017年6月19日月曜日 4:25:14″, sunset:date “2017年6月19日月曜日 18:59:52″, cityname:”Tokyo”}, {sunrise:date “2017年6月19日月曜日 5:59:28″, sunset:date “2017年6月19日月曜日 15:53:30″, cityname:”Sydney”}}

on getSunriseSunset(cityRec)
  set curLocale to current application’s NSLocale’s currentLocale()
  
set curDate to current application’s NSDate’s |date|()
  
  
set aTZ to current application’s NSTimeZone’s alloc()’s initWithName:(kCityName of cityRec)
  
set aSunrizeSunset to current application’s EDSunriseSet’s alloc()’s initWithDate:curDate timezone:aTZ latitude:(kCityLatitude of cityRec) longitude:(kCityLongitude of cityRec)
  
  
set aSunRiseDate to (aSunrizeSunset’s sunrise) as date
  
–>  date “2017年6月19日月曜日 4:25:14″
  
  
set aSunSetDate to (aSunrizeSunset’s sunset) as date
  
–>  date “2017年6月19日月曜日 18:59:52″
  
  
return {sunrise:aSunRiseDate, sunset:aSunSetDate, cityname:kCityName of cityRec}
end getSunriseSunset

★Click Here to Open This Script 

2017/04/11 指定のテキストから言語コードや言語名を取得する

Shane Stanleyによる、指定テキストを識別して言語コードや言語名を取得するAppleScriptです。

割とアラビア語のテキストを判定するのに文字数が必要だったのが意外です。

以前に掲載した「2015/09/10 テキストの言語を推測する」よりも若干(1行ぐらい)短くなっています。

AppleScript名:指定のテキストから言語コードや言語名を取得する
– Created 2017-04-10 by Shane Stanley
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

set str1 to “Ilmatyynyalukseni on täynnä ankeriaita.”
set str2 to “Luftputebåten min er full av ål”
set str3 to “私の名前は長野谷です。”
set str4 to أنشأ فوكوزاوا يوكيتشي (١٨٣٥-١٩٠١) في اكتوبر عام ١٨٥٨ مدرسة للدراسات الهولندية (تحولت بعد ذلك لمدرسة للغة الانكليزية) في ايدو (طوكيو حاليا). يعد فوكوزاوا يوكيتشي من أحد مؤسسي نهضة اليابان الحديثة، فونهتم بمدرستنا بنوع التعليم الذي ينمي القدرات الإبداعية والفنية التي يتطلب توافرها في طلاب الجامعة بحيث لا ينشغل الطلاب باختبار قبول الجامعات ونحترم استقلالية وتفرد كل طالب وذلك في جو دافئ في بيئة طبيعية مليئة بأشجار
set str5 to 게이오 기주쿠는 어디에나 있는 학교의 하나로 만족하지 않습니다. 게이오 기주쿠는 기주쿠(義塾, 의숙)에서 배우는 학생과 교원이 일본의기품의 원천지덕의 모범 되는 것을 목표로 하는 학숙(學塾)입니다. “
set str6 to 庆应义塾不是仅仅满足于成常常到的一般性学校。”

set a1Res to my guessLanguageCodeOf:str1 –>  ”fi”
set a2Res to my guessLanguageCodeOf:str2 –>  ”sv”
set a3Res to my guessLanguageCodeOf:str3 –>  ”ja”
set a4Res to my guessLanguageCodeOf:str4 –>  ”ar”
set a5Res to my guessLanguageCodeOf:str5 –>  ”ko”
set a6Res to my guessLanguageCodeOf:str6 –>  ”zh-Hans”

set b1Res to my guessLanguageOf:str1 –>  ”Finnish”
set b2Res to my guessLanguageOf:str2 –>  ”Swedish”
set b3Res to my guessLanguageOf:str3 –>  ”Japanese”
set b4Res to my guessLanguageOf:str4 –>  ”Arabic”
set b5Res to my guessLanguageOf:str5 –>  ”Korean”
set b6Res to my guessLanguageOf:str6 –>  ”Chinese”

on guessLanguageOf:theString
  set theTagger to current application’s NSLinguisticTagger’s alloc()’s initWithTagSchemes:{current application’s NSLinguisticTagSchemeLanguage} options:0
  
theTagger’s setString:theString
  
set languageID to theTagger’s tagAtIndex:0 |scheme|:(current application’s NSLinguisticTagSchemeLanguage) tokenRange:(missing value) sentenceRange:(missing value)
  
return ((current application’s NSLocale’s localeWithLocaleIdentifier:“en”)’s localizedStringForLanguageCode:languageID) as text
end guessLanguageOf:

on guessLanguageCodeOf:theString
  set theTagger to current application’s NSLinguisticTagger’s alloc()’s initWithTagSchemes:{current application’s NSLinguisticTagSchemeLanguage} options:0
  
theTagger’s setString:theString
  
set languageID to theTagger’s tagAtIndex:0 |scheme|:(current application’s NSLinguisticTagSchemeLanguage) tokenRange:(missing value) sentenceRange:(missing value)
  
return languageID as text
end guessLanguageCodeOf:

★Click Here to Open This Script 

2016/01/14 ローカライズドな度量衡表示(エネルギー)

Cocoaの機能を用いて、ローカライズされた(英語、日本語などの言語環境を考慮した)度量衡の表示を行うAppleScriptです。

「カロリー」の定義(算出方法)がいろいろあるので、「熱力学カロリー」と明記するのはけっこうなことだと思いますが、一般的にはそこまで厳密に表記しないのでちょっと違和感があります。

このあたり、ローカライズの下請けを行っているLocTeamが先走ってしまったのではないかと思われます。

AppleScript名:ASOCでローカライズドな度量衡表示(エネルギー)
– Created 2015-11-20 by Shane Stanley
– Modified 2016-01-14 by Takaaki Naganoya
– 2016 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

–Energy

set engEFormatter to current application’s NSEnergyFormatter’s alloc()’s init()
engEFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:“en-US”)
engEFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong)

engEFormatter’s setForFoodEnergyUse:true
set a1Res to (engEFormatter’s stringFromJoules:85) as string
–>  ”20.315 calories”

engEFormatter’s setForFoodEnergyUse:false
set a1Res to (engEFormatter’s stringFromJoules:85) as string
–>  ”20.315 calories”

set a2Res to (engEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitJoule)) as string –joule
–>  ”1 joule”
set a3Res to (engEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitKilojoule)) as string –k joule
–>  ”1 kilojoule”
set a4Res to (engEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitCalorie)) as string –Cal
–>  ”1 calorie”
set a5Res to (engEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitKilocalorie)) as string –K Cal
–>  ”1 kilocalorie”

set jpnEFormatter to current application’s NSEnergyFormatter’s alloc()’s init()
jpnEFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:“ja”)
jpnEFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong)

jpnEFormatter’s setForFoodEnergyUse:true
set j1Res to (jpnEFormatter’s stringFromJoules:85) as string
–>  ”85ジュール”

jpnEFormatter’s setForFoodEnergyUse:false
set j1Res to (jpnEFormatter’s stringFromJoules:85) as string
–>  ”85ジュール”

set j2Res to (jpnEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitJoule)) as string –joule
–>  ”1ジュール”
set j3Res to (jpnEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitKilojoule)) as string –k joule
–>  ”1キロジュール”
set j4Res to (jpnEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitCalorie)) as string –Cal
–>  ”1熱力学カロリー”
set j5Res to (jpnEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitKilocalorie)) as string –K Cal
–>  ”1キロカロリー”

★Click Here to Open This Script 

2016/01/14 ローカライズドな度量衡表示(重さ)

Cocoaの機能を用いて、ローカライズされた(英語、日本語などの言語環境を考慮した)度量衡の表示を行うAppleScriptです。

重さについては、トン(t=1,000kg)がないのと細かい方の単位(mgとかμgとか)がないのはいかがなものかと。

日本国内では日常的には「ストーン」という単位は使わないので、「何の石かな?」と思ってしまうところです。

AppleScript名:ASOCでローカライズドな度量衡表示(重さ)
– Created 2015-11-20 by Shane Stanley
– Modified 2016-01-14 by Takaaki Naganoya
– 2016 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

–Weight

set engMFormatter to current application’s NSMassFormatter’s alloc()’s init()
engMFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:“en-US”)
engMFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong) –NSFormattingUnitStyleShort, NSFormattingUnitStyleMedium, NSFormattingUnitStyleLong

set a1Res to (engMFormatter’s stringFromKilograms:85) as string –Kg
–>  ”187.393 pounds”
set a2Res to (engMFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitGram)) as string –g
–>  ”1 gram”
set a3Res to (engMFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitOunce)) as string –オンス
–>  ”1 ounce”
set a4Res to (engMFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitStone)) as string –ストーン(=14 pounds)
–>  ”1 stone”

set jpnMFormatter to current application’s NSMassFormatter’s alloc()’s init()
jpnMFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:“ja”)
jpnMFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong) –NSFormattingUnitStyleShort, NSFormattingUnitStyleMedium, NSFormattingUnitStyleLong

set j1Res to (jpnMFormatter’s stringFromKilograms:85) as string –Kg
–>  ”85キログラム”
set j2Res to (jpnMFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitGram)) as string –g
–>  ”1グラム”
set j3Res to (jpnMFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitOunce)) as string –オンス
–>  ”1オンス”
set j4Res to (jpnMFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitStone)) as string –ストーン(=14 pounds)
–>  ”1ストーン

★Click Here to Open This Script 

2016/01/14 ローカライズドな度量衡表示(長さ)

Cocoaの機能を用いて、ローカライズされた(英語、日本語などの言語環境を考慮した)度量衡の表示を行うAppleScriptです。

AppleScript名:ASOCでローカライズドな度量衡表示(長さ)
– Created 2015-11-20 by Shane Stanley
– Modified 2016-01-14 by Takaaki Naganoya
– 2016 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

–Length

set engLFormatter to current application’s NSLengthFormatter’s alloc()’s init()
engLFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:“en-US”)
engLFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong) –NSFormattingUnitStyleShort, NSFormattingUnitStyleMedium, NSFormattingUnitStyleLong

set a1Res to (engLFormatter’s stringFromMeters:85) as string –m
–>  ”92.956 yards”
set a2Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitMillimeter)) as string –mm
–>  ”1 millimeter”
set a3Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitCentimeter)) as string –cm
–>  ”1 centimeter”
set a4Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitMeter)) as string –m
–>  ”1 meter”
set a5Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitKilometer)) as string –Km
–>  ”1 kilometer”
set a6Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitInch)) as string –inch
–>  ”1 inch”
set a7Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitFoot)) as string –feet
–>  ”1 foot”
set a8Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitYard)) as string –Yard
–>  ”1 yard”
set a9Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitMile)) as string –Mile
–>  ”1 mile”

set jpnLFormatter to current application’s NSLengthFormatter’s alloc()’s init()
jpnLFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:“ja”)
jpnLFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong) –NSFormattingUnitStyleShort, NSFormattingUnitStyleMedium, NSFormattingUnitStyleLong

set j1Res to (jpnLFormatter’s stringFromMeters:85) as string –m
–>  ”85メートル”
set j2Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitMillimeter)) as string –mm
–>  ”1ミリメートル”
set j3Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitCentimeter)) as string –cm
–>  ”1センチメートル”
set j4Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitMeter)) as string –m
–>  ”1メートル”
set j5Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitKilometer)) as string –Km
–>  ”1キロメートル”
set j6Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitInch)) as string –inch
–>  ”1インチ”
set j7Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitFoot)) as string –feet
–>  ”1フィート”
set j8Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitYard)) as string –Yard
–>  ”1ヤード”
set j9Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitMile)) as string –Mile
–>  ”1マイル”

★Click Here to Open This Script 

2015/08/27 ASOCでNSLocaleのじっけん

CocoaのNSLocale関連のメソッドなどを調べてみました。

AppleScript名:ASOCでNSLocaleのじっけん
– Created 2015-08-26 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

set aLocale to current application’s NSLocale’s currentLocale()
–>  (__NSCFLocale) <__nscflocale : 0x6000000f6b00>

aLocale’s localeIdentifier()
–>  (NSString) “ja_JP”

set bLocale to current application’s NSLocale’s systemLocale()
–>  (__NSCFLocale) <__nscflocale : 0x6180000f9100>

bLocale’s localeIdentifier()
–>  (NSString) “”

set cLocale to current application’s NSLocale’s alloc()’s initWithLocaleIdentifier:“en_US”
–>  (__NSCFLocale) <__nscflocale : 0x6000010f5680>

cLocale’s localeIdentifier()
–>  (NSString) “en_US”

set anArray to current application’s NSLocale’s availableLocaleIdentifiers()
–>  (NSArray) {​​​​​”eu”, ​​​​​”hr_BA”, ​​​​​”en_CM”, ​​​​​”rw_RW”, ….., ​​​​​”ja_JP”….”uk_UA”​​​}

set bArray to current application’s NSLocale’s ISOCountryCodes()
–>  (NSArray) {​​​​​”AD”, ​​​​​”AE”, ​​​​​”AF”, ​​​​​”AG”,….. ​​​​​”JP”…. ​​​​​”ZW”​​​}

set cArray to current application’s NSLocale’s ISOCurrencyCodes()
–>  (NSArray) {​​​​​”ADP”, ​​​​​”AED”, ​​​​​”AFA”, ​​​​​….”JPY”….. ​​​​​”ZWD”​​​}

set dArray to current application’s NSLocale’s ISOLanguageCodes()
–>  (NSArray) {​​​​​”aa”, ​​​​​”ab”, ​​​​​”ace”, ​​​​​”ach”,…. ​​​​​”jpr”,….​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​”zza”​​​}

set eArray to current application’s NSLocale’s commonISOCurrencyCodes()
–>  (NSArray) {​​​​​”AED”, ​​​​​”AFN”, ​​​​​”ALL”, …​​​​​”JPY”….​​​​​”ZMW”​​​}

set aStr to current application’s NSLocale’s canonicalLocaleIdentifierFromString:“ja_JP”
–>  (NSString) “ja_JP”

set aObj to current application’s NSLocale’s componentsFromLocaleIdentifier:“ja_JP”
–>  (NSDictionary) {​​​​​kCFLocaleLanguageCodeKey:”ja”, ​​​​​kCFLocaleCountryCodeKey:”JP”​​​}

set bObj to current application’s NSLocale’s localeIdentifierFromComponents:aObj
–>  (NSString) “ja_JP”

set bStr to current application’s NSLocale’s canonicalLanguageIdentifierFromString:“ja_JP”
–>  (NSString) “ja-JP”

set cStr to current application’s NSLocale’s localeIdentifierFromWindowsLocaleCode:17
–>  (NSString) “ja”

set dStr to current application’s NSLocale’s windowsLocaleCodeFromLocaleIdentifier:“ja_JP”
–>  1041

set fArray to current application’s NSLocale’s preferredLanguages()
–>  (NSArray) {​​​​​”ja”, ​​​​​”en-US”, ​​​​​”en-GB”, ​​​​​”fr”, ​​​​​”en”​​​}

set aNum to current application’s NSLocale’s characterDirectionForLanguage:“ja_JP”
–> 1 –Left to Right
set bNum to current application’s NSLocale’s characterDirectionForLanguage:“ar”
–>  2 –Right to Left

set cNum to current application’s NSLocale’s lineDirectionForLanguage:“ja_JP”
–>  3 –Up to Down
set dNum to current application’s NSLocale’s lineDirectionForLanguage:“ar”
–>  3 –Up to Down

★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/16 指定ロケールの月名、曜日名を取得する

指定ロケールの月名、曜日名を取得するAppleScriptです。

曜日名などはローカライズされた呼称がけっこう言語ごとに異なるので、重要です。香港のクライアントと仕事をしたときに、1週間の開始曜日が月曜日だったのと、曜日が中国語の(漢字なのに)異なる表記で驚かされました。

OS X自体がサポートしている言語の数だけ月および曜日呼称を取得できるはずですが、話者人口が少ない言語だと英語表記がそのまま表示されるかもしれません。

AppleScript名:指定ロケールの月名、曜日名を取得する
– Created 2014-01-26 Shane Stanley
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”

set mList to monthNames(“ja”) –Japanese
–> {”1月”, “2月”, “3月”, “4月”, “5月”, “6月”, “7月”, “8月”, “9月”, “10月”, “11月”, “12月”}

set mList to monthNames(“zh_CN”) –Chinese
–> {”一月”, “二月”, “三月”, “四月”, “五月”, “六月”, “七月”, “八月”, “九月”, “十月”, “十一月”, “十二月”}

set mList to monthNames(“us”) –US English
–> {”January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”}

set mList to monthNames(“fr”) –French
–> {”janvier”, “février”, “mars”, “avril”, “mai”, “juin”, “juillet”, “août”, “septembre”, “octobre”, “novembre”, “décembre”}

set mList to monthNames(“ru”) –Russian
–> {”января”, “февраля”, “марта”, “апреля”, “мая”, “июня”, “июля”, “августа”, “сентября”, “октября”, “ноября”, “декабря”}

set dList to dayNames(“ja”) –Japanese
–> {”日曜日”, “月曜日”, “火曜日”, “水曜日”, “木曜日”, “金曜日”, “土曜日”}

set dList to dayNames(“zh_CN”) –Chinese
–> {”星期日”, “星期一”, “星期二”, “星期三”, “星期四”, “星期五”, “星期六”}

set dList to dayNames(“us”) –US English
–> {”Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}

set dList to dayNames(“fr”) –French
–> {”dimanche”, “lundi”, “mardi”, “mercredi”, “jeudi”, “vendredi”, “samedi”}

set dList to dayNames(“ru”) –Russian
–> {”воскресенье”, “понедельник”, “вторник”, “среда”, “четверг”, “пятница”, “суббота”}

on monthNames(aLocStr as string)
  set fm to current application’s NSDateFormatter’s new()
  
fm’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:aLocStr)
  
return fm’s monthSymbols() as list
end monthNames

on dayNames(aLocStr as string)
  set fm to current application’s NSDateFormatter’s new()
  
fm’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:aLocStr)
  
return fm’s weekdaySymbols() as list
end dayNames

★Click Here to Open This Script 

2014/10/29 AppleScriptからASOCのソートルーチンを利用する上での注意事項

OS X 10.10で一般のAppleScriptとASOCを普通にまじえて記述できるようになったため、いろいろと試してみました。

すると、ソートについていろいろと注意すべき点がわかってきたのでまとめてみます。

文字列のソート順序が違う

ためしにさまざまな文字を与えて並び順を調べてみたら、微妙に違うことがわかりました。

テストデータとして、

set aList to {”ん”, “ば”, “は”, “ぱ”, “あ”, “ま”, “ぁ”, “ア”, “か”, “ァ”}

のようなデータを用意し、昇順ソートを指定した際に、

AS: {”あ”, “ァ”, “ぁ”, “ア”, “か”, “は”, “ば”, “ぱ”, “ま”, “ん”}
ASOC:{”あ”, “ぁ”, “ア”, “ァ”, “か”, “は”, “ば”, “ぱ”, “ま”, “ん”}

と、文字の並び順が微妙に異なります。ASOC側でもうちょっとソートのパラメータを指定すると結果が変わったりしてきそうですが、とりあえず現状はこんな感じです。

どちらかといえば、ASだけで行うソートの方に不自然さを感じるぐらいですし、いまどき半角カタカナでもないでしょうから無視できそうですが・・・問題は次です。

小数を含む数値リストをソートすると丸め誤差が出る

小数を含む数値のリスト、

set aList to {1, 1, 10, 0.1, 1.1, -1, -100}

をデータとして与えてソートしてみたところ、返ってきた結果は、

–> {-100, -1, 0.10000000149, 1, 1.100000023842, 10}

微妙に値が変わってしまいました

回避策についてはいろいろ調べていますが、(US AppleのAppleScript Users MLに久しぶりに投稿してShane Stanleyに教えてもらいました)小数の値を含む数値リストのやりとりをするのは得策ではなさそうな雰囲気です(いまのところ)。

少なくとも、ASから数値をASOCのハンドラに渡しただけでは変わらないようです。

いろいろ試してみたところ、ハンドラ側でNSNumberに変換して処理を行い、結果を戻すさいに「as number」とか「as real」とかで型変換して返すときに丸め誤差が出てしまう模様です。

数値のパラメータであれば、ASから数値をASOCハンドラに渡して、丸め誤差を出さずに返すことはできたのですが、リストに入れてしまうとお手上げの状態。

まだ、試行錯誤を行ってみる必要があるようです。

AppleScript名:asocで1D Listをソート
use AppleScript version “2.4″
use framework “Foundation”
use scripting additions

–昇順ソート(ASOC)
set aList to {“ん”, “ば”, “は”, “ぱ”, “あ”, “ま”, “ぁ”, “ア”, “か”, “ァ”}
set aRes to sort1DList_ascOrder_(aList, true)
–> {”あ”, “ぁ”, “ア”, “ァ”, “か”, “は”, “ば”, “ぱ”, “ま”, “ん”}

–降順ソート(ASOC)
set bRes to sort1DList_ascOrder_(aList, false)
–> {”ん”, “ま”, “ぱ”, “ば”, “は”, “か”, “あ”, “ぁ”, “ア”, “ァ”}

–昇順ソート(AppleScript)
set cRes to shellSortAscending(aList)
–> {”あ”, “ァ”, “ぁ”, “ア”, “か”, “は”, “ば”, “ぱ”, “ま”, “ん”}

–昇順ソート(AppleScript)
set dRes to shellSortDescending(aList)
–> {”ん”, “ま”, “ぱ”, “ば”, “は”, “か”, “ぁ”, “ア”, “あ”, “ァ”}

set aList to {“1″, “01″, “10″, “000.1″, “1.1″, “-1″, “-100″}
set aRes to sort1DList_ascOrder_(aList, true)
–> {”-1″, “-100″, “000.1″, “01″, “1″, “1.1″, “10″}

set aList to {1, 1, 10, 0.1, 1.1, -1, -100}
set aRes to sort1DNumList_ascOrder_(aList, true)
–> {-100, -1, 0.10000000149, 1, 1.100000023842, 10}

–1D List(数値)をsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート
on sort1DNumList:theList ascOrder:aBool
  tell current application’s NSSet to set theSet to setWithArray_(theList)
  
tell current application’s NSSortDescriptor to set theDescriptor to sortDescriptorWithKey_ascending_(missing value, true)
  
set sortedList to theSet’s sortedArrayUsingDescriptors:{theDescriptor}
  
return (sortedList) as list
end sort1DNumList:ascOrder:

–1D List(文字)をsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート
on sort1DList:theList ascOrder:aBool
  set aDdesc to current application’s NSSortDescriptor’s sortDescriptorWithKey:“self” ascending:aBool selector:“localizedCaseInsensitiveCompare:”
  
set theArray to current application’s NSArray’s arrayWithArray:theList
  
return (theArray’s sortedArrayUsingDescriptors:{aDdesc}) as list
end sort1DList:ascOrder:

–入れ子ではないリストの昇順ソート
on shellSortAscending(aSortList)
  script oBj
    property list : aSortList
  end script
  
set len to count oBj’s list’s items
  
set gap to 1
  
repeat while (gap len)
    set gap to ((gap * 3) + 1)
  end repeat
  
repeat while (gap > 0)
    set gap to (gap div 3)
    
if (gap < len) then
      repeat with i from gap to (len - 1)
        set temp to oBj’s list’s item (i + 1)
        
set j to i
        
repeat while ((j gap) and (oBj’s list’s item (j - gap + 1) > temp))
          set oBj’s list’s item (j + 1) to oBj’s list’s item (j - gap + 1)
          
set j to j - gap
        end repeat
        
set oBj’s list’s item (j + 1) to temp
      end repeat
    end if
  end repeat
  
return oBj’s list
end shellSortAscending

–入れ子ではないリストの降順ソート
on shellSortDescending(aSortList)
  script oBj
    property list : aSortList
  end script
  
set len to count oBj’s list’s items
  
set gap to 1
  
repeat while (gap len)
    set gap to ((gap * 3) + 1)
  end repeat
  
repeat while (gap > 0)
    set gap to (gap div 3)
    
if (gap < len) then
      repeat with i from gap to (len - 1)
        set temp to oBj’s list’s item (i + 1)
        
set j to i
        
repeat while ((j gap) and (oBj’s list’s item (j - gap + 1) < temp))
          set oBj’s list’s item (j + 1) to oBj’s list’s item (j - gap + 1)
          
set j to j - gap
        end repeat
        
set oBj’s list’s item (j + 1) to temp
      end repeat
    end if
  end repeat
  
return oBj’s list
end shellSortDescending

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

AppleScript名:asocで少数を含む数値を誤差のない形式でASに返す
–By Shane Stanley

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

set e to 0.1
set f to numberFloatWith_(e)
–> 0.1

on numberFloatWith:aNum
  set theNum to current application’s NSNumber’s numberWithFloat:aNum
  
return (theNum’s descriptionWithLocale:(current application’s NSLocale’s systemLocale())) as real
end numberFloatWith:

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