AppleScript名:指定リストから、Regexpで要素を抽出(NSPredicate) |
— Created 2017-10-29 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set stringArray to current application’s NSArray’s arrayWithArray:{"adobe", "22", "microsoft", "99"} set thePred to current application’s NSPredicate’s predicateWithFormat:"self MATCHES ’\\\\d\\\\d’" set bList to (stringArray’s filteredArrayUsingPredicate:thePred) as list –> {"22", "99"} |
カテゴリー: Text
正規表現で数字を抽出(NSRegularExpressionSearch)
AppleScript名:正規表現で数字を抽出(NSRegularExpressionSearch) |
— Created 2017-12-17 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aStr to "126 ひよこ豆コーヒー豆だだちゃ豆" set n1Res to filterNumStr(aStr) of me log result –> "126" set aStr to "ひよこ豆コーヒー豆だだちゃ豆 345" set n2Res to filterNumStr(aStr) of me log result –> "345 set aStr to "ひよこ豆コーヒー豆999だだちゃ豆" set n3Res to filterNumStr(aStr) of me log result –> 999 on filterNumStr(aStr as string) set aLen to length of aStr set regStr to "\\d{1," & (aLen as string) & "}" set aRes to findStrByPattern(aStr, regStr) of me return aRes as {boolean, number} end filterNumStr on findStrByPattern(aText as string, regStr as string) set anNSString to current application’s NSString’s stringWithString:aText set aRange to anNSString’s rangeOfString:regStr options:(current application’s NSRegularExpressionSearch) if aRange = {location:0, length:0} then return "" set bStr to anNSString’s substringWithRange:aRange return bStr as string end findStrByPattern |
Numbers上で選択中の列のデータからIPアドレスを抽出(NSPredicate)
AppleScript名:Numbers上で選択中の列のデータからIPアドレスを抽出(NSPredicate) |
— Created 2018-01-03 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –http://piyocast.com/as/archives/5080 property NSPredicate : a reference to current application’s NSPredicate property NSArray : a reference to current application’s NSArray set aList to getSelectionDataFromNumbers() of me set anArray to NSArray’s arrayWithArray:aList set aPred to NSPredicate’s predicateWithFormat:"SELF MATCHES ’[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}’" set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list –> {"XX.XX.XX.XXX", "XXX.XX.XXX.XXX", "XXX.XXX.XX.XXX", …..} on getSelectionDataFromNumbers() tell application "Numbers" tell front document tell active sheet tell table 1 set aList to value of every cell of selection range end tell end tell end tell end tell return aList end getSelectionDataFromNumbers |
指数表示数値を文字列化
指数表示の数値を文字列化するAppleScriptです。
AppleScriptの数値変数で表現可能な範囲は割と狭く、±1.79769E308。実数部は9桁です。
そのため、不意に指数表示になってしまったデータから指数表現を解除して、数値文字列に変換し、適宜bcコマンドなどで数値文字列同士の演算を行うことがままあります。そういう場合に使用します。
AppleScript text item delimitersを活用していることから、あきらかに自分が組んだScriptではありません(なるべく避けるので)。たぶん、Mailing Listに流れていたScriptです。
AppleScript名:指数表示数値を文字列化 |
set longNumber to "1082204521" set exponent to longNumber as number –> 1.082204521E+9 set numberString to Stringify(exponent) 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 |
序数を求めるルーチン同士を比較
AppleScript名:序数を求めるルーチン同士を比較 |
repeat with i from 1 to 1000 set a to retOrdinalNumStr(i) of me set b to getOrdinalNumber(i) of me log {a, b} end repeat –数値を与えると序数の文字列を返す on retOrdinalNumStr(aNum) set aStr to aNum as string –下1桁の数字を取得 set last1Str to last character of aStr –下2桁目の数字を取得 if length of aStr > 1 then set last2Str to character -2 of aStr else set last2Str to "" end if –場合分け set retStr to "" if last1Str = "1" then if last2Str = "1" then set retStr to "th" –11 else set retStr to "st" end if else if last1Str = "2" then if last2Str = "1" then set retStr to "th" –12 else set retStr to "nd" end if else if last1Str = "3" then if last2Str = "1" then set retStr to "th" –13 else set retStr to "rd" end if else set retStr to "th" end if return aStr & retStr end retOrdinalNumStr — 序数表現に変更 on getOrdinalNumber(anyInteger) if (anyInteger mod 10) = 1 and (anyInteger mod 100) ≠ 11 then set tempList to anyInteger & "st" else if (anyInteger mod 10) = 2 and (anyInteger mod 100) ≠ 12 then set tempList to anyInteger & "nd" else if (anyInteger mod 10) = 3 and (anyInteger mod 100) ≠ 13 then set tempList to anyInteger & "rd" else set tempList to anyInteger & "th" end if return tempList as text end getOrdinalNumber |
文字で書いた数値を本物の数値に変換
仰々しくタイトルを書いていますが、なんのことはない文字で書いた数字文字列の数値へのcastを行う処理です。
AppleScriptのネイティブのやりかただと、
set a to "12345" set b to a as number
という程度ですみます(Adobe Illustratorへのtellブロック内で上記のScriptを実行しないでください。L*a*b*色空間のaとbの1文字の予約語を持っているので、動作がおかしくなります)。
本Scriptで紹介しているのは、Cocoaのオブジェクトの数値文字列をCocoaの数値オブジェクト(NSNumber)に変換するものです。
本来はNSNumberのままなのですが、Scripting Bridgeの仕様でNSNumberになったものは自動でAppleScriptの数値まで変換されます。
AppleScript名:文字で書いた数値を本物の数値に変換 |
— Created 2014-12-28 by Takaaki Naganoya — 2014 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set j to "123456789" set aStr to current application’s NSString’s stringWithString:j set aNum to aStr’s intValue() |
数値の桁数を求める v2
AppleScript名:数値の桁数を求める v2 |
— Created 2014-12-04 by Takaaki Naganoya — 2014 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use bPlus : script "BridgePlus" set aList to {} set aRes to 0 repeat with i from 1 to 9999 by 1 set d to getDigit(i) of me if aRes is not equal to d then set the end of aList to {i, d} set aRes to d end if end repeat aList –> {{1, 1}, {10, 2}, {100, 3}, {1000, 4}} –数値の桁数を求める(マイナスの数は想定外) on getDigit(aNum as integer) load framework set b to ((current application’s SMSForder’s log10ValueOf:aNum)) set c to b as real set d to round c rounding down return (d + 1) end getDigit |
BridgePlusがmacOS 10.15以降でうまく動かない環境もあるので、数値演算ライブラリcalcLibASを呼び出すものも掲載しておきます。
AppleScript名:数値の桁数を求める v3.scpt |
— – Created by: Takaaki Naganoya – Created on: 2020/06/30 — – Copyright © 2020 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" use scripting additions use framework "Foundation" use calSub : script "calcLibAS" –http://piyocast.com/as/asinyaye set aRes1 to getNumDigit(-9999) of me –> 3 set aRes2 to getNumDigit(100) of me –> 2 on getNumDigit(aNum) set a2Val to abs aNum return round (log10 a2Val) rounding down end getNumDigit |
数値にゼロパディングしたテキストを返す
AppleScript名:数値にゼロパディングしたテキストを返す |
set aNum to 1 set aStr to retZeroPaddingText(aNum, 4) of me –数値にゼロパディングしたテキストを返す on retZeroPaddingText(aNum, aLen) set tText to ("0000000000" & aNum as text) set tCount to length of tText set resText to text (tCount – aLen + 1) thru tCount of tText return resText end retZeroPaddingText |
数値を指定桁でゼロパディング
AppleScript名:数値を指定桁でゼロパディング |
— Created 2015-08-05 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aTEXT to numToZeroPaddingStr(99, 4, "0") –> "0099" set aTEXT to numToZeroPaddingStr(99999999, 20, "0") –> "00000000000099999999" –整数の値に指定桁数ゼロパディングして文字列で返す on numToZeroPaddingStr(aNum as integer, aDigit as integer, paddingChar as text) set aNumForm to current application’s NSNumberFormatter’s alloc()’s init() aNumForm’s setPaddingPosition:(current application’s NSNumberFormatterPadBeforePrefix) aNumForm’s setPaddingCharacter:paddingChar aNumForm’s setMinimumIntegerDigits:aDigit set bNum to current application’s NSNumber’s numberWithInt:aNum set aStr to aNumForm’s stringFromNumber:bNum return aStr as text end numToZeroPaddingStr |
ローマ数字エンコーダー、デコーダー v2
AppleScript名:ローマ数字エンコーダー、デコーダー v2 |
— Created 2015-11-16 by Takaaki Naganoya — Modified 2017-05-18 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –http://stackoverflow.com/questions/14762282/how-to-convert-a-roman-numeral-to-an-nsnumer repeat with i from 1 to 3999 set aRes to numberToRomanNumerals(i) of me set bRes to romanNumeralsToNumber(aRes) of me log {i, aRes, bRes} end repeat –Roman Number String Encoder on numberToRomanNumerals(aNum) if (aNum < 0 or aNum > 3999) then return "" –条件が間違っていたので直した(2017/5/18) set r_ones to {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"} set r_tens to {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"} set r_hunds to {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"} set r_thouds to {"M", "MM", "MMM", "MMMM", "MMMMM", "MMMMMM", "MMMMMMM", "MMMMMMMM", "MMMMMMMMM"} set ones to aNum mod 10 set tens to (aNum – ones) mod 100 set hundreds to (aNum – tens – ones) mod 1000 set thou to (aNum – hundreds – tens – ones) mod 10000 set tens to tens / 10 set hundreds to hundreds / 100 set thou to thou / 1000 set rNum to current application’s NSString’s stringWithString:"" if thou > 0 then set rNum to rNum’s stringByAppendingString:(item thou of r_thouds) if hundreds > 0 then set rNum to rNum’s stringByAppendingString:(item hundreds of r_hunds) if tens > 0 then set rNum to rNum’s stringByAppendingString:(item tens of r_tens) if ones > 0 then set rNum to rNum’s stringByAppendingString:(item ones of r_ones) return rNum as text end numberToRomanNumerals –Roman Number String Decoder on romanNumeralsToNumber(str) — Piero Garzotto (http://scriptbuilders.net/files/romannumerals1.0.html) local str, r, i set r to 0 repeat with i from 1 to count str set r to r + (item (offset of (item i of str) in "mdclxvi") of ¬ {1000, 500, 100, 50, 10, 5, 1}) * (((((offset of ¬ (item (i + 1) of (str & "@")) in "mdclxvi@") ≥ (offset of ¬ (item i of str) in "mdclxvi")) as integer) * 2) – 1) end repeat return r end romanNumeralsToNumber |
数値を日本語文字列に
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ストーン |