AppleScript名:元号変換v31 |
set a to "2010/7/21" set a to parseDate(a) of me set {aGengoStr, aGengoNum} to retJapaneseGengo(a) of me –> {"平成", 22} on retJapaneseGengo(aDate) set aYear to year of aDate set aMonth to month of aDate as number set aDay to day of aDate set aStr to retZeroPaddingText(aYear, 4) of me & retZeroPaddingText(aMonth, 2) of me & retZeroPaddingText(aDay, 2) of me set aGengo to "" if aStr ≥ "19890108" then set aGengo to "平成" set aGengoNum to aYear – 1989 + 1 else if aStr ≥ "19261225" then set aGengo to "昭和" set aGengoNum to aYear – 1926 + 1 else if aStr ≥ "19120730" then set aGengo to "大正" set aGengoNum to aYear – 1912 + 1 else if aStr ≥ "18680125" then set aGengo to "明治" set aGengoNum to aYear – 1868 + 1 end if return {aGengo, aGengoNum} end retJapaneseGengo –数値にゼロパディングしたテキストを返す 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 on parseDate(inStr) set aClass to class of inStr if aClass = string then try set aDate to date inStr on error return false end try else if aClass = date then set aDate to inStr end if return aDate end parseDate |
タグ: 10.11savvy
複数入力フィールドつきダイアログを表示
AppleScript名:複数入力フィールドつきダイアログを表示 |
— Created 2017-09-23 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property windisp : false property wController : false set fieldList to {"field 1", "field 2", "field 3", "field 4", "field 5", "field 6", "field 7", "field 8", "field 9", "field 10"} –set fieldList to {"field 1", "field 2"} set aButtonMSG to "OK" set aWinTitle to "Multiple Text Field Input" set aVal to getMultiTextFieldValues(fieldList, aWinTitle, aButtonMSG, 180) of me on getMultiTextFieldValues(fieldList, aWinTitle, aButtonMSG, timeOutSecs) set (my windisp) to false set fLen to (length of fieldList) + 1 set aView to current application’s NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 360, (20 * fLen))) set a1y to (30 * fLen) set tList to {} repeat with i in fieldList set a1TF to (current application’s NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(60, a1y, 80, 20))) (a1TF’s setEditable:false) (a1TF’s setStringValue:(i as string)) (a1TF’s setDrawsBackground:false) (a1TF’s setBordered:false) set a2TF to (current application’s NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(140, a1y, 200, 20))) (a2TF’s setEditable:true) (a2TF’s setDrawsBackground:true) (a2TF’s setBordered:true) (aView’s addSubview:a1TF) (aView’s addSubview:a2TF) set the end of tList to a2TF set a1y to a1y – 30 end repeat –Buttonをつくる set bButton to (current application’s NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(110, 10, 180, 40))) bButton’s setButtonType:(current application’s NSMomentaryLightButton) bButton’s setBezelStyle:(current application’s NSRoundedBezelStyle) bButton’s setTitle:aButtonMSG bButton’s setTarget:me bButton’s setAction:("clicked:") bButton’s setKeyEquivalent:(return) aView’s addSubview:bButton aView’s setNeedsDisplay:true –NSWindowControllerを作ってみた set aWin to (my makeWinWithView(aView, 400, ((fLen + 1) * 30), aWinTitle)) set wController to current application’s NSWindowController’s alloc() wController’s initWithWindow:aWin set (my windisp) to true wController’s showWindow:me set aCount to timeOutSecs * 10 set hitF to false repeat aCount times if (my windisp) = false then set hitF to true exit repeat end if delay 0.1 set aCount to aCount – 1 end repeat my closeWin:aWin if hitF = true then set aResList to {} repeat with i in tList set the end of aResList to (i’s stringValue()) as string end repeat else return false end if return aResList end getMultiTextFieldValues on clicked:aSender set (my windisp) to false end clicked: –make Window for Display on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle) set aScreen to current application’s 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 current application’s 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 setReleasedWhenClosed:true aWin’s |center|() 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: |
QuartzComoserでグラフ表示てすと v5(Window Controllerを追加)
–> Download script with Quartz composer
AppleScript名:QuartzComoserでグラフ表示てすと v5(Window Controllerを追加) |
— Created 2015-11-03 by Takaaki Naganoya — Modified 2017-10-18 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "Quartz" use framework "AppKit" use framework "Carbon" — AEInteractWithUser() is in Carbon property NSMutableDictionary : a reference to current application’s NSMutableDictionary property NSWindowCloseButton : a reference to current application’s NSWindowCloseButton property NSScreen : a reference to current application’s NSScreen property NSPredicate : a reference to current application’s NSPredicate property NSDictionary : a reference to current application’s NSDictionary property NSBackingStoreBuffered : a reference to current application’s NSBackingStoreBuffered property NSMutableArray : a reference to current application’s NSMutableArray property NSTitledWindowMask : a reference to current application’s NSTitledWindowMask property NSString : a reference to current application’s NSString property NSWindow : a reference to current application’s NSWindow property NSNumber : a reference to current application’s NSNumber property NSNormalWindowLevel : a reference to current application’s NSNormalWindowLevel property QCView : a reference to current application’s QCView property NSColor : a reference to current application’s NSColor property NSWindowController : a reference to current application’s NSWindowController if current application’s AEInteractWithUser(-1, missing value, missing value) is not equal to 0 then return set chartData to NSMutableArray’s new() –chartData’s addObject:(NSMutableDictionary’s dictionaryWithObjectsAndKeys_("練馬区", "label", 3, "value", missing value))–older way (Obsolete in 10.13) chartData’s addObject:(my recWithLabels:{"label", "value"} andValues:{"練馬区", 3}) chartData’s addObject:(my recWithLabels:{"label", "value"} andValues:{"青梅市", 1}) chartData’s addObject:(my recWithLabels:{"label", "value"} andValues:{"中野区", 2}) –上記データの最大値を求める set aMaxRec to chartData’s filteredArrayUsingPredicate:(NSPredicate’s predicateWithFormat_("SELF.value == %@.@max.value", chartData)) set aMax to value of aMaxRec set aMaxVal to (first item of aMax) as integer –Scalingの最大値を求める if aMaxVal ≥ 10 then set aScaleMax to (10 div aMaxVal) set aScaleMin to aScaleMax div 10 else set aScaleMax to (10 / aMaxVal) set aScaleMin to 1 end if try set aPath to path to resource "Chart.qtz" on error return end try set qtPath to NSString’s stringWithString:(POSIX path of aPath) set aView to QCView’s alloc()’s init() set qtRes to (aView’s loadCompositionFromFile:qtPath) aView’s setValue:chartData forInputKey:"Data" aView’s setValue:(NSNumber’s numberWithFloat:(0.5)) forInputKey:"Scale" aView’s setValue:(NSNumber’s numberWithFloat:(0.2)) forInputKey:"Spacing" aView’s setAutostartsRendering:true set maXFrameRate to aView’s maxRenderingFrameRate() ( aView’s setValue:(NSNumber’s numberWithFloat:aScaleMax / 10) forInputKey:"Scale") set aWin to (my makeWinWithView(aView, 800, 600, "AppleScript Composition Test")) set wController to NSWindowController’s alloc()wController’s initWithWindow:aWin aWin’s makeFirstResponder:aView wController’s showWindow:me aWin’s makeKeyAndOrderFront:me delay 5 my closeWin:aWin aView’s stopRendering() –レンダリング停止 –make Window for Display on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle) set aScreen to NSScreen’s mainScreen() set aFrame to {{0, 0}, {aWinWidth, aWinHeight}} set aBacking to NSTitledWindowMask set aDefer to NSBackingStoreBuffered — Window set aWin to NSWindow’s alloc() (aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen) aWin’s setBackgroundColor:(NSColor’s whiteColor()) aWin’s setTitle:aTitle aWin’s setDelegate:me aWin’s setDisplaysWhenScreenProfileChanges:true aWin’s setHasShadow:true aWin’s setIgnoresMouseEvents:false aWin’s setLevel:(NSNormalWindowLevel) aWin’s setOpaque:false aWin’s setReleasedWhenClosed:true aWin’s |center|() aWin’s makeKeyAndOrderFront:(me) –aWin’s movableByWindowBackground:true — Set Custom View aWin’s setContentView:aView –Set Close Button set closeButton to NSWindow’s standardWindowButton:(NSWindowCloseButton) forStyleMask:(NSTitledWindowMask) 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 recWithLabels:theKeys andValues:theValues return (NSDictionary’s dictionaryWithObjects:theValues forKeys:theKeys) as record end recWithLabels:andValues: |
popup button×2を作成
AppleScript名:popup button×2を作成 |
— Created 2015-12-30 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "Carbon" — AEInteractWithUser() is in Carbon property windisp : false property wController : false if current application’s AEInteractWithUser(-1, missing value, missing value) is not equal to 0 then return set ap1List to {"Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India"} set ap2List to {"Juliett", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo"} set aButtonMSG to "OK" set aSliderValMSG to "Numbers上の値を交換するセルの選択" set aVal to getPopupValues(ap1List, ap2List, aButtonMSG, aSliderValMSG, 20) of me –> {"Alpha", "Kilo"}–操作した場合 –> {false, false}–タイムアウト時 on getPopupValues(ap1List, ap2List, aButtonMSG, aSliderValMSG, timeOutSecs) set (my windisp) to true set aView to current application’s NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 360, 120)) –Labelをつくる set a1TF to current application’s NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(60, 110, 80, 20)) set a2TF to current application’s NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(60, 70, 80, 20)) a1TF’s setEditable:false a2TF’s setEditable:false a1TF’s setStringValue:"移動前:" a2TF’s setStringValue:"移動後:" a1TF’s setDrawsBackground:false a2TF’s setDrawsBackground:false a1TF’s setBordered:false a2TF’s setBordered:false –Ppopup Buttonをつくる set a1Button to current application’s NSPopUpButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(140, 110, 180, 20)) pullsDown:false set a2Button to current application’s NSPopUpButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(140, 70, 180, 20)) pullsDown:false a1Button’s removeAllItems() a2Button’s removeAllItems() a1Button’s addItemsWithTitles:ap1List a2Button’s addItemsWithTitles:ap2List –Buttonをつくる set bButton to (current application’s NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(110, 10, 180, 40))) bButton’s setButtonType:(current application’s NSMomentaryLightButton) bButton’s setBezelStyle:(current application’s NSRoundedBezelStyle) bButton’s setTitle:aButtonMSG bButton’s setTarget:me bButton’s setAction:("clicked:") bButton’s setKeyEquivalent:(return) aView’s addSubview:a1TF aView’s addSubview:a2TF aView’s addSubview:a1Button aView’s addSubview:a2Button aView’s addSubview:bButton aView’s setNeedsDisplay:true –NSWindowControllerを作ってみた set aWin to (my makeWinWithView(aView, 400, 160, aSliderValMSG)) set wController to current application’s NSWindowController’s alloc() wController’s initWithWindow:aWin wController’s showWindow:me set aCount to timeOutSecs * 10 set hitF to false repeat aCount times if (my windisp) = false then set hitF to true exit repeat end if delay 0.1 set aCount to aCount – 1 end repeat my closeWin:aWin if hitF = true then set s1Val to a1Button’s titleOfSelectedItem() as string set s2Val to a2Button’s titleOfSelectedItem() as string else set {s1Val, s2Val} to {false, false} end if return {s1Val, s2Val} end getPopupValues on clicked:aSender set (my windisp) to false end clicked: –make Window for Display on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle) set aScreen to current application’s 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 current application’s NSWindow’s alloc() (aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen) –aWin’s setBackgroundColor:(current application’s NSColor’s whiteColor()) 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 setReleasedWhenClosed:true aWin’s |center|() –aWin’s makeKeyAndOrderFront:(me) 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: |
color popup buttonを作成 v1(Controllerあり)
AppleScript名:color popup buttonを作成 v1(Controllerあり) |
— Created 2017-07-15 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "Carbon" — AEInteractWithUser() is in Carbon property windisp : false property wController : false –いらなかったかも? if current application’s AEInteractWithUser(-1, missing value, missing value) is not equal to 0 then return set ap1List to {{65535, 0, 65535}, {0, 32896, 16448}, {0, 32896, 65535}, {19702, 31223, 40505}} set aButtonMSG to "OK" set aSliderValMSG to "Select Color" set aVal to getPopupValues(ap1List, 65535, aButtonMSG, aSliderValMSG, 20) of me on getPopupValues(ap1List, aColMax, aButtonMSG, aSliderValMSG, timeOutSecs) set (my windisp) to true set aView to current application’s NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 360, 100)) –Labelをつくる set a1TF to current application’s NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(30, 60, 80, 20)) a1TF’s setEditable:false a1TF’s setStringValue:"Color:" a1TF’s setDrawsBackground:false a1TF’s setBordered:false –Ppopup Buttonをつくる set a1Button to current application’s NSPopUpButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(80, 60, 200, 20)) pullsDown:false a1Button’s removeAllItems() set a1Menu to current application’s NSMenu’s alloc()’s init() set iCount to 0 repeat with i in ap1List copy i to {r1, g1, b1} set nsCol to makeNSColorFromRGBAval(r1, g1, b1, aColMax, aColMax) of me set anImage to makeNSImageWithFilledWithColor(64, 16, nsCol) of me set aTitle to "col_test_" & (iCount as string) set aMenuItem to (current application’s NSMenuItem’s alloc()’s initWithTitle:aTitle action:"actionHandler:" keyEquivalent:"") (aMenuItem’s setImage:anImage) (aMenuItem’s setEnabled:true) (a1Menu’s addItem:aMenuItem) set iCount to iCount + 1 end repeat a1Button’s setMenu:a1Menu –Buttonをつくる set bButton to (current application’s NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(80, 10, 140, 40))) bButton’s setButtonType:(current application’s NSMomentaryLightButton) bButton’s setBezelStyle:(current application’s NSRoundedBezelStyle) bButton’s setTitle:aButtonMSG bButton’s setTarget:me bButton’s setAction:("clicked:") bButton’s setKeyEquivalent:(return) aView’s addSubview:a1TF aView’s addSubview:a1Button aView’s addSubview:bButton aView’s setNeedsDisplay:true –NSWindowControllerを作ってみた(いらない?) set aWin to (my makeWinWithView(aView, 300, 100, aSliderValMSG)) set wController to current application’s NSWindowController’s alloc() wController’s initWithWindow:aWin wController’s showWindow:me set aCount to timeOutSecs * 100 set hitF to false repeat aCount times if (my windisp) = false then set hitF to true exit repeat end if delay 0.01 set aCount to aCount – 1 end repeat my closeWin:aWin if hitF = true then set s1Val to a1Button’s titleOfSelectedItem() as string else set s1Val to false end if return s1Val end getPopupValues on clicked:aSender set (my windisp) to false end clicked: –make Window for Display on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle) set aScreen to current application’s 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 current application’s NSWindow’s alloc() (aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen) –aWin’s setBackgroundColor:(current application’s NSColor’s whiteColor()) 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 setReleasedWhenClosed:true aWin’s |center|() –aWin’s makeKeyAndOrderFront:(me) 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: –Popup Action Handler on actionHandler:sender set aTag to tag of sender as integer set aTitle to title of sender as string end actionHandler: 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 current application’s NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa return aColor end makeNSColorFromRGBAval –指定サイズの画像を作成し、指定色で塗ってファイル書き出し on makeNSImageWithFilledWithColor(aWidth, aHeight, fillColor) set anImage to current application’s NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight)) anImage’s lockFocus() — set theRect to {{x:0, y:0}, {height:aHeight, width:aWidth}} set theNSBezierPath to current application’s NSBezierPath’s bezierPath theNSBezierPath’s appendBezierPathWithRect:theRect — fillColor’s |set|() –色設定 theNSBezierPath’s fill() –ぬりつぶし — anImage’s unlockFocus() — return anImage end makeNSImageWithFilledWithColor |
slider+buttonを作成 v3
AppleScript名:slider+buttonを作成 v3 |
— Created 2015-12-27 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "Carbon" — AEInteractWithUser() is in Carbon property windisp : false property wController : false property aSliderValMSG : "" if current application’s AEInteractWithUser(-1, missing value, missing value) is not equal to 0 then return set aMaxVal to 10 set aButtonMSG to "OK" set aSliderValMSG to "スライダーの設定値:" set aVal to getSliderValue(aMaxVal, aButtonMSG, aSliderValMSG) of me on getSliderValue(aMaxVal, aButtonMSG, aSliderValMSG) set (my windisp) to true set (my aSliderValMSG) to aSliderValMSG set aView to current application’s NSSplitView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 360, 40)) aView’s setVertical:false –Sliderをつくる set aSlider to makeSider(aMaxVal) of me –Buttonをつくる set bButton to (current application’s NSButton’s alloc()’s init()) bButton’s setTitle:aButtonMSG bButton’s setTarget:me bButton’s setAction:("clicked:") bButton’s setKeyEquivalent:(return) –キーボードショートカット(リターンキー) aView’s addSubview:aSlider aView’s addSubview:bButton aView’s setNeedsDisplay:true –NSWindowControllerを作ってみた set aWin to (my makeWinWithView(aView, 400, 80, aSliderValMSG & (aMaxVal div 2) as string)) set wController to current application’s NSWindowController’s alloc() wController’s initWithWindow:aWin wController’s showWindow:me set aCount to 1800 repeat aCount times if (my windisp) = false then exit repeat end if delay 0.1 set aCount to aCount – 1 end repeat my closeWin:aWin set sVal to aSlider’s intValue() return sVal end getSliderValue on sliderChanged:aSender set aVal to aSender’s intValue() set parentWin to aSender’s |window|() parentWin’s setTitle:(my aSliderValMSG & (aVal as text)) end sliderChanged: on clicked:aSender set (my windisp) to false end clicked: –make Window for Display on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle) set aScreen to current application’s 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 current application’s NSWindow’s alloc() (aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen) –aWin’s setBackgroundColor:(current application’s NSColor’s whiteColor()) 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 setReleasedWhenClosed:true aWin’s |center|() –aWin’s makeKeyAndOrderFront:(me) 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 makeSider(aMaxNum) set aSlider to current application’s NSSlider’s alloc()’s init() aSlider’s setMaxValue:aMaxNum aSlider’s setMinValue:1 aSlider’s setNumberOfTickMarks:aMaxNum aSlider’s setKnobThickness:50 aSlider’s setAllowsTickMarkValuesOnly:true aSlider’s setTickMarkPosition:(current application’s NSTickMarkBelow) aSlider’s setIntValue:(aMaxNum div 2) aSlider’s setTarget:me aSlider’s setAction:("sliderChanged:") return aSlider end makeSider |
テーブルビューを表示 v5
AppleScript名:ASOCでテーブルビューを表示 v5a.scpt |
— 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 "Quartz" property |NSURL| : a reference to current application’s |NSURL| property NSData : a reference to current application’s NSData property NSView : a reference to current application’s NSView 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 MKMapView : a reference to current application’s MKMapView property NSURLRequest : a reference to current application’s NSURLRequest property NSURLConnection : a reference to current application’s NSURLConnection property NSJSONSerialization : a reference to current application’s NSJSONSerialization property NSWindowController : a reference to current application’s NSWindowController property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding property windisp : false property theDataSource : {} on run my performSelectorOnMainThread:"disp:" withObject:(missing value) waitUntilDone:true end run on disp:aParam set aWidth to 400 set aHeight to 200 set aDic to {{field1:"test 10", field2:"test 20"}, {field1:"test 11", field2:"test 21"}, {field1:"test 12", field2:"test 22"}, {field1:"test 13", field2:"test 23"}, {field1:"test 14", field2:"test 24"}, {field1:"test 15", field2:"test 25"}, {field1:"test 16", field2:"test 26"}, {field1:"test 17", field2:"test 27"}, {field1:"test 18", field2:"test 28"}, {field1:"test 19", field2:"test 29"}, {field1:"test 10", field2:"test 20"}, {field1:"test 11", field2:"test 21"}, {field1:"test 12", field2:"test 22"}, {field1:"test 13", field2:"test 23"}, {field1:"test 14", field2:"test 24"}, {field1:"test 15", field2:"test 25"}, {field1:"test 16", field2:"test 26"}, {field1:"test 17", field2:"test 27"}, {field1:"test 18", field2:"test 28"}, {field1:"test 19", field2:"test 29"}} dispTableView(aWidth, aHeight, "Result", "OK", 180, aDic) of me end disp: on dispTableView(aWidth as integer, aHeight as integer, aTitle as text, aButtonMSG as text, timeOutSecs as number, aDictList) –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 set aTableView to makeTableView(aDictList, aWidth, aHeight – 40) 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:aTableView 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 aWin’s makeFirstResponder:aTableView 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 dispTableView –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: –TableView Event Handlers on numberOfRowsInTableView:aView return my theDataSource’s |count|() end numberOfRowsInTableView: on tableView:aView objectValueForTableColumn:aColumn row:aRow set aRec to (my theDataSource)’s objectAtIndex:(aRow as number) set aTitle to (aColumn’s headerCell()’s title()) as string set aRes to (aRec’s valueForKey:aTitle) return aRes end tableView:objectValueForTableColumn:row: on makeTableView(aDicList, aWidth, aHeight) set aOffset to 40 set theDataSource to current application’s NSMutableArray’s alloc()’s init() theDataSource’s addObjectsFromArray:aDicList set aScroll to current application’s NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, aOffset, aWidth, aHeight)) set aView to current application’s NSTableView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, aOffset, aWidth, aHeight)) set aFirstRec to current application’s NSDictionary’s dictionaryWithDictionary:(first item of (aDicList as list)) set keyList to (aFirstRec’s allKeys()) as list set aLen to length of keyList repeat with i in keyList set j to contents of i set aColumn to (current application’s NSTableColumn’s alloc()’s initWithIdentifier:j) (aColumn’s setWidth:(aWidth div aLen)) (aColumn’s headerCell()’s setStringValue:j) (aView’s addTableColumn:aColumn) end repeat aView’s setDelegate:me aView’s setDataSource:me aView’s reloadData() aScroll’s setDocumentView:aView aView’s enclosingScrollView()’s setHasVerticalScroller:true –1行目を選択 set aIndexSet to current application’s NSIndexSet’s indexSetWithIndex:0 aView’s selectRowIndexes:aIndexSet byExtendingSelection:false –強制的にトップにスクロール –set maxHeight to aScroll’s documentView()’s |bounds|()’s |size|()’s height set aDBounds to aScroll’s documentView()’s |bounds|() if class of aDBounds = list then –macOS 10.13 or later set maxHeight to item 2 of item 1 of aDBounds else –macOS 10.10….10.12 set maxHeight to height of |size| of aDBounds end if set aPT to current application’s NSMakePoint(0.0, -40.0) —— (aScroll’s documentView()’s |bounds|()’s |size|()’s height)) aScroll’s documentView()’s scrollPoint:aPT return aScroll end makeTableView |
プログレスバー+buttonを作成
ウィンドウを作成して、その上でプログレスバーと途中停止用のボタンを描画、プログレスバーのアニメーション表示を行わせるサンプルScriptです。
スクリプトエディタ上でControl-Command-Rの操作を行い、スクリプトをメインスレッドで実行する必要があります。
AppleScript名:プログレスバー+buttonを作成 |
— Created 2015-12-11 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "Carbon" — AEInteractWithUser() is in Carbon property windisp : false property aPBar : missing value if current application’s AEInteractWithUser(-1, missing value, missing value) is not equal to 0 then return set aMaxVal to 100 set aButtonMSG to "Abort" set aTitle to "現在進行中…" set aWin to makeProgressWindow(aMaxVal, aButtonMSG, aTitle) of me repeat with i from 1 to aMaxVal by 1 if (my windisp) = false then exit repeat end if (aPBar’s setDoubleValue:(i as real)) delay 0.01 end repeat if i is not equal to aMaxVal then tell current application display dialog "Aborted" buttons {"OK"} default button 1 end tell end if my closeWin:aWin set my aPBar to missing value set aWin to missing value on makeProgressWindow(aMaxVal, aButtonMSG, aTitle) set (my windisp) to true –set (my aSliderValMSG) to aSliderValMSG set aView to current application’s NSSplitView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 360, 40)) aView’s setVertical:false –ProgressIndicatorをつくる set aSlider to makeProgressIndicator(aMaxVal) of me –Buttonをつくる set bButton to (current application’s NSButton’s alloc()’s init()) bButton’s setTitle:aButtonMSG bButton’s setTarget:me bButton’s setAction:("clicked:") aView’s addSubview:aSlider aView’s addSubview:bButton aView’s setNeedsDisplay:true set aWin to (my makeDockLevelWinWithView(aView, 400, 80, aTitle)) return aWin end makeProgressWindow on clicked:aSender set (my windisp) to false end clicked: –make Window for Display on makeDockLevelWinWithView(aView, aWinWidth, aWinHeight, aTitle) set aScreen to current application’s 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 current application’s NSWindow’s alloc() (aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen) –aWin’s setBackgroundColor:(current application’s NSColor’s whiteColor()) 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 NSDockWindowLevel) –プログレスバー表示用に変更 aWin’s setOpaque:false aWin’s setReleasedWhenClosed:true aWin’s |center|() aWin’s makeKeyAndOrderFront:(me) aWin’s setContentView:aView return aWin end makeDockLevelWinWithView –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: –make progress indicator on makeProgressIndicator(aMaxNum) set aPBar to current application’s NSProgressIndicator’s alloc()’s init() aPBar’s setMaxValue:aMaxNum aPBar’s setMinValue:1 aPBar’s setIndeterminate:false aPBar’s setControlSize:(current application’s NSProgressIndicatorPreferredLargeThickness) aPBar’s setDoubleValue:(1.0 as real) return aPBar end makeProgressIndicator |
テキストビュー+ボタンを作成(フォント指定)v2
動的にWindow+TextView+ボタンを作成し、指定フォントで指定文字を表示するAppleScriptです。
スクリプトエディタ上でControl+Command+Rによって実行します(メインスレッドで実行)。
AppleScript名:テキストビュー+ボタンを作成(フォント指定)v2 |
— Created 2016-02-01 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "Carbon" — AEInteractWithUser() is in Carbon property windisp : false property NSFont : a reference to current application’s NSFont property NSPredicate : a reference to current application’s NSPredicate property NSFontManager : a reference to current application’s NSFontManager set fRes to choose from list getEveryFontPSName() of me if fRes = {} or fRes = missing value then return set aFontName to contents of first item of fRes set aWidth to 600 set aHeight to 450 if current application’s AEInteractWithUser(-1, missing value, missing value) is not equal to 0 then return set aTitle to "テキストビューのじっけん/TextView Test" –Window Title set aButtonMSG to "OK" –Button Title –表示用テキストの作成 set aRes to checkExistenceOfFont(aFontName) of me if aRes = false then display dialog "There is no <" & aFontName & "> font. Designate another one." –No font return end if set bRes to retDefinedCharactersInFont(aFontName) of me set dispStr to listToStringUsingTextItemDelimiter(bRes, ", ") of me dispTextView(aWidth, aHeight, aFontName, dispStr, aButtonMSG, 180, aFontName, 36) of me on dispTextView(aWidth as integer, aHeight as integer, aTitle as text, dispStr, aButtonMSG as text, timeOutSecs as number, fontID, fontSize) set aColor to current application’s NSColor’s colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0 set (my windisp) to true –Text View+Scroll Viewをつくる set aScroll to current application’s NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) set aView to current application’s NSTextView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) aView’s setRichText:true aView’s useAllLigatures:true aView’s setTextColor:(current application’s NSColor’s yellowColor()) –cyanColor aView’s setFont:(current application’s NSFont’s fontWithName:fontID |size|:fontSize) –ヒラギノ明朝Pro W3 aView’s setBackgroundColor:aColor aScroll’s setDocumentView:aView aView’s enclosingScrollView()’s setHasVerticalScroller:true –Buttonをつくる set bButton to (current application’s NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, 40))) bButton’s setTitle:aButtonMSG bButton’s setTarget:me bButton’s setAction:("clicked:") –SplitViewをつくる set aSplitV to current application’s NSSplitView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aHeight, aWidth)) aSplitV’s setVertical:false aSplitV’s addSubview:aScroll aSplitV’s addSubview:bButton aSplitV’s setNeedsDisplay:true –WindowとWindow Controllerをつくる set aWin to makeWinWithView(aSplitV, aWidth, aHeight, aTitle, 0.9) aWin’s makeKeyAndOrderFront:(missing value) set wController to current application’s NSWindowController’s alloc() wController’s initWithWindow:aWin aWin’s makeFirstResponder:aView aView’s setString:dispStr wController’s showWindow:me set aCount to timeOutSecs * 10 –timeout seconds * 10 repeat aCount times if (my windisp) = false then exit repeat end if delay 0.1 set aCount to aCount – 1 end repeat my closeWin:aWin end dispTextView –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 current application’s NSScreen’s mainScreen() set aFrame to {{0, 0}, {aWinWidth, aWinHeight}} set aBacking to current application’s NSTitledWindowMask –NSBorderlessWindowMask set aDefer to current application’s NSBackingStoreBuffered — Window set aWin to current application’s NSWindow’s alloc() (aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen) –aWin’s setBackgroundColor:(current application’s NSColor’s whiteColor()) 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: –指定PostScript名称のフォントがコンピューター上に存在するかどうかチェック on checkExistenceOfFont(fontName as string) if fontName = "" then return false set aFont to current application’s NSFont’s fontWithName:fontName |size|:9.0 if aFont = missing value then return false else return true end if end checkExistenceOfFont –指定Postscript名称のフォントに定義されている文字数を数えて返す on countDefinedCharactersInFont(fontName as string) script spdF property aList : {} end script set aFont to current application’s NSFont’s fontWithName:fontName |size|:9.0 if aFont = missing value then return false set aSet to aFont’s coveredCharacterSet() set aList of spdF to {} repeat with i from 1 to 65535 set aRes to (aSet’s characterIsMember:i) as boolean if aRes = true then set the end of aList of spdF to (string id i) end if end repeat return length of (aList of spdF) end countDefinedCharactersInFont –指定Postscript名称のフォントに定義されている文字を返す on retDefinedCharactersInFont(fontName as string) script spdG property aList : {} end script set aFont to current application’s NSFont’s fontWithName:fontName |size|:24.0 set aSet to aFont’s coveredCharacterSet() set aList of spdG to {} repeat with i from 1 to 65535 set aRes to (aSet’s characterIsMember:i) as boolean if aRes = true then set the end of aList of spdG to (string id i) end if end repeat return (aList of spdG) end retDefinedCharactersInFont on listToStringUsingTextItemDelimiter(sourceList, textItemDelimiter) set the CocoaArray to current application’s NSArray’s arrayWithArray:sourceList set the CocoaString to CocoaArray’s componentsJoinedByString:textItemDelimiter return (CocoaString as string) end listToStringUsingTextItemDelimiter –インストールされているフォントのpost script nameを取得する on getEveryFontPSName() set aFontList to NSFontManager’s sharedFontManager()’s availableFonts() set thePred to NSPredicate’s predicateWithFormat:"NOT SELF BEGINSWITH ’.’" set aFontList to (aFontList’s filteredArrayUsingPredicate:thePred) as list set aList to {} repeat with i in aFontList set aName to contents of i set the end of aList to aName end repeat return aList end getEveryFontPSName |
Segmented Controlを表示する
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 |
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 *) |