オープンソースの「DDUnitConverter」(By Dave DeLong)を用いて、度量衡(measurement unit)の変換を行うAppleScriptです。AppleScriptから呼び出すために、DDUnitConverterをフレームワーク化した「ddUnitConversionKit」を作成しています。
もともと、AppleScriptには内蔵の度量衡変換機能があり、
AppleScript名:インチからセンチへの変換 |
set a to 1 set a to a as inches –> inches 1.0 set b to a as centimeters –> centimeters 2.54 set c to a as meters –> meters 0.0254 set d to a as kilometers –> kilometers 2.54E-5 |
とか、
AppleScript名:液体の容積の単位変換 |
–液体の容積の単位変換 set a to 10 as liters –> liters 10.0 set b to a as gallons –> gallons 2.641720372842 set c to a as quarts –> quarts 10.566881491367 |
ぐらいは簡単にできます。AppleScriptがサポートしている度量衡は、長さ、重さ、温度、液体の容積です。
ただ、mmの下のμmとか、オングストロームとか、そういう単位には対応していませんし、重量でいえばKgに対応していてもt(トン)の単位はサポートしていなかったりと、日常的な度量衡の単位の世界でも不十分な感じが否めません(数値の有効桁数が10桁と小さいことも影響?)。
Cocoaの世界で度量衡変換機能について調べてみたところ、km→m→cm→mmといった変換はしてくれるものの、他の単位への変換は行ってくれていなかったりして、標準機能だけではいまひとつな印象です。
AppleScript名:NSLengthFormatterのじっけん |
— Created 2015-11-18 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –http://dev.classmethod.jp/references/ios8-new-formatter/ set aLengthFormatter to current application’s NSLengthFormatter’s alloc()’s init() set aMeters to 5000 set aRes to (aLengthFormatter’s stringFromMeters:aMeters) as text –> "5 km" set bRes to (aLengthFormatter’s stringFromMeters:500) as text –> "500 m" set cRes to (aLengthFormatter’s stringFromMeters:0.5) as text –> "50 cm" set dRes to (aLengthFormatter’s stringFromMeters:0.005) as text –> "5 mm" set eRes to (aLengthFormatter’s stringFromMeters:("0.000005" as real)) as text –> "0.005 mm" set res2 to aLengthFormatter’s getObjectValue:(missing value) forString:aRes errorDescription:(missing value) –> false set res3 to (aLengthFormatter’s stringFromValue:aMeters unit:(current application’s NSLengthFormatterUnitMillimeter)) as text –> "5,000 mm" set res4 to (aLengthFormatter’s stringFromValue:aMeters unit:(current application’s NSLengthFormatterUnitCentimeter)) as text –> "5,000 cm" set res5 to (aLengthFormatter’s stringFromValue:aMeters unit:(current application’s NSLengthFormatterUnitMeter)) as text –> "5,000 m" set res6 to (aLengthFormatter’s stringFromValue:aMeters unit:(current application’s NSLengthFormatterUnitKilometer)) as text –> "5,000 km" set res7 to (aLengthFormatter’s stringFromValue:aMeters unit:(current application’s NSLengthFormatterUnitInch)) as text –> "5,000 in" set res8 to (aLengthFormatter’s stringFromValue:aMeters unit:(current application’s NSLengthFormatterUnitFoot)) as text –> "5,000 ft" set res9 to (aLengthFormatter’s stringFromValue:aMeters unit:(current application’s NSLengthFormatterUnitYard)) as text –> "5,000 yd" set res10 to (aLengthFormatter’s stringFromValue:aMeters unit:(current application’s NSLengthFormatterUnitMile)) as text –> "5,000 mi" |
AppleScript名:NSMassFormatterのじっけん |
— Created 2015-11-18 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –http://dev.classmethod.jp/references/ios8-new-formatter/ set aMasFormatter to current application’s NSMassFormatter’s alloc()’s init() set aKilloGrams to 60.0 set aRes to (aMasFormatter’s stringFromKilograms:aKilloGrams) as text –> "60 kg" set a2Res to (aMasFormatter’s stringFromKilograms:(0.5)) as text –> "500 g" set a3Res to (aMasFormatter’s stringFromKilograms:(5000000)) as text –> "5,000,000 kg" set a4Res to (aMasFormatter’s stringFromKilograms:("0.0005" as real)) as text –> "0.5 g" set bRes to (aMasFormatter’s stringFromValue:aKilloGrams unit:(current application’s NSMassFormatterUnitGram)) as text –> "60 g" set cRes to (aMasFormatter’s stringFromValue:aKilloGrams unit:(current application’s NSMassFormatterUnitKilogram)) as text –> "60 kg" set dRes to (aMasFormatter’s stringFromValue:aKilloGrams unit:(current application’s NSMassFormatterUnitOunce)) as text –> "60 oz" set eRes to (aMasFormatter’s stringFromValue:aKilloGrams unit:(current application’s NSMassFormatterUnitPound)) as text –> "60 lb" set fRes to (aMasFormatter’s stringFromValue:aKilloGrams unit:(current application’s NSMassFormatterUnitStone)) as text –> "60 st" set gRes to (aMasFormatter’s unitStringFromValue:aKilloGrams unit:(current application’s NSMassFormatterUnitGram)) as text –> "g" set hRes to (aMasFormatter’s unitStringFromValue:aKilloGrams unit:(current application’s NSMassFormatterUnitKilogram)) as text –> "kg" set iRes to (aMasFormatter’s unitStringFromValue:aKilloGrams unit:(current application’s NSMassFormatterUnitOunce)) as text –> "oz" set jRes to (aMasFormatter’s unitStringFromValue:aKilloGrams unit:(current application’s NSMassFormatterUnitPound)) as text –> "lb" set kRes to (aMasFormatter’s unitStringFromValue:aKilloGrams unit:(current application’s NSMassFormatterUnitStone)) as text –> "st" set res01 to aMasFormatter’s getObjectValue:(missing value) forString:fRes errorDescription:(missing value) –> false set res02 to (aMasFormatter’s stringFromKilograms:80) as text –> "80 kg" set res03 to aMasFormatter’s unitStyle() –> 2 = NSFormattingUnitStyleMedium |
そこで、冒頭に紹介したようにDDUnitConverterを導入してみたわけですが、これはこれで問題がないわけでもありません。本来、通貨同士の変換をサポートしており、通貨レートの更新機能を持っているのですが、実際に呼び出してみると更新されていない雰囲気が濃厚です(呼び出し方を間違っているのか?)。
なので、「通貨レート以外」の度量衡変換にのみ用いるのが安全な使い方なのかもしれない、というところです。
例によって、OS X 10.10以降用にビルドしたバイナリを用意しておきましたので、興味のある方はアーカイブを展開したあとで、~/Library/Frameworksに入れておためしください。
–> Download ddUnitConversionKit.framework(To ~/Library/Frameworks/)
AppleScript名:DDUnitConverterのじっけん1 |
— Created 2015-11-19 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version “2.4” use scripting additions use framework “Foundation” use framework “ddUnitConversionKit” –davedelong/DDUnitConverter –https://github.com/davedelong/DDUnitConverter –enums in DDVelocityUnitConverter.h property DDVelocityUnitCentimetersPerHour : 0 property DDVelocityUnitCentimetersPerMinute : 1 property DDVelocityUnitCentimetersPerSecond : 2 property DDVelocityUnitFeetPerHour : 3 property DDVelocityUnitFeetPerMinute : 4 property DDVelocityUnitFeetPerSecond : 5 property DDVelocityUnitInchesPerHour : 6 property DDVelocityUnitInchesPerMinute : 7 property DDVelocityUnitInchesPerSecond : 8 property DDVelocityUnitKilometersPerHour : 9 property DDVelocityUnitKilometersPerMinute : 10 property DDVelocityUnitKilometersPerSecond : 11 property DDVelocityUnitKnots : 12 property DDVelocityUnitLight : 13 property DDVelocityUnitMach : 14 property DDVelocityUnitMetersPerHour : 15 property DDVelocityUnitMetersPerMinute : 16 property DDVelocityUnitMetersPerSecond : 17 property DDVelocityUnitMilesPerHour : 18 property DDVelocityUnitMilesPerMinute : 19 property DDVelocityUnitMilesPerSecond : 20 property DDVelocityUnitFurlongsPerFortnight : 21 –時速100kmを秒速kmに変換 set aVal to ((current application’s DDUnitConverter’s velocityUnitConverter())’s convertNumber:100 fromUnit:(DDVelocityUnitKilometersPerHour) toUnit:(DDVelocityUnitKilometersPerSecond)) as real –> 0.027777777778 |
AppleScript名:DDCurrencyUnitConverterのじっけん |
— Created 2015-11-19 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version “2.4” use scripting additions use framework “Foundation” use framework “ddUnitConversionKit” –davedelong/DDUnitConverter –https://github.com/davedelong/DDUnitConverter –enums in DDCurrencyUnitConverter.h property DDCurrencyUnitEuro : 0 property DDCurrencyUnitJapaneseYen : 1 property DDCurrencyUnitUKPoundSterling : 2 property DDCurrencyUnitUSDollar : 3 property DDCurrencyUnitAlgerianDinar : 4 property DDCurrencyUnitArgentinePeso : 5 property DDCurrencyUnitAustralianDollar : 6 property DDCurrencyUnitBahrainDinar : 7 property DDCurrencyUnitBotswanaPula : 8 property DDCurrencyUnitBrazilianReal : 9 property DDCurrencyUnitBruneiDollar : 10 property DDCurrencyUnitCanadianDollar : 11 property DDCurrencyUnitChileanPeso : 12 property DDCurrencyUnitChineseYuan : 13 property DDCurrencyUnitColombianPeso : 14 property DDCurrencyUnitCzechKoruna : 15 property DDCurrencyUnitDanishKrone : 16 property DDCurrencyUnitHungarianForint : 17 property DDCurrencyUnitIcelandicKrona : 18 property DDCurrencyUnitIndianRupee : 19 property DDCurrencyUnitIndonesianRupiah : 20 property DDCurrencyUnitIranianRial : 21 property DDCurrencyUnitIsraeliNewSheqel : 22 property DDCurrencyUnitKazakhstaniTenge : 23 property DDCurrencyUnitKoreanWon : 24 property DDCurrencyUnitKuwaitiDinar : 25 property DDCurrencyUnitLibyanDinar : 26 property DDCurrencyUnitMalaysianRinggit : 27 property DDCurrencyUnitMauritianRupee : 28 property DDCurrencyUnitMexicanPeso : 29 property DDCurrencyUnitNepaleseRupee : 30 property DDCurrencyUnitNewZealandDollar : 31 property DDCurrencyUnitNorwegianKrone : 32 property DDCurrencyUnitRialOmani : 33 property DDCurrencyUnitPakistaniRupee : 34 property DDCurrencyUnitNuevoSol : 35 property DDCurrencyUnitPhilippinePeso : 36 property DDCurrencyUnitPolishZloty : 37 property DDCurrencyUnitQatarRiyal : 38 property DDCurrencyUnitRussianRuble : 39 property DDCurrencyUnitSaudiArabianRiyal : 40 property DDCurrencyUnitSingaporeDollar : 41 property DDCurrencyUnitSouthAfricanRand : 42 property DDCurrencyUnitSriLankaRupee : 43 property DDCurrencyUnitSwedishKrona : 44 property DDCurrencyUnitSwissFranc : 45 property DDCurrencyUnitThaiBaht : 46 property DDCurrencyUnitTrinidadAndTobagoDollar : 47 property DDCurrencyUnitTunisianDinar : 48 property DDCurrencyUnitUAEDirham : 49 property DDCurrencyUnitPesoUruguayo : 50 property DDCurrencyUnitBolivarFuerte : 51 property DDCurrencyUnitSDR : 52 –最初に通貨レートの更新を行う必要があるが、実行しても変わらないのはなぜ???? set aConv to current application’s DDUnitConverter’s currencyUnitConverter() aConv’s refreshExchangeRates() set aVal to (aConv’s convertNumber:100 fromUnit:(DDCurrencyUnitUSDollar) toUnit:(DDCurrencyUnitJapaneseYen)) –> (NSNumber) 1.006839721743 |