アラートダイアログ上にRadio Buttonを表示して項目選択するAppleScriptです。
普通にラジオボタンで項目選択できるようになりました。
表示列数を指定できるようにしておきましたが、選択項目の余りが発生すると処理に矛盾が発生するため、キリのいい(割り切れる)列数を指定してください(短時間で作った試作品なので、そこまで気合は入れていません)。
AppleScript名:アラートダイアログ上にRadio Buttonを表示 v4a |
— Created 2019-08-07 by Takaaki Naganoya — 2019 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 NSAlert : a reference to current application’s NSAlert property NSButton : a reference to current application’s NSButton property NSRadioButton : a reference to current application’s NSRadioButton property NSButtonTypeRadio : a reference to current application’s NSButtonTypeRadio property NSRunningApplication : a reference to current application’s NSRunningApplication property theResult : 0 property returnCode : 0 set paramObj to {myMessage:"Select target Item", mySubMessage:"適切なものを以下からえらんでください", myColNum:2, matrixTitleList:{"にんじん", "ごぼう", "だいこん", "りんご", "キャベツ", "レタス", "じゃがいも", "にんじん", "小松菜", "青梗菜", "しいたけ", "舞茸"}} –my chooseItemByRadioButton:paramObj –for Debugging my performSelectorOnMainThread:"chooseItemByRadioButton:" withObject:(paramObj) waitUntilDone:true return (theResult as integer) on chooseItemByRadioButton:(paramObj) set aMainMes to myMessage of paramObj set aSubMes to mySubMessage of paramObj set aMatList to (matrixTitleList of paramObj) as list set aLen to length of aMatList set colNum to (myColNum of paramObj) as integer set rowNum to (aLen div colNum) + (aLen mod colNum) set aButtonCellWidth to 150 set aButtonCellHeight to 24 set viewWidth to aButtonCellWidth * colNum set viewHeight to aButtonCellHeight * rowNum –define the matrix size where you’ll put the radio buttons set matrixRect to current application’s NSMakeRect(0.0, 0.0, viewWidth, viewHeight) set aView to NSView’s alloc()’s initWithFrame:(matrixRect) set aCount to 1 set tmpArray to current application’s NSMutableArray’s new() repeat with y from 1 to rowNum repeat with x from 1 to colNum if aCount ≤ aLen then set j to contents of item aCount of aMatList set tmpB to (NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(((x – 1) * aButtonCellWidth), ((aLen – aCount) div colNum) * aButtonCellHeight, aButtonCellWidth, aButtonCellHeight))) (tmpB’s setTitle:j) (tmpB’s setShowsBorderOnlyWhileMouseInside:true) (tmpB’s setTag:(aCount)) (tmpB’s setTarget:me) (tmpB’s setAction:("clicked:")) (tmpB’s setButtonType:(current application’s NSButtonTypeRadio)) (tmpArray’s addObject:tmpB) end if set aCount to aCount + 1 end repeat end repeat –Select the first radio button item (tmpArray’s objectAtIndex:0)’s setState:(current application’s NSOnState) set theResult to 1 (aView’s setSubviews:tmpArray) — set up alert set theAlert to NSAlert’s alloc()’s init() tell theAlert its setMessageText:aMainMes its setInformativeText:aSubMes its addButtonWithTitle:"OK" its addButtonWithTitle:"Cancel" its setAccessoryView:aView 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 end chooseItemByRadioButton: on doModal:aParam set (my returnCode) to aParam’s runModal() end doModal: on clicked:aParam set aTag to tag of aParam set theResult to aTag end clicked: |