AppleScript名:Segmented Controlを表示する |
— Created 2017-12-20 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" property NSView : a reference to current application’s NSView property NSScreen : a reference to current application’s NSScreen property NSButton : a reference to current application’s NSButton property NSWindow : a reference to current application’s NSWindow property NSWindowController : a reference to current application’s NSWindowController property NSSegmentedControl : a reference to current application’s NSSegmentedControl property windisp : false property selSeg : 0 set aWidth to 500 set aHeight to 100 set segTitleList to {"First Segment", "Second Segment", "Third Segment", "Forth Segment"} set aRes to dispSegControl(aWidth, aHeight, "Segemented Control", "OK", 180, segTitleList) of me on dispSegControl(aWidth as integer, aHeight as integer, aTitle as text, aButtonMSG as text, timeOutSecs as number, segTitleList) –Check If this script runs in foreground if not (current application’s NSThread’s isMainThread()) as boolean then error "This script must be run from the main thread (Command-Control-R in Script Editor)." end if set selSeg to 0 set (my windisp) to true –Segmented Controlをつくる set aSeg to makeSegmentedControl(segTitleList, aWidth, aHeight) of me –Buttonをつくる set bButton to (NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(aWidth / 4, 0, aWidth / 2, 40))) bButton’s setTitle:aButtonMSG bButton’s setButtonType:(current application’s NSMomentaryLightButton) bButton’s setBezelStyle:(current application’s NSRoundedBezelStyle) bButton’s setKeyEquivalent:(return) bButton’s setTarget:me bButton’s setAction:("clicked:") –NSViewをつくる set aNSV to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aHeight, aWidth)) aNSV’s addSubview:aSeg aNSV’s addSubview:bButton aNSV’s setNeedsDisplay:true set aWin to makeWinWithView(aNSV, aWidth, aHeight, aTitle, 1.0) set wController to NSWindowController’s alloc() wController’s initWithWindow:aWin wController’s showWindow:me aWin’s makeKeyAndOrderFront:me set aCount to timeOutSecs * 10 –timeout seconds * 10 repeat aCount times if (my windisp) = false then exit repeat end if delay 0.1 end repeat my closeWin:aWin return (selSeg + 1) end dispSegControl –Button Clicked Event Handler on clicked:aSender set (my windisp) to false end clicked: on clickedSeg:aSender set aSel to aSender’s selectedSegment() set selSeg to aSel end clickedSeg: –make Window for Input on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV) set aScreen to NSScreen’s mainScreen() set aFrame to {{0, 0}, {aWinWidth, aWinHeight}} set aBacking to current application’s NSTitledWindowMask set aDefer to current application’s NSBackingStoreBuffered — Window set aWin to NSWindow’s alloc() (aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen) aWin’s setTitle:aTitle aWin’s setDelegate:me aWin’s setDisplaysWhenScreenProfileChanges:true aWin’s setHasShadow:true aWin’s setIgnoresMouseEvents:false aWin’s setLevel:(current application’s NSNormalWindowLevel) aWin’s setOpaque:false aWin’s setAlphaValue:alphaV –append aWin’s setReleasedWhenClosed:true aWin’s |center|() aWin’s makeKeyAndOrderFront:(me) — Set Custom View aWin’s setContentView:aView return aWin end makeWinWithView –close win on closeWin:aWindow repeat with n from 10 to 1 by -1 (aWindow’s setAlphaValue:n / 10) delay 0.02 end repeat aWindow’s |close|() end closeWin: on makeSegmentedControl(titleList, aWidth, aHeight) set aLen to length of titleList set aSeg to NSSegmentedControl’s alloc()’s init() aSeg’s setSegmentCount:aLen set aCount to 0 repeat with i in titleList set j to contents of i (aSeg’s setLabel:j forSegment:aCount) set aCount to aCount + 1 end repeat aSeg’s setTranslatesAutoresizingMaskIntoConstraints:false aSeg’s setSegmentStyle:(current application’s NSSegmentStyleTexturedRounded) aSeg’s setFrame:(current application’s NSMakeRect(20, aHeight – 60, aWidth, aHeight – 40)) aSeg’s setTrackingMode:0 aSeg’s setTarget:me aSeg’s setAction:"clickedSeg:" aSeg’s setSelectedSegment:0 return aSeg end makeSegmentedControl |
タグ: 10.13savvy
Colorsで色バリエーション展開を計算して表示 v2
Coloursをフレームワーク化したcolorsKit.frameworkを呼び出して、指定のRGB色のカラーバリエーションを計算するAppleScriptです。
AppleScript名:Colorsで色バリエーション展開を計算して表示 v2 |
— Created 2017-12-20 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" use framework "colorsKit" –https://github.com/bennyguitar/Colours property NSView : a reference to current application’s NSView property NSColor : a reference to current application’s NSColor property NSString : a reference to current application’s NSString property NSScreen : a reference to current application’s NSScreen property NSButton : a reference to current application’s NSButton property NSWindow : a reference to current application’s NSWindow property NSColorWell : a reference to current application’s NSColorWell property NSWindowController : a reference to current application’s NSWindowController property windisp : false set aWidth to 500 set aHeight to 250 set {rVal, gVal, bVal} to choose color set aNSCol to makeNSColorFromRGBAval(rVal, gVal, bVal, 65536, 65536) of me dispCustomView(aWidth, aHeight, "Color Variation Result", "OK", 180, aNSCol) of me on dispCustomView(aWidth as integer, aHeight as integer, aTitle as text, aButtonMSG as text, timeOutSecs as number, aNSCol) –Check If this script runs in foreground if not (current application’s NSThread’s isMainThread()) as boolean then error "This script must be run from the main thread (Command-Control-R in Script Editor)." end if set (my windisp) to true –Buttonをつくる set bButton to (NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(aWidth / 4, 0, aWidth / 2, 40))) bButton’s setTitle:aButtonMSG bButton’s setButtonType:(current application’s NSMomentaryLightButton) bButton’s setBezelStyle:(current application’s NSRoundedBezelStyle) bButton’s setKeyEquivalent:(return) bButton’s setTarget:me bButton’s setAction:("clicked:") –NSViewをつくる set aNSV to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aHeight, aWidth)) aNSV’s addSubview:bButton aNSV’s setNeedsDisplay:true –NSColorWellをつくる repeat with ii from 0 to 3 by 1 set colorArray1 to (aNSCol’s colorSchemeOfType:ii) as list set the beginning of colorArray1 to aNSCol set tmpLen to (length of colorArray1) set aStep to 0 repeat with i in colorArray1 set aColorWell to (NSColorWell’s alloc()’s initWithFrame:(current application’s NSMakeRect((100 * aStep), (((3 – ii) * 50) + 40), 100, 40))) (aColorWell’s setColor:i) (aColorWell’s setBordered:true) (aNSV’s addSubview:aColorWell) set aStep to aStep + 1 end repeat end repeat set aWin to makeWinWithView(aNSV, aWidth, aHeight, aTitle, 1.0) set wController to NSWindowController’s alloc() wController’s initWithWindow:aWin wController’s showWindow:me aWin’s makeKeyAndOrderFront:me set aCount to timeOutSecs * 10 –timeout seconds * 10 repeat aCount times if (my windisp) = false then exit repeat end if delay 0.1 end repeat my closeWin:aWin end dispCustomView –Button Clicked Event Handler on clicked:aSender set (my windisp) to false end clicked: –make Window for Input on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV) set aScreen to NSScreen’s mainScreen() set aFrame to {{0, 0}, {aWinWidth, aWinHeight}} set aBacking to current application’s NSTitledWindowMask set aDefer to current application’s NSBackingStoreBuffered — Window set aWin to NSWindow’s alloc() (aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen) aWin’s setTitle:aTitle aWin’s setDelegate:me aWin’s setDisplaysWhenScreenProfileChanges:true aWin’s setHasShadow:true aWin’s setIgnoresMouseEvents:false aWin’s setLevel:(current application’s NSNormalWindowLevel) aWin’s setOpaque:false aWin’s setAlphaValue:alphaV –append aWin’s setReleasedWhenClosed:true aWin’s |center|() aWin’s makeKeyAndOrderFront:(me) — Set Custom View aWin’s setContentView:aView return aWin end makeWinWithView –close win on closeWin:aWindow repeat with n from 10 to 1 by -1 (aWindow’s setAlphaValue:n / 10) delay 0.02 end repeat aWindow’s |close|() end closeWin: on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer) set aRedCocoa to (redValue / aMaxVal) as real set aGreenCocoa to (greenValue / aMaxVal) as real set aBlueCocoa to (blueValue / aMaxVal) as real set aAlphaCocoa to (alphaValue / aMaxVal) as real set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa return aColor end makeNSColorFromRGBAval |
指定ファイルをFinderで選択表示_OLD Style_as
AppleScript名:指定ファイルをFinderで選択表示_OLD Style_as |
— Created 2016-10-31 by Takaaki Naganoya — 2016 Piyomaru Software set aFile to choose file tell application "Finder" activate reveal aFile end tell |
指定ファイルをFinderで選択表示_asoc
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 |