アラートダイアログ上にボタンタイプのチェックボックスを並べて、選択したボタンのタイトルを選択するAppleScriptです。
書類の印刷対象を、こまかくページ指定するために作成した部品です。
ヘルプボタンにより、全ボタンの選択/非選択状態を作り出せます。
▲Beta2でAlert Dialogのタイトルまわりが変更されたようですが、、、、うーーん。あと、選択状態をボタンの見かけから知ることができないですわー
AppleScript名:アラートダイアログ上に印刷対象ページ一覧を表示して対象ページを指定 |
— Created 2020-07-17 by Takaaki Naganoya — 2020 Piyomaru Software set aRes to selectByCheckbox("Select each printout page", "Selected pages are the target", 64, "Helvetica") of chooseSeqNum script chooseSeqNum use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" property NSFont : a reference to current application’s NSFont property NSView : a reference to current application’s NSView property NSAlert : a reference to current application’s NSAlert property NSColor : a reference to current application’s NSColor property NSButton : a reference to current application’s NSButton property NSOnState : a reference to current application’s NSOnState property NSOffState : a reference to current application’s NSOffState property NSTextField : a reference to current application’s NSTextField property NSMutableArray : a reference to current application’s NSMutableArray property NSButtonTypeOnOff : a reference to current application’s NSButtonTypeOnOff property NSMutableDictionary : a reference to current application’s NSMutableDictionary property NSRunningApplication : a reference to current application’s NSRunningApplication property NSFontAttributeName : a reference to current application’s NSFontAttributeName property NSKernAttributeName : a reference to current application’s NSKernAttributeName property NSMutableParagraphStyle : a reference to current application’s NSMutableParagraphStyle property NSLigatureAttributeName : a reference to current application’s NSLigatureAttributeName property NSMutableAttributedString : a reference to current application’s NSMutableAttributedString property NSUnderlineStyleAttributeName : a reference to current application’s NSUnderlineStyleAttributeName property NSParagraphStyleAttributeName : a reference to current application’s NSParagraphStyleAttributeName property NSForegroundColorAttributeName : a reference to current application’s NSForegroundColorAttributeName property theResult : 0 property returnCode : 0 property cList : {} –Checkbox button object array property bArray : {} on selectByCheckbox(aMainMessage, aSubMessage, aMaxPage, myFont) set paramObj to {myMessage:aMainMessage, mySubMessage:aSubMessage, maxPage:aMaxPage, fontName:myFont} –my chooseItemByCheckBox:paramObj –for Debugging my performSelectorOnMainThread:"chooseItemByCheckBox:" withObject:(paramObj) waitUntilDone:true –> {1, 3, 5, 7, 9, 11} return cList end selectByCheckbox on chooseItemByCheckBox:(paramObj) set aMainMes to (myMessage of paramObj) as string set aSubMes to (mySubMessage of paramObj) as string set aMaxPage to (maxPage of paramObj) as integer set aFontName to (fontName of paramObj) as string set cList to {} set colNum to 10 set rowNum to (aMaxPage div 10) + 1 set aLen to (colNum * rowNum) set aButtonCellWidth to 56 set aButtonCellHeight to 40 set viewWidth to aButtonCellWidth * colNum set viewHeight to aButtonCellHeight * (rowNum + 1) –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 aFontSize to 24 set bArray to current application’s NSMutableArray’s new() –Make Header (Month, Year) set aCalList to makeSeqNumList(1, aMaxPage) of me set aCount to 1 –Make Calendar (Calendar Body) repeat with y from 1 to rowNum repeat with x from 1 to colNum –try set j to (contents of item aCount of aCalList) 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 as string)) (tmpB’s setFont:(NSFont’s fontWithName:aFontName |size|:aFontSize)) –set attrTitle to makeRTFfromParameters((aCount as string), aFontName, aFontSize, 0, (aFontSize * 1.2)) of me –(tmpB’s setAttributedTitle:(attrTitle)) (tmpB’s setShowsBorderOnlyWhileMouseInside:true) (tmpB’s setAlignment:(current application’s NSCenterTextAlignment)) (tmpB’s setEnabled:(j ≠ "")) (tmpB’s setTarget:me) (tmpB’s setAction:("clicked:")) (tmpB’s setButtonType:(NSButtonTypeOnOff)) (tmpB’s setHidden:(j = "")) (tmpB’s setTag:(j)) (bArray’s addObject:tmpB) set aCount to aCount + 1 if aCount > aMaxPage then exit repeat –end try end repeat if aCount > aMaxPage then exit repeat end repeat –Select the first radio button item –(tmpArray’s objectAtIndex:0)’s setState:(current application’s NSOnState) set my theResult to {} (aView’s setSubviews:bArray) — 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 –for Help Button its setShowsHelp:(true) its setDelegate:(me) 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 bNumList to (bArray’s valueForKeyPath:"state") as list –set cList to {} repeat with i from 1 to aMaxPage set aFlag to (contents of item i of bNumList) as boolean if aFlag = true then set the end of cList to contents of i end if end repeat copy cList to bArray end chooseItemByCheckBox: on doModal:aParam set (my returnCode) to aParam’s runModal() end doModal: on clicked:aParam set aTag to (tag of aParam) as integer –clicked if aTag is not in (my theResult) then set the end of (my theResult) to aTag else set theResult to my deleteItem:aTag fromList:theResult end if end clicked: on deleteItem:anItem fromList:theList set theArray to NSMutableArray’s arrayWithArray:theList theArray’s removeObject:anItem return theArray as list end deleteItem:fromList: –1D List(数値)をsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート on sort1DNumList:theList ascOrder:aBool tell current application’s NSSet to set theSet to setWithArray_(theList) tell current application’s NSSortDescriptor to set theDescriptor to sortDescriptorWithKey_ascending_(missing value, true) set sortedList to theSet’s sortedArrayUsingDescriptors:{theDescriptor} return (sortedList) as list end sort1DNumList:ascOrder: –Help Button Clicked Event Handler on alertShowHelp:aNotification set aRes to display dialog "Do you change all checkbox state?" buttons {"All Off", "All On", "Cancel"} default button 3 with icon 1 set bRes to (button returned of aRes) as string if bRes = "All Off" then set bLen to bArray’s |count|() set theResult to {} repeat with i from 0 to bLen ((bArray’s objectAtIndex:i)’s setState:(current application’s NSOffState)) end repeat else if bRes = "All On" then set bLen to bArray’s |count|() set theResult to {} repeat with i from 0 to bLen ((bArray’s objectAtIndex:i)’s setState:(current application’s NSOnState)) set the end of theResult to i + 1 end repeat end if return false –trueを返すと親ウィンドウ(アラートダイアログ)がクローズする end alertShowHelp: –書式つきテキストを組み立てる on makeRTFfromParameters(aStr as string, fontName as string, aFontSize as real, aKerning as real, aLineSpacing as real) set aVal1 to NSFont’s fontWithName:fontName |size|:aFontSize set aKey1 to (NSFontAttributeName) set aVal2 to NSColor’s blackColor() set aKey2 to (NSForegroundColorAttributeName) set aVal3 to aKerning set akey3 to (NSKernAttributeName) set aVal4 to 0 set akey4 to (NSUnderlineStyleAttributeName) set aVal5 to 2 –all ligature ON set akey5 to (NSLigatureAttributeName) set aParagraphStyle to NSMutableParagraphStyle’s alloc()’s init() aParagraphStyle’s setMinimumLineHeight:(aLineSpacing) aParagraphStyle’s setMaximumLineHeight:(aLineSpacing) set akey7 to (NSParagraphStyleAttributeName) set keyList to {aKey1, aKey2, akey3, akey4, akey5, akey7} set valList to {aVal1, aVal2, aVal3, aVal4, aVal5, aParagraphStyle} set attrsDictionary to NSMutableDictionary’s dictionaryWithObjects:valList forKeys:keyList set attrStr to NSMutableAttributedString’s alloc()’s initWithString:aStr attributes:attrsDictionary return attrStr end makeRTFfromParameters on makeNSTextField(xPos as integer, yPos as integer, myWidth as integer, myHeight as integer, editableF as boolean, setVal as string, backgroundF as boolean, borderedF as boolean) set aNSString to NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(xPos, yPos, myWidth, myHeight)) aNSString’s setEditable:(editableF) aNSString’s setStringValue:(setVal) aNSString’s setDrawsBackground:(backgroundF) aNSString’s setBordered:(borderedF) return aNSString end makeNSTextField on makeSeqNumList(fromNum as integer, toNum as integer) script spd property aList : {} end script set aList of spd to {} repeat with i from fromNum to toNum set the end of (aList of spd) to i end repeat return (aList of spd) end makeSeqNumList end script |