AppleScript名:指定ファイルをFinderで選択表示_asoc |
— Created 2016-10-31 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" set aFile to POSIX path of (choose file) set pathStr to current application’s NSString’s stringWithString:aFile set parentPath to pathStr’s stringByDeletingLastPathComponent() set aRes to current application’s NSWorkspace’s sharedWorkspace()’s selectFile:pathStr inFileViewerRootedAtPath:parentPath |
Appleのハードウェアのアイコン名を一覧から選択して返す
AppleScript名:Appleのハードウェアのアイコン名を一覧から選択して返す |
— Created 2017-07-29 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aRes to chooseHardwareModel() of me on chooseHardwareModel() set sRes to (do shell script "ls " & (quoted form of "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/") & "com.apple.*.icns") set aList to paragraphs of sRes set aaList to choose from list aList if aaList = false then return set aPath to first item of aaList set aStr to ((current application’s NSString’s stringWithString:aPath)’s lastPathComponent()’s stringByDeletingPathExtension()) as string return aStr end chooseHardwareModel |
shell環境変数を取得する
do shell scriptコマンドでシェルコマンドが実行される場合の環境変数の確認は、1コマンドで実行できます。Terminal.app上で実行するときと初期条件が異なるので、必要に応じて環境変数の設定が必要です。
do shell script "env"
★Click Here to Open This Script
これが、Cocoaの機能だとどういう条件で実行されるのか、というのがこのScriptを書いた原因です。動かしてみて、「ああ、do shell scriptコマンドと同じなんだね」ということが理解できました。
AppleScript名:shell環境変数を取得する |
— Created 2016-03-16 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" current application’s NSProcessInfo’s processInfo()’s environment() –> (NSDictionary) {PATH:"/usr/bin:/bin:/usr/sbin:/sbin", TMPDIR:"/var/folders/h4/jfhlwst88xl9z0001s7k9vk00000gr/T/", LOGNAME:"maro", XPC_FLAGS:"0x0", HOME:"/Users/me", Apple_PubSub_Socket_Render:"/private/tmp/com.apple.launchd.KvufhRIpUw/Render", USER:"me", SSH_AUTH_SOCK:"/private/tmp/com.apple.launchd.3B8HzKhUz5/Listeners", DISPLAY:"/private/tmp/com.apple.launchd.el9lFx0WpV/org.macosforge.xquartz:0", XPC_SERVICE_NAME:"au.com.myriad-com.ASObjC-Explorer-4.1891872", SHELL:"/bin/bash", __CF_USER_TEXT_ENCODING:"0x1F8:0x1:0xE"} |
ベクトルの長さを求める
AppleScript名:ベクトルの長さを求める |
set vL to getVectorLength(5, 2) of me
–ベクトルの長さを求める on getVectorLength(a, b) return getSQR(a ^ 2 + b ^ 2) of me end getVectorLength –平方根を求める on getSQR(aNum) return (aNum ^ 0.5) end getSQR |
MatRによる行列計算テスト v4
–> download sample data ”matrix.txt”
AppleScript名:MatRによる行列計算テスト v4 |
— Created 2017-05-15 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "DCMatrix" –https://github.com/davidcox95/MatR –タブ区切りテキストファイルからMatrixを作る set aPosixFile to POSIX path of (choose file) set aMatrix to current application’s |Matrix|’s alloc()’s initWithContentsFromFile:aPosixFile aMatrix’s |print|() (* 5 6 7 8 3 4 5 6 1 2 3 4 *) |
MatRによる行列計算テスト v3
AppleScript名:MatRによる行列計算テスト v3 |
— Created 2017-05-15 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "DCMatrix" –https://github.com/davidcox95/MatR –listからMatrixを作る set mList to {{1, 2, 3, 4}, {11, 12, 13, 14}, {21, 22, 23, 24}} set aMatrix to current application’s |Matrix|’s alloc()’s initWithArray:mList andRows:3 byColumns:4 aMatrix’s |print|() (* 1 2 3 4 11 12 13 14 21 22 23 24 *) set a1Res to aMatrix’s |transpose|() (a1Res’s |print|()) (* 1 11 21 2 12 22 3 13 23 4 14 24 *) |
MatRによる行列計算テスト v2
AppleScript名:MatRによる行列計算テスト v2 |
— Created 2017-05-15 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "DCMatrix" –https://github.com/davidcox95/MatR –行列データをdoubleのListに set aMatrix to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:5) andRows:3 byColumns:4 set aList to doubleListFromMatrix(aMatrix) of me –> {{5.0, 5.0, 5.0, 5.0}, {5.0, 5.0, 5.0, 5.0}, {5.0, 5.0, 5.0, 5.0}} –行列データをintegerのListに set bList to intListFromMatrix(aMatrix) of me –> {{5, 5, 5, 5}, {5, 5, 5, 5}, {5, 5, 5, 5}} –行列データをstringのListに set cList to strListFromMatrix(aMatrix) of me –> {{"5", "5", "5", "5"}, {"5", "5", "5", "5"}, {"5", "5", "5", "5"}} set bMatrix to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:3) andRows:4 byColumns:3 set bScalar to current application’s NSNumber’s numberWithDouble:2 set bRes to bMatrix’s addScalar:bScalar set bbRes to intListFromMatrix(bRes) of me –> {{5, 5, 5}, {5, 5, 5}, {5, 5, 5}, {5, 5, 5}} set cScalar to current application’s NSNumber’s numberWithDouble:2 set cRes to bMatrix’s subtractScalar:cScalar set ccRes to intListFromMatrix(cRes) of me –> {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}} set dScalar to current application’s NSNumber’s numberWithDouble:2 set eRes to bMatrix’s multiplyScalar:dScalar set eeRes to intListFromMatrix(eRes) of me –> {{6, 6, 6}, {6, 6, 6}, {6, 6, 6}, {6, 6, 6}} set fScalar to current application’s NSNumber’s numberWithDouble:2 set fRes to bMatrix’s divideScalar:fScalar set ffRes to doubleListFromMatrix(fRes) of me –> {{1.5, 1.5, 1.5}, {1.5, 1.5, 1.5}, {1.5, 1.5, 1.5}, {1.5, 1.5, 1.5}} set m4 to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:4) andRows:3 byColumns:3 set m2 to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:2) andRows:3 byColumns:3 set gRes to m4’s addMatrix:m2 set ggRes to intListFromMatrix(gRes) of me –> {{6, 6, 6}, {6, 6, 6}, {6, 6, 6}} set hRes to m4’s subtractMatrix:m2 set hhRes to intListFromMatrix(hRes) of me –> {{2, 2, 2}, {2, 2, 2}, {2, 2, 2}} set iRes to m4’s multiplyMatrix:m2 set iiRes to intListFromMatrix(iRes) of me –> {{24, 24, 24}, {24, 24, 24}, {24, 24, 24 on doubleListFromMatrix(aMatrix) set aList to {} set aRows to aMatrix’s rows() set aColumns to aMatrix’s columns() repeat with rowC from 0 to (aRows – 1) set colList to {} repeat with colC from 0 to (aColumns – 1) set the end of colList to (aMatrix’s getElementAtRow:rowC andColumn:colC) as real end repeat set the end of aList to colList end repeat return aList end doubleListFromMatrix on intListFromMatrix(aMatrix) set aList to {} set aRows to aMatrix’s rows() set aColumns to aMatrix’s columns() repeat with rowC from 0 to (aRows – 1) set colList to {} repeat with colC from 0 to (aColumns – 1) set the end of colList to (aMatrix’s getElementAtRow:rowC andColumn:colC) as integer end repeat set the end of aList to colList end repeat return aList end intListFromMatrix on strListFromMatrix(aMatrix) set aList to {} set aRows to aMatrix’s rows() set aColumns to aMatrix’s columns() repeat with rowC from 0 to (aRows – 1) set colList to {} repeat with colC from 0 to (aColumns – 1) set the end of colList to (aMatrix’s getElementAtRow:rowC andColumn:colC) as string end repeat set the end of aList to colList end repeat return aList end strListFromMatrix |
MatRによる行列計算テスト v1
AppleScript名:MatRによる行列計算テスト v1 |
— Created 2017-05-15 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "DCMatrix" –https://github.com/davidcox95/MatR –指定数値、指定サイズ(行、列)で行列データを作成する set aMatrix to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:5) andRows:3 byColumns:4 (aMatrix’s |print|()) (* 5 5 5 5 5 5 5 5 5 5 5 5 *) –行列データに数値を加算する set bMatrix to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:7) andRows:4 byColumns:3 set bScalar to current application’s NSNumber’s numberWithDouble:2 set bRes to bMatrix’s addScalar:bScalar bRes’s |print|() (* 9 9 9 9 9 9 9 9 9 9 9 9 *) –行列データから数値を減算する set cScalar to current application’s NSNumber’s numberWithDouble:3 set cRes to bMatrix’s subtractScalar:cScalar cRes’s |print|() (* 4 4 4 4 4 4 4 4 4 4 4 4 *) –行列データに数値を乗算する set dScalar to current application’s NSNumber’s numberWithDouble:2 set eRes to bMatrix’s multiplyScalar:dScalar eRes’s |print|() (* 14 14 14 14 14 14 14 14 14 14 14 14 *) –行列データに数値を除算する set fScalar to current application’s NSNumber’s numberWithDouble:2 set fRes to bMatrix’s divideScalar:fScalar fRes’s |print|() (* 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 *) –行列同士の加算 set m4 to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:4) andRows:3 byColumns:3 set m2 to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:2) andRows:3 byColumns:3 set gRes to m4’s addMatrix:m2 gRes’s |print|() (* 6 6 6 6 6 6 6 6 6 *) –行列同士の減算 set hRes to m4’s subtractMatrix:m2 hRes’s |print|() (* 2 2 2 2 2 2 2 2 2 *) –行列同士の乗算 set iRes to m4’s multiplyMatrix:m2 iRes’s |print|() (* 24 24 24 24 24 24 24 24 24 *) |
与えられたデータのうちIPアドレスとして妥当なもののみを抽出
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 |
指定リストから、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 |