AppleScript名:日本語的数値表現デコーダー v3.1 |
–課題:複数の小数点チェックが未実装 –set a to the clipboard set a to "1垓232京120兆1億822万10" set jRes to decodeJapaneseNumText(a) of japaneseNumberDecodingKit –> "102320120000108220000" –set the clipboard to jRes script japaneseNumberDecodingKit –日本語数値表現文字列を数字文字列にデコードする on decodeJapaneseNumText(a) set scaleList1 to {"万", "億", "兆", "京", "垓", "丈", "壌", "溝", "砂", "正", "載", "極", "恒河沙", "阿僧梢", "那由他", "不可思議", "無量大数"} set bList to splitByDigitText(a, scaleList1) of me –パースした最終アイテムにメジャー桁の桁リスト内の文字が入っているかをチェック(0〜9999までの桁を処理するため) set lastItem to contents of last item of bList set aRes to chkContainEachListItem(lastItem, scaleList1) of me if aRes = false then –bListの最終アイテムが0〜9999だった場合 set bbList to items 1 thru -2 of bList else –bListの最終アイテムはscaleList1の桁テキストを含んでいた(端数がなかった) set bbList to bList set lastItem to "0" end if –各桁の評価を行う実装試験 set allFormula to {} set scaleList2a to {"千", "百", "十"} set scaleList2b to {"1000", "100", "10"} set firstF to true repeat with i in bbList set anItem to contents of i as string set cList to splitByDigitText(anItem, scaleList2a) of me set tList to {"千", "百", "十"} set rList to {"*1000+", "*100+", "*10+"} set aa to repCharList(anItem, tList, rList) of me set aPart to text 1 thru -2 of aa set bPart to text -1 thru -1 of aa if aPart ends with "+" then set aPart to aPart & "0" end if set scaleList2 to {"10000", "100000000", "1000000000000", "10000000000000000", "100000000000000000000", "1000000000000000000000000", "10000000000000000000000000000", "100000000000000000000000000000000", "1000000000000000000000000000000000000", "10000000000000000000000000000000000000000", "100000000000000000000000000000000000000000000", "1000000000000000000000000000000000000000000000000", "10000000000000000000000000000000000000000000000000000", "100000000000000000000000000000000000000000000000000000000", "1000000000000000000000000000000000000000000000000000000000000", "10000000000000000000000000000000000000000000000000000000000000000", "100000000000000000000000000000000000000000000000000000000000000000000"} set aFormula to "(" & aPart & ")" & "*" & repCharList(bPart, scaleList1, scaleList2) of me if firstF then set allFormula to aFormula set firstF to false else set allFormula to allFormula & " + " & aFormula end if end repeat set allFormula to allFormula & " + " & lastItem –allFormula set cRes to calcByBC(allFormula) of me return cRes end decodeJapaneseNumText on splitByDigitText(aText, scaleList) set aList to words of aText if (count of aList) = 1 then return aText end if set calList to {} set posCount to 1 set startPointer to 1 set iCount to length of aList repeat with i from 1 to iCount set j to contents of item i of aList if j is in scaleList then set tmpItem to items startPointer thru i of aList set the end of calList to (tmpItem as string) set startPointer to i + 1 end if end repeat if (i + 1) is not equal to startPointer then set tmpItem to items startPointer thru -1 of aList set tmpItem to tmpItem as string set the end of calList to tmpItem end if return calList end splitByDigitText –Written By Philip Aker –文字置換ルーチン on repChar(origText, targStr, repStr) set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr} set temp to text items of origText set AppleScript’s text item delimiters to repStr set res to temp as text set AppleScript’s text item delimiters to txdl return res end repChar on calcByBC(aFormula) set yC to ASCII character 92 set aCMD to "echo \" scale=10; " & aFormula & "\" | bc" set aRes to do shell script aCMD return aRes end calcByBC –文字置換ルーチン リストにより複数のテキストを一括で置換 on repCharList(origText, targStrList, repStrList) set {tCount, rCount} to {length of targStrList, length of repStrList} –targStrListとrepStrListの項目数がイコールでなければfalseを返す if tCount is not equal to rCount then return false set origT to origText repeat with i from 1 to tCount set targStr to contents of (item i of targStrList) set repStr to contents of (item i of repStrList) set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr} set temp to text items of origT set AppleScript’s text item delimiters to repStr set res to temp as text set AppleScript’s text item delimiters to txdl set origT to res end repeat return res end repCharList –指定文字列の中に指定リストの各アイテムが含まれているかをチェック on chkContainEachListItem(chkStr, chkList) repeat with i in chkList set j to contents of i if chkStr contains j then return true end if end repeat return false end chkContainEachListItem end script |
タグ: 10.11savvy
NSByteCountFormatterCountStyleで数値から容量を表すテキストに変換
AppleScript名:NSByteCountFormatterCountStyleで数値から容量を表すテキストに変換 |
— Created 2014-12-03 by Takaaki Naganoya — 2014 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" –NSByteCountFormatterCountStyle property NSByteCountFormatterCountStyleFile : 0 property NSByteCountFormatterCountStyleMemory : 1 property NSByteCountFormatterCountStyleDecimal : 2 property NSByteCountFormatterCountStyleBinary : 3 set a to 100000 –ファイル容量 set b to current application’s NSByteCountFormatter’s stringFromByteCount:a countStyle:(current application’s NSByteCountFormatterCountStyleFile) set c to b as string log result –> "100 KB" –メモリー容量 set d to current application’s NSByteCountFormatter’s stringFromByteCount:a countStyle:(current application’s NSByteCountFormatterCountStyleMemory) set e to b as string log result –> "100 KB" –10進数 set f to current application’s NSByteCountFormatter’s stringFromByteCount:a countStyle:(current application’s NSByteCountFormatterCountStyleDecimal) set g to b as string log result –> "100 KB" –バイナリ? set h to current application’s NSByteCountFormatter’s stringFromByteCount:a countStyle:(current application’s NSByteCountFormatterCountStyleBinary) set i to b as string log result –> "100 KB" |
NSNumberFormatterのテスト v2
AppleScript名:NSNumberFormatterのテスト v2 |
— Created 2014-12-03 by Takaaki Naganoya — 2014 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" set a to 100000 set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterNoStyle) set c to b as string –> "100000" set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterDecimalStyle) set c to b as string –> "100,000" set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterCurrencyStyle) set c to b as string –> "¥100,000" set aa to 0.1 set b to current application’s NSNumberFormatter’s localizedStringFromNumber:aa numberStyle:(current application’s NSNumberFormatterPercentStyle) set c to b as string –> "10%" set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterScientificStyle) set c to b as string –> "1E5" set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterSpellOutStyle) set c to b as string –> "十万" set a to 111111111 set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterSpellOutStyle) set c to b as string –> "一億千百十一万千百十一" set a to 9.11111111111E+16 set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterSpellOutStyle) set c to b as string –> "一京八千十四兆三千九百八十五億九百四十八万千九百八十四" |
DDUnitConverterで時速を秒速に変換
–> ddUnitConversionKit.framework
AppleScript名:DDUnitConverterで時速を秒速に変換 |
— Created 2015-11-19 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "ddUnitConversionKit" –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 |
ポイント数値のテキストをmmに書き換える v4
AppleScript名:ポイント数値のテキストをmmに書き換える v4 |
set aRes to " 772.4 x 1082.8 pts" set sizeRes to changePTStoMM(aRes) of me log result –> {strDat:"272.6572 x 382.2284 mm", numDat:{272.6572, 382.2284}} set aRes to "595 x 842 pts (A4)" set sizeRes to changePTStoMM(aRes) of me log result –> {strDat:"210.035 x 297.226 mm", numDat:{210.035, 297.226}} set aRes to " 2242.2 x 1618.58 pts" set sizeRes to changePTStoMM(aRes) of me log result –> {strDat:"210.035 x 297.226 mm", numDat:{210.035, 297.226}} –ポイント数値のテキストをmmに書き換える v4 –問題が見つかったので書き換え(2010/3/12) –超泥縄式の対応だったので、「(A4)」の判型表示が行われないケースにはきちんと対応できていなかった –慌てて直した。ちゃんと文字と数字のアトリビュートを取得しているのでまじめに活用してみた on changePTStoMM(aText) set wList to words of aText set attrList to {} repeat with i in wList set the end of attrList to numChk(i) of me end repeat set aCount to length of wList set aReNumList to {} repeat with i from 1 to (aCount) set anItem to item i of wList set anAttr to item i of attrList if anAttr = true then set the end of aReNumList to (point2mm(anItem as number) of me) end if end repeat –エラー時の対応(多分ないと思うが、一応) if length of aReNumList is not equal to 2 then return false –まじめに数値データから文字列を組み立てて返す set num1 to contents of item 1 of aReNumList set num2 to contents of item 2 of aReNumList set aText to ((num1 as string) & " x " & num2 as string) & " mm" return {strDat:aText, numDat:aReNumList} end changePTStoMM –リストを任意のデリミタ付きでテキストに on makeStrFromListWithDelim(aDelim, aList) set aText to "" set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to aDelim set aText to aList as text set AppleScript’s text item delimiters to curDelim return aText end makeStrFromListWithDelim –ポイントからmmへの変換 on point2mm(a) return (a * 0.353) –1ポイント=0.353mm end point2mm –数値かどうか調べる on numChk(aNum) set aClass to (class of aNum) as string if aClass = "number" or aClass = "double" or aClass = "integer" or aClass = "real" then return true else if aClass = "string" or aClass = "text" or aClass = "unicode text" then try set bNum to aNum as number return true on error return false end try end if end numChk on trimStrFromTo(aStr, fromStr, toStr) –fromStrは前から探す if fromStr is not equal to "" then set sPos to (offset of fromStr in aStr) + 1 else set sPos to 1 end if –toStrは後ろから探す if toStr is not equal to "" then set b to (reverse of characters of aStr) as string set ePos to (offset of toStr in b) set ePos to ((length of aStr) – ePos) else set ePos to length of aStr end if set aRes to text sPos thru ePos of aStr return aRes end trimStrFromTo |
ローカライズドな期間表記
AppleScript名:ローカライズドな期間表記 |
— Created 2016-01-14 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –Date Interval set engNSDateIntervalFormatter to current application’s NSDateIntervalFormatter’s alloc()’s init() engNSDateIntervalFormatter’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"en-US") engNSDateIntervalFormatter’s setDateStyle:(current application’s NSDateIntervalFormatterLongStyle) engNSDateIntervalFormatter’s setDateTemplate:(current application’s NSDateFormatter’s alloc()’s init()’s setDateFormat:"yyyy-MM-dd HH:mm") engNSDateIntervalFormatter’s setTimeStyle:(current application’s NSDateIntervalFormatterNoStyle) engNSDateIntervalFormatter’s setTimeZone:(current application’s NSTimeZone’s timeZoneWithAbbreviation:"JST") engNSDateIntervalFormatter’s setCalendar:(current application’s NSCalendar’s currentCalendar()) set startDate to current application’s NSDate’s |date|() set endDate to current application’s NSDate’s dateWithTimeInterval:86400 sinceDate:startDate set outEString to (engNSDateIntervalFormatter’s stringFromDate:startDate toDate:endDate) as string –> "1/14/16 – 1/15/16"–ShortStyle –> "Jan 14 – 15, 2016"–Medium Style –> "January 14 – 15, 2016"–Long Style –> "Thursday, January 14 – Friday, January 15, 2016"–Full Style set jpnNSDateIntervalFormatter to current application’s NSDateIntervalFormatter’s alloc()’s init() jpnNSDateIntervalFormatter’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"ja") jpnNSDateIntervalFormatter’s setDateStyle:(current application’s NSDateIntervalFormatterFullStyle) jpnNSDateIntervalFormatter’s setDateTemplate:(current application’s NSDateFormatter’s alloc()’s init()’s setDateFormat:"yyyy-MM-dd HH:mm") jpnNSDateIntervalFormatter’s setTimeStyle:(current application’s NSDateIntervalFormatterNoStyle) jpnNSDateIntervalFormatter’s setTimeZone:(current application’s NSTimeZone’s timeZoneWithAbbreviation:"JST") jpnNSDateIntervalFormatter’s setCalendar:(current application’s NSCalendar’s currentCalendar()) set startDate to current application’s NSDate’s |date|() set endDate to current application’s NSDate’s dateWithTimeInterval:86400 sinceDate:startDate set outJString to (jpnNSDateIntervalFormatter’s stringFromDate:startDate toDate:endDate) as string –> "2016/01/14~2016/01/15"–ShortStyle –> "2016/01/14~2016/01/15"–Medium Style –> "2016/01/14~2016/01/15"–Long Style –> "2016/01/14(木曜日)~2016/01/15(金曜日)"–Full Style |
ローカライズドな度量衡表示(エネルギー)
AppleScript名:ローカライズドな度量衡表示(エネルギー) |
— 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キロカロリー" |
ローカライズドな度量衡表示(重さ)
AppleScript名:ローカライズドな度量衡表示(重さ) |
— 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ストーン |
ローカライズドな度量衡表示(長さ)
AppleScript名:ローカライズドな度量衡表示(長さ) |
— 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マイル" |
ローカライズドな度量衡変換(lengthFormatter)
AppleScript名:ローカライズドな度量衡変換(lengthFormatter) |
— Created 2015-11-20 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" set theNSMassFormatter to current application’s NSMassFormatter’s alloc()’s init() theNSMassFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"en-US") theNSMassFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong) (theNSMassFormatter’s stringFromKilograms:85) as list of string or string log result –> "187.393 pounds" (theNSMassFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitGram)) as list of string or string log result –> "1 gram" set theNSLengthFormatter to current application’s NSLengthFormatter’s alloc()’s init() theNSLengthFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"ja") theNSLengthFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong) (theNSLengthFormatter’s stringFromMeters:1) as list of string or string log result –> "100センチメートル"–We usually use "100cm" in Japanese. (theNSLengthFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitFoot)) as list of string or string log result –> "1フィート" |
液体の容積の単位変換
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 |
最小公倍数(LCM)を求める v3
AppleScript名:最小公倍数を求める v3 |
— Created 2017-05-20 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "primeNumKit" –https://github.com/NazarYavornytskyy/prime-number-objc set a1Num to 75 set b1Num to 315 set cRes to getLCMbetweenTwoNums(a1Num, b1Num) of me –> 1575 –2つの数の最小公倍数を求める on getLCMbetweenTwoNums(a1Num, b1Num) set aGCD to getGCDbetweenTwoNums(a1Num, b1Num) of me set a2Num to a1Num div aGCD set b2Num to b1Num div aGCD return (aGCD * a2Num * b2Num) end getLCMbetweenTwoNums –2つの数の最大公約数を求める on getGCDbetweenTwoNums(aNum, bNum) set aRes to getPrimeFactor(aNum) of me set bRes to getPrimeFactor(bNum) of me –2つのリストを集合として扱い、積集合を求める set aSet to current application’s NSMutableSet’s setWithArray:aRes set bSet to current application’s NSMutableSet’s setWithArray:bRes aSet’s intersectSet:bSet set cRes to aSet’s allObjects() as list set dRes to multipleListElements(cRes) of me return dRes end getGCDbetweenTwoNums –与えられた数を素因数分解する on getPrimeFactor(aTargNum) copy aTargNum to a set pFactList to {} repeat set pRes to checkPrimeNumber(a) of me if pRes = true then set the end of pFactList to a return pFactList end if set aFactor to checkFactor(a) of me set the end of pFactList to aFactor set a to a div aFactor end repeat end getPrimeFactor –素数チェック on checkPrimeNumber(aNum) if aNum < 10000 then –0.25秒以上かかりそうな処理はCocoaで処理(実測値ベース) return checkPrimeNumberSub1(aNum) of me else set aPrime to current application’s PrimeNumberHelper’s alloc()’s init() return (aPrime’s isPrime:aNum) as boolean end if end checkPrimeNumber –小さい数の場合の素数検出 on checkPrimeNumberSub1(aNum) repeat with i from 2 to (aNum div 2) if aNum mod i is equal to 0 then return i –false end if end repeat return true end checkPrimeNumberSub1 –素因数チェック on checkFactor(aNum) repeat with i from 2 to (aNum div 2) if aNum mod i is equal to 0 then return i end if end repeat end checkFactor –リスト内の要素をすべて乗算する on multipleListElements(aList) set aNum to 1 set aRes to 1 repeat with i in aList set aRes to aRes * (aNum * i) end repeat return aRes end multipleListElements |
最大公約数(GCD)を求めるv4
AppleScript名:最大公約数を求めるv4 |
— Created 2017-05-20 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "primeNumKit" –https://github.com/NazarYavornytskyy/prime-number-objc set aRes to getGCDbetweenTwoNums(75, 315) of me –> 15 –2つの数の最大公約数を求める on getGCDbetweenTwoNums(aNum, bNum) set aRes to getPrimeFactor(aNum) of me set bRes to getPrimeFactor(bNum) of me –2つのリストを集合として扱い、積集合を求める set aSet to current application’s NSMutableSet’s setWithArray:aRes set bSet to current application’s NSMutableSet’s setWithArray:bRes aSet’s intersectSet:bSet set cRes to aSet’s allObjects() as list set dRes to multipleListElements(cRes) of me return dRes end getGCDbetweenTwoNums –与えられた数を素因数分解する on getPrimeFactor(aTargNum) copy aTargNum to a set pFactList to {} repeat set pRes to checkPrimeNumber(a) of me if pRes = true then set the end of pFactList to a return pFactList end if set aFactor to checkFactor(a) of me set the end of pFactList to aFactor set a to a div aFactor end repeat end getPrimeFactor –素数チェック on checkPrimeNumber(aNum) if aNum < 10000 then –0.25秒以上かかりそうな処理はCocoaで処理(実測値ベース) return checkPrimeNumberSub1(aNum) of me else set aPrime to current application’s PrimeNumberHelper’s alloc()’s init() return (aPrime’s isPrime:aNum) as boolean end if end checkPrimeNumber –小さい数の場合の素数検出 on checkPrimeNumberSub1(aNum) repeat with i from 2 to (aNum div 2) if aNum mod i is equal to 0 then return i –false end if end repeat return true end checkPrimeNumberSub1 –素因数チェック on checkFactor(aNum) repeat with i from 2 to (aNum div 2) if aNum mod i is equal to 0 then return i end if end repeat end checkFactor –リスト内の要素をすべて乗算する on multipleListElements(aList) set aNum to 1 set aRes to 1 repeat with i in aList set aRes to aRes * (aNum * i) end repeat return aRes end multipleListElements |
Keynoteでタイトルがマスターに入っていなかった場合に修正 v2
Keynoteのタイトルがマスターに入っていなかった場合に、スライドの一番左上に存在するテキストアイテムをタイトルに入れ直すAppleScriptです。
Keynoteで急いでスライドを作ってはみたものの、あわててタイトルをマスタースライドのタイトルオブジェクトに入れていなかった場合に、(見た目は変わらないものの)あとで修正しておく必要があるケースがあります(Keynoteのデータを再利用するような場合とか)。
そこで、現在表示中のスライド中で一番左上に存在するテキストアイテムで、フォントサイズ20ポイント以上、文字色が黒 {0, 0, 0} のものをタイトルとみなして、文字を取得。あらためてマスタースライドのタイトルオブジェクトに文字を入れ直します。
このままだと、色データが{0,0,1}になったぐらいで検出できなくなってしまいますが、Color Domain処理を組み合わせることで「だいたい黒」「黒っぽい色」といった判定が行えるため、組み合わせて使うと効果的でしょう。
▲Before
▲After
AppleScript名:Keynoteでタイトルがマスターに入っていなかった場合に矯正 v2 |
use AppleScript version "2.4" use framework "Foundation" use scripting additions use bPlus : script "BridgePlus" property minTitleFontSize : 20 property targBaseSlide : "タイトル(上)" tell application "Keynote" tell front document set newMaster to master slide targBaseSlide tell current slide set curBaseName to name of base slide –現在のスライド上のすべてのテキストアイテムの座標{x ,y}を取得して最小(一番上)のものを推測 set pList to position of every text item set p2List to sort2DListAscending(pList) of me set mostUpPos to contents of first item of p2List –一番上に存在するテキストアイテムから情報を抽出 set tTextItem to first item of (every text item whose position is mostUpPos) tell tTextItem set tObjText to (object text) set aTitle to tObjText as string set aProp to size of (object text) set aColor to color of (object text) end tell –スライドの一番上に存在するテキストアイテムのフォントサイズが想定以上で、色が黒の場合には –マスタースライドのデフォルトタイトルに、一番上に存在していたテキストアイテムの本文を突っ込む if (aProp ≥ minTitleFontSize) and (aColor = {0, 0, 0}) then –色判定でColor Domain処理を行えば、「黒っぽい色」や「赤っぽい色」を対象にできる if curBaseName is not equal to targBaseSlide then set base slide to newMaster end if delete tTextItem set object text of default title item to aTitle end if end tell end tell end tell –2D Listのソート on sort2DListAscending(aList) load framework set sortIndexes to {0, 1} –Key Item id: begin from 0 set sortOrders to {true, true} –Ascending, Ascending set sortTypes to {"compare:", "compare:"} set bList to (current application’s SMSForder’s subarraysIn:(aList) sortedByIndexes:sortIndexes ascending:sortOrders sortTypes:sortTypes |error|:(missing value)) return bList as list of string or string end sort2DListAscending |
Finder Windowを円運動 v4
Finderのウィンドウ6つ(可変)を画面上で別々に楕円運動させるAppleScriptです。
三角関数の計算にShane StanleyのBridge Plus AppleScript Librariesを利用しています。
AppleScript名:Finder Windowを円運動 v4 |
— Created 2014-11-17 by Takaaki Naganoya — Modified 2018-02-23 by Takaaki Naganoya — 2014-2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" — for NSScreen use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html property aNum : 300 property aOffset : 0 property maxWin : 6 property winList : {} property randList : {} property winSize : 300 –Initialize load framework set winList to {} set randList to {} repeat maxWin times tell application "Finder" activate set aWin to make new Finder window tell aWin set toolbar visible to false set sidebar width to 0 set statusbar visible to false set position to {100, 100} set bounds to {100, 100, 100 + winSize, 100 + winSize} end tell set the end of winList to aWin set the end of randList to {random number from 0 to 400, random number from 0 to 400, random number from 1 to 360} end tell end repeat –Main repeat with i from 1 to 360 by 6 repeat with ii from 1 to maxWin set aWinObj to contents of item ii of winList set {aRandX, aRandY, aRandDegree} to item ii of randList set aDeg to i + aRandDegree set aSinNum to (current application’s SMSForder’s sinValueOf:aDeg) as real set aCosNum to (current application’s SMSForder’s cosValueOf:aDeg) as real set x to ((aNum * aSinNum) + aNum) * 2 + aOffset set y to (aNum * aCosNum) + aNum + aOffset ignoring application responses tell application "Finder" tell aWinObj set position to {(x as integer) + aRandX, (y as integer) + aRandY} end tell end tell end ignoring end repeat end repeat –Sweep tell application "Finder" repeat with i in winList tell i close end tell end repeat end tell |
Finder Windowを円運動 v1b
Finderのウィンドウ1つを画面上で楕円運動させるAppleScriptです。
三角関数の計算にShane StanleyのBridge Plus AppleScript Librariesを利用しています。
以前に掲載したバージョン(v1)から、BridgePlusを使うように変更しました。
自分が最初に見たAppleScriptが、「AppleScript道入門」に掲載されていたテキストエディタのウィンドウを画面の淵に沿わせて直線移動で1周させるものでした(それを見て「たいしたことはない」と思ってしまいましたが)。
いまでは、楕円軌道に沿ったウィンドウ移動もこのぐらいのスピードでできている、ということを実感できました。
AppleScript名:Finder Windowを円運動 v1b |
use AppleScript version "2.4" use scripting additions use framework "Foundation" use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html use framework "AppKit" — for NSScreen property aNum : 300 property aOffset : 0 load framework set {newX, newY} to (current application’s NSScreen’s mainScreen()’s frame()’s |size|()) as list tell application "Finder" set aWin to make new Finder window end tell repeat with i from 1 to 360 by 6 set |asin| to (current application’s SMSForder’s sinValueOf:i) as real set |acos| to (current application’s SMSForder’s cosValueOf:i) as real set x to ((aNum * |asin|) + aNum) * 2.0 + aOffset set y to (aNum * |acos|) + aNum + aOffset tell application "Finder" tell aWin set position to {x as integer, y as integer} end tell end tell end repeat tell application "Finder" tell aWin close end tell end tell |
テーマを選択してKeynoteの新規ドキュメントを作成
AppleScript名:テーマを選択してKeynoteの新規ドキュメントを作成 |
tell application "Keynote" set tList to name of every theme set themeName to first item of (choose from list tList) set newDoc to make new document with properties {document theme:theme themeName} end tell |
Keynote 6.6でドキュメントを新規作成して指定可能なマスタースライド一覧を取得
AppleScript名:Keynote 6.6でドキュメントを新規作成して指定可能なマスタースライド一覧を取得 |
— Created 2015-10-27 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" tell application "Keynote" –Keynoteの用語辞書のサンプルどおりに書いても動かない!!(オブジェクト名がローカライズされているので、テーマ名が言語環境依存) set aDoc to (make new document with properties {document theme:theme "ホワイト", width:1920, height:1080}) tell aDoc set masList to name of every master slide –> {"タイトル & サブタイトル", "画像(横長)", "タイトル(中央)", "画像(縦長)", "タイトル(上)", "タイトル & 箇条書き", "タイトル、箇条書き、画像", "箇条書き", "画像(3 点)", "引用", "写真", "空白"} end tell end tell |
Keynoteで現在表示中のスライド上にある表のカラム幅を自動調整
▲Before
▲After
AppleScript名:Keynoteで現在表示中のスライド上にある表のカラム幅を自動調整 v2 |
— Created 2017-10-06 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" tell application "Keynote" tell front document tell current slide set tCount to count every table if tCount = 0 then return tell table 1 set cCount to count every column set cWidth to width of every column set aWidth to width –table width set aveWidth to (aWidth – (first item of cWidth)) / (cCount – 1) tell columns 2 thru cCount set width to aveWidth end tell end tell end tell end tell end tell |
Keynoteで2D円グラフを作成
Keynoteの現在の書類上に2D円グラフを作成するAppleScriptです。
その他、さまざまなKeynote上で作成可能なグラフを作成するAppleScriptについては、電子書籍「Keynote Control with AppleScript」❶❷で解説しています。
AppleScript名:Keynoteで2D円グラフを作成 |
–set aRec to {kanjiNum:24960, kanjiRating:28.0, hiraganaNum:56080, hiraganaRating:62.9, katakanaNum:1063, katakanaRating:1.2, otherNum:7136, otherRating:8.0} –文学(坊ちゃん) –set aRec to {kanjiNum:563289, kanjiRating:22.0, hiraganaNum:1311933, hiraganaRating:51.2, katakanaNum:210161, katakanaRating:8.2, otherNum:478690, otherRating:18.7, totalCount:2564073}–異世界はスマートフォンとともに set aRec to {kanjiNum:364870, kanjiRating:26.7, hiraganaNum:682261, hiraganaRating:50.0, katakanaNum:89109, katakanaRating:6.6, otherNum:230374, otherRating:16.9, totalCount:1366614} –Knight’s and Magic set cCount to (kanjiNum of aRec) set hCount to (hiraganaNum of aRec) set kCount to (katakanaNum of aRec) set oCount to (otherNum of aRec) tell application "Keynote" set aDoc to (make new document with properties {document theme:theme "ホワイト", width:1024, height:768}) –Normal Slide –set aDoc to (make new document with properties {document theme:theme "ホワイト", width:1920, height:1080})–Wide Slide tell front document set aSlide to make new slide at after (first slide) with properties {base slide:master slide "タイトル(上)"} tell aSlide set object text of default title item to "文字種別使用比率" –add chart row names {"文字種別"} column names {"漢字", "ひらがな", "カタカナ", "その他"} data {{cCount, hCount, kCount, oCount}} type pie_2d –group by chart row add chart row names {"文字種別"} column names {"Kanji", "Hiragana", "Katakana", "Other"} data {{cCount, hCount, kCount, oCount}} type pie_2d –group by chart row end tell end tell end tell |