AppleScript名:与えられたデータのうちIPアドレスとして妥当なもののみを抽出 |
— Created 2018-01-03 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSPredicate : a reference to current application’s NSPredicate property NSArray : a reference to current application’s NSArray set aaList to {"112.4.208.194", "17.20.30..12"} set aResList to {} repeat with i in aaList set j to contents of i set the end of aResList to chkIPAddressFormat(j) of me end repeat aResList on chkIPAddressFormat(aStr as string) set aList to {aStr} 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 return (length of bRes = 1) as boolean end chkIPAddressFormat |
タグ: 10.12savvy
指定リストから、Regexpで要素を抽出(NSPredicate)
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"} |
正規表現で数字を抽出(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 |
連番JPEGファイルを読み込んで連結したPDFを作成(新規作成)
AppleScript名:連番JPEGファイルを読み込んで連結したPDFを作成(新規作成) |
— Created 2016-09-20 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "QuartzCore" use framework "Quartz" use framework "AppKit" set aExt to ".jpg" set aFol to choose folder set fList to getFilePathList(aFol, aExt) of me set f2List to my sort1DList:fList ascOrder:true –sort by ascending set newFile to POSIX path of (choose file name with prompt "新規PDFファイルの名称を選択") set newFilePath to current application’s NSString’s stringWithString:newFile –Make Blank PDF set aPDFdoc to current application’s PDFDocument’s alloc()’s init() set pageNum to 0 repeat with i in f2List set j to contents of i set aURL to (current application’s |NSURL|’s fileURLWithPath:j) set bImg to (current application’s NSImage’s alloc()’s initWithContentsOfURL:aURL) (aPDFdoc’s insertPage:(current application’s PDFPage’s alloc()’s initWithImage:bImg) atIndex:pageNum) set pageNum to pageNum + 1 end repeat aPDFdoc’s writeToFile:newFilePath –ASOCで指定フォルダのファイルパス一覧取得(拡張子指定つき) on getFilePathList(aFol, aExt) set aPath to current application’s NSString’s stringWithString:(POSIX path of aFol) set aFM to current application’s NSFileManager’s defaultManager() set nameList to (aFM’s contentsOfDirectoryAtPath:aPath |error|:(missing value)) as list set anArray to current application’s NSMutableArray’s alloc()’s init() repeat with i in nameList set j to i as text if (j ends with aExt) and (j does not start with ".") then –exclude invisible files set newPath to (aPath’s stringByAppendingString:j) (anArray’s addObject:newPath) end if end repeat return anArray as list end getFilePathList –1D List(文字)をsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート on sort1DList:theList ascOrder:aBool set aDdesc to current application’s NSSortDescriptor’s sortDescriptorWithKey:"self" ascending:aBool selector:"localizedCaseInsensitiveCompare:" set theArray to current application’s NSArray’s arrayWithArray:theList return (theArray’s sortedArrayUsingDescriptors:{aDdesc}) as list end sort1DList:ascOrder: |
連番JPEGファイルを読み込んで連結したPDFを作成(既存のPDFに追加)
AppleScript名:連番JPEGファイルを読み込んで連結したPDFを作成(既存のPDFに追加) |
— Created 2016-09-20 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "QuartzCore" use framework "Quartz" use framework "AppKit" set aExt to ".jpg" set targAlias to retFrontFinderWindowsTargetIfExits(path to desktop) of me set aFol to choose folder with prompt "追記するJPEG画像ファイルが入っているフォルダを選択" default location targAlias set fList to getFilePathList(aFol, aExt) of me set f2List to my sort1DList:fList ascOrder:true –sort by ascending set newFile to POSIX path of (choose file of type {"com.adobe.pdf"} with prompt "既存のPDFファイルを選択(このPDF末尾に画像を追加)") set newFilePath to current application’s NSString’s stringWithString:newFile set newFileURL to current application’s |NSURL|’s fileURLWithPath:newFile –Get Exsisting PDF’s URL and Use it set aPDFdoc to current application’s PDFDocument’s alloc()’s initWithURL:newFileURL set pageNum to ((aPDFdoc’s pageCount()) as integer) repeat with i in f2List set j to contents of i set aURL to (current application’s |NSURL|’s fileURLWithPath:j) set bImg to (current application’s NSImage’s alloc()’s initWithContentsOfURL:aURL) (aPDFdoc’s insertPage:(current application’s PDFPage’s alloc()’s initWithImage:bImg) atIndex:pageNum) set pageNum to pageNum + 1 end repeat aPDFdoc’s writeToFile:newFilePath –ASOCで指定フォルダのファイルパス一覧取得(拡張子指定つき) on getFilePathList(aFol, aExt) set aPath to current application’s NSString’s stringWithString:(POSIX path of aFol) set aFM to current application’s NSFileManager’s defaultManager() set nameList to (aFM’s contentsOfDirectoryAtPath:aPath |error|:(missing value)) as list set anArray to current application’s NSMutableArray’s alloc()’s init() repeat with i in nameList set j to i as text if (j ends with aExt) and (j does not start with ".") then –exclude invisible files set newPath to (aPath’s stringByAppendingString:j) (anArray’s addObject:newPath) end if end repeat return anArray as list end getFilePathList –1D List(文字)をsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート on sort1DList:theList ascOrder:aBool set aDdesc to current application’s NSSortDescriptor’s sortDescriptorWithKey:"self" ascending:aBool selector:"localizedCaseInsensitiveCompare:" set theArray to current application’s NSArray’s arrayWithArray:theList return (theArray’s sortedArrayUsingDescriptors:{aDdesc}) as list end sort1DList:ascOrder: on retFrontFinderWindowsTargetIfExits(aDefaultLocation) tell application "Finder" set wCount to count every window if wCount ≥ 1 then tell front window set aTarg to target as alias end tell return aTarg else return aDefaultLocation end if end tell end retFrontFinderWindowsTargetIfExits |
指数表示数値を文字列化
指数表示の数値を文字列化する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名:真偽値の反転 |
set a to true
set b to not a –> false |
序数を求めるルーチン同士を比較
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 |
奇数かどうかチェック
AppleScript名:奇数かどうかチェック |
set a to 5 set aRes to chkOddNum(a) of me –> true set b to 8 set bRes to chkEvenNum(b) of me –> true –奇数かどうかチェック on chkOddNum(aNum) set a to aNum mod 2 if a = 1 then return true else return false end if end chkOddNum –偶数かどうかチェック on chkEvenNum(aNum) set a to aNum mod 2 if a = 0 then return true else return false end if end chkEvenNum |
文字で書いた数値を本物の数値に変換
仰々しくタイトルを書いていますが、なんのことはない文字で書いた数字文字列の数値への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 |
ABSを求める
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 |
同スコアを考慮した順位決定(汎用ルーチン)
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 |