アラートダイアログを作成し、その上にsegmented control(タブみたいな複数項目1択UI部品)を表示して選択肢の選択を行うAppleScriptです。
選択肢のインデックス番号が(1からはじまる)返ってきます。
AppleScript名:アラートダイアログ上にsegmented controlを表示 |
— Created 2019-02-14 by Takaaki Naganoya — 2019 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" property NSAlert : a reference to current application’s NSAlert property NSView : a reference to current application’s NSView property NSSegmentedControl : a reference to current application’s NSSegmentedControl property NSRunningApplication : a reference to current application’s NSRunningApplication property NSSegmentStyleTexturedRounded : a reference to current application’s NSSegmentStyleTexturedRounded property theSegSel : 0 property returnCode : 0 set paramObj to {myMessage:"項目選択", mySubMessage:"どれか選択してください。", segmentMes:{"Red", "Blue", "Yellow", "Brown", "White", "Cyan", "Grey"}} set segRes to my chooseSegment:paramObj on chooseSegment:paramObj set aMainMes to myMessage of paramObj set aSubMes to mySubMessage of paramObj set segMesList to segmentMes of paramObj — create a view set theView to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 400, 200)) –Segmented Controlをつくる set aSeg to makeSegmentedControl(segMesList, 400, 80) of me theView’s setSubviews:{aSeg} set theAlert to NSAlert’s alloc()’s init() — set up alert tell theAlert its setMessageText:aMainMes its setInformativeText:aSubMes its addButtonWithTitle:"OK" its addButtonWithTitle:"Cancel" its setAccessoryView:aSeg end tell — show alert in modal loop NSRunningApplication’s currentApplication()’s activateWithOptions:0 my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true if (my returnCode as number) = 1001 then error number -128 set (my theSegSel) to (aSeg’s selectedSegment()) as number return (my theSegSel) + 1 end chooseSegment: on doModal:aParam set (my returnCode) to aParam’s runModal() end doModal: 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:(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 on clickedSeg:aSender set aSel to aSender’s selectedSegment() set selSeg to aSel end clickedSeg: |
More from my site
(Visited 163 times, 1 visits today)