AppleScript名:ABSを求める |
set x to -44 set x to absNum(x) –> 44 on absNum(q) if q is less than 0 then set q to –q return q end absNum |
カテゴリー: Number
同スコアを考慮した順位決定(汎用ルーチン)
AppleScript名:同スコアを考慮した順位決定(汎用ルーチン) |
use AppleScript version "2.4" use scripting additions set rList to {{"近 装甲強化型ジム COST: 200", 60}, {"遠 ジム・キャノン COST: 160", 39}, {"近 ジム・コマンド COST: 200", 32}, {"近 ジム(WD隊) COST: 160", 26}, {"近 陸戦型ガンダム COST: 220", 23}, {"近 ジム改 COST: 240", 22}, {"遠 ガンタンク COST: 200", 21}, {"格 ジム(指揮官機) COST: 160", 20}, {"近 ジム COST: 120", 19}, {"格 陸戦型ジム COST: 120", 10}, {"近 ジム・トレーナー COST: 120", 9}, {"射 ジム・スナイパーII(WD隊) COST: 220", 8}, {"射 陸戦型ガンダム(ジム頭) COST: 200", 7}, {"格 ガンダム COST: 280", 6}, {"近 ザクII(F2) COST: 160", 6}, {"格 ジム・ストライカー COST: 180", 4}, {"近 ジム・寒冷地仕様 COST: 200", 4}, {"狙 ジム・スナイパーカスタム COST: 200", 3}, {"近 アクア・ジム COST: 160", 2}, {"遠 量産型ガンタンク COST: 160", 2}, {"格 ガンキャノン重装型 COST: 160", 1}, {"近 ジム・コマンドライトアーマー COST: 160", 1}, {"射 ガンキャノン COST: 200", 1}, {"格 ボールK型 COST: 120", 0}, {"射 デザート・ジム COST: 160", 0}} set rRes to retRankingList(rList) of me –> {{rank:1, aCon:"近 装甲強化型ジム COST: 200", aTimes:60}, {rank:2, aCon:"遠 ジム・キャノン COST: 160", aTimes:39}, {rank:3, aCon:"近 ジム・コマンド COST: 200", aTimes:32}, {rank:4, aCon:"近 ジム(WD隊) COST: 160", aTimes:26}, {rank:5, aCon:"近 陸戦型ガンダム COST: 220", aTimes:23}, {rank:6, aCon:"近 ジム改 COST: 240", aTimes:22}, {rank:7, aCon:"遠 ガンタンク COST: 200", aTimes:21}, {rank:8, aCon:"格 ジム(指揮官機) COST: 160", aTimes:20}, {rank:9, aCon:"近 ジム COST: 120", aTimes:19}, {rank:10, aCon:"格 陸戦型ジム COST: 120", aTimes:10}, {rank:11, aCon:"近 ジム・トレーナー COST: 120", aTimes:9}, {rank:12, aCon:"射 ジム・スナイパーII(WD隊) COST: 220", aTimes:8}, {rank:13, aCon:"射 陸戦型ガンダム(ジム頭) COST: 200", aTimes:7}, {rank:14, aCon:"格 ガンダム COST: 280", aTimes:6}, {rank:14, aCon:"近 ザクII(F2) COST: 160", aTimes:6}, {rank:16, aCon:"格 ジム・ストライカー COST: 180", aTimes:4}, {rank:16, aCon:"近 ジム・寒冷地仕様 COST: 200", aTimes:4}, {rank:18, aCon:"狙 ジム・スナイパーカスタム COST: 200", aTimes:3}, {rank:19, aCon:"近 アクア・ジム COST: 160", aTimes:2}, {rank:19, aCon:"遠 量産型ガンタンク COST: 160", aTimes:2}, {rank:21, aCon:"格 ガンキャノン重装型 COST: 160", aTimes:1}, {rank:21, aCon:"近 ジム・コマンドライトアーマー COST: 160", aTimes:1}, {rank:21, aCon:"射 ガンキャノン COST: 200", aTimes:1}, {rank:24, aCon:"格 ボールK型 COST: 120", aTimes:0}, {rank:24, aCon:"射 デザート・ジム COST: 160", aTimes:0}} on retRankingList(rList) set aText to {} –出力用リスト set aCount to 0 set prevPoint to -1 set buffCount to 0 repeat with i in rList copy i to {j, bCount} if prevPoint = bCount then –同じポイントが続いた場合 if buffCount is not equal to 0 then –同じポイントが複数回(2回以上)続いている場合 set buffCount to buffCount + 1 log {"case A:", buffCount, aCount, bCount, prevPoint} else –同じポイントが続いている場合(2回目) set buffCount to 1 log {"case B:", buffCount, aCount, bCount, prevPoint} end if else –同じポイントが続いていない場合 if buffCount is not equal to 0 then –直前まで同じポイントが続いていた場合 set aCount to aCount + buffCount + 1 –バッファしておいたカウントを出力する set buffCount to 0 log {"case C:", buffCount, aCount, bCount, prevPoint} else –通常パターン(直前まで同じポイントが続いていたりしない) set buffCount to 0 set aCount to aCount + 1 –ランキング順位を+1 log {"case D:", buffCount, aCount, bCount, prevPoint} end if end if set the end of aText to {rank:aCount, aCon:j as string, aTimes:bCount} copy bCount to prevPoint end repeat return aText end retRankingList –リストを任意のデリミタ付きでテキストに on retArrowText(aList, aDelim) 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 retArrowText |
平方根(SQR)を求める
平方根(SQR)を求める処理です。このほか、JavaScript Coreで関数計算を行わせるライブラリ「calcLibAS」を用いる方法があります。
AppleScript名:平方根(SQR)を求める |
set a to getSQR(4) of me –> 2.0 –平方根を求める on getSQR(aNum) return (aNum ^ 0.5) end getSQR |
数値を日本語文字列に
AppleScript名:数値を日本語文字列に |
— Created 2016-11-13 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set theValue to (current application’s NSNumberFormatter’s localizedStringFromNumber:287654321 numberStyle:(current application’s NSNumberFormatterSpellOutStyle)) as string –> "二億八千七百六十五万四千三百二十一" |
日本語的数値表現エンコーダーv2
AppleScript名:日本語的数値表現エンコーダーv2 |
–課題:オーバーフローチェックを行っていない set a to "102320120000108220010" set jRes to encodeJapaneseNumText(a) of japaneseNumberEncodingKit –> "1垓232京120兆1億822万10" script japaneseNumberEncodingKit –数字文字列を日本語数値表現文字列に変換 on encodeJapaneseNumText(aNum) set aText to Stringify(aNum) of me set aText to aText as Unicode text set dotText to "." as Unicode text set upperDigit to "" set lowerDigit to "" –小数点の処理 if dotText is in aText then set b to offset of dotText in aText set upperDigit to characters 1 thru (b – 1) of aText set upperDigit to upperDigit as Unicode text set lowerDigit to characters b thru -1 of aText set lowerDigit to lowerDigit as Unicode text else set upperDigit to aText end if set scaleList3 to {"", "万", "億", "兆", "京", "垓", "丈", "壌", "溝", "砂", "正", "載", "極", "恒河沙", "阿僧梢", "那由他", "不可思議", "無量大数"} set splitDigit to 4 set nList to splitByDigit(upperDigit, splitDigit) of me set nList to reverse of nList set resText to "" set digCount to 1 repeat with i in nList set b to (contents of i) as number if b is not equal to 0 then set resText to (b as text) & item digCount of scaleList3 & resText end if set digCount to digCount + 1 end repeat return resText & lowerDigit end encodeJapaneseNumText –指定桁数で区切る on splitByDigit(a, splitDigit) set aList to characters of a set aList to reverse of aList log aList set resList to {} set tempT to "" set tempC to 1 repeat with i in aList set tempT to contents of i & tempT if tempC mod splitDigit = 0 then set resList to {tempT} & resList set tempT to "" end if set tempC to tempC + 1 end repeat if tempT is not equal to "" then set resList to {tempT} & resList end if resList end splitByDigit on Stringify(x) — for E+ numbers set x to x as string set {tids, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, {"E+"}} if (count (text items of x)) = 1 then set AppleScript’s text item delimiters to tids return x else set {n, z} to {text item 1 of x, (text item 2 of x) as integer} set AppleScript’s text item delimiters to tids set i to character 1 of n set decSepChar to character 2 of n — "." or "," set d to text 3 thru -1 of n set l to count d if l > z then return (i & (text 1 thru z of d) & decSepChar & (text (z + 1) thru -1 of d)) else repeat (z – l) times set d to d & "0" end repeat return (i & d) end if end if end Stringify end script |
日本語的数値表現デコーダー v3.1
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 |
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 |
数値に対して、小数点以下の数値の切り上げ、切り下げ
AppleScript名:数値に対して、小数点以下の数値の切り上げ、切り下げ |
— Created 2017-08-12 by Takaaki Naganoya — Modified 2017-08-28 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSString : a reference to current application’s NSString property NSNumber : a reference to current application’s NSNumber property NSNumberFormatter : a reference to current application’s NSNumberFormatter property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp property NSNumberFormatterRoundDown : a reference to current application’s NSNumberFormatterRoundDown set a to 0.909661 set a1Res to roundingDownNumStr(a, 2) of me –> 0.9 on roundingDownNumStr(aNum as string, aDigit as integer) set a to NSString’s stringWithString:aNum set aa to a’s doubleValue() set aFormatter to NSNumberFormatter’s alloc()’s init() aFormatter’s setMaximumFractionDigits:aDigit aFormatter’s setRoundingMode:(NSNumberFormatterRoundDown) set aStr to aFormatter’s stringFromNumber:aa return (aStr as text) as real end roundingDownNumStr on roundingUpNumStr(aNum as string, aDigit as integer) set a to NSString’s stringWithString:aNum set aa to a’s doubleValue() set aFormatter to NSNumberFormatter’s alloc()’s init() aFormatter’s setMaximumFractionDigits:aDigit aFormatter’s setRoundingMode:(NSNumberFormatterRoundUp) set aStr to aFormatter’s stringFromNumber:aa return (aStr as text) as real end roundingUpNumStr |
数値文字列に対して、少数点以下の数値の切り上げ、切り下げ v2
AppleScript名:数値文字列に対して、少数点以下の数値の切り上げ、切り下げ v2 |
— Created 2017-08-12 by Takaaki Naganoya — Modified 2017-08-28 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSString : a reference to current application’s NSString property NSNumber : a reference to current application’s NSNumber property NSNumberFormatter : a reference to current application’s NSNumberFormatter property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp property NSNumberFormatterRoundDown : a reference to current application’s NSNumberFormatterRoundDown set a to "0.9096617698669434" set a1Res to roundingDownNumStr(a, 2) of me set b to "0.0001830748806241899" set b1Res to roundingDownNumStr(b, 2) of me return {a1Res, b1Res} –> {0.9, 0.0} on roundingDownNumStr(aNum as string, aDigit as integer) set a to NSString’s stringWithString:aNum set aa to a’s doubleValue() set aFormatter to NSNumberFormatter’s alloc()’s init() aFormatter’s setMaximumFractionDigits:aDigit aFormatter’s setRoundingMode:(NSNumberFormatterRoundDown) set aStr to aFormatter’s stringFromNumber:aa return (aStr as text) as real end roundingDownNumStr on roundingUpNumStr(aNum as string, aDigit as integer) set a to NSString’s stringWithString:aNum set aa to a’s doubleValue() set aFormatter to NSNumberFormatter’s alloc()’s init() aFormatter’s setMaximumFractionDigits:aDigit aFormatter’s setRoundingMode:(NSNumberFormatterRoundUp) set aStr to aFormatter’s stringFromNumber:aa return (aStr as text) as real end roundingUpNumStr |