指定色のアラートダイアログを作成し、その上にScroll View+Table Viewを表示して選択肢の選択を行うAppleScriptです。
アラートダイアログの背景色変更のテストを行なったものです。また、メソッド名をテキストで(Table Viewから)取得し、そのメソッド名で色を作成するテスト(NSSelectorFromString)も行いました。
Table Viewで選択した色は、次回実行時に反映されます(スクリプトエディタ/Script Debugger上で)。
本当はTable View上で行を選択したときに、アラートダイアログの背景色を変更させたかったのですが、試してみたもののうまく動かなかったので、その機能は削りました。最初から指定色のダイアログウィンドウを表示するだけなら問題はないので、現状のようなコードに落ち着きました。
AppleScript名:アラートダイアログの背景色を指定してTable Viewを表示 |
— Created 2019-02-25 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 NSColor : a reference to current application’s NSColor property NSIndexSet : a reference to current application’s NSIndexSet property NSScrollView : a reference to current application’s NSScrollView property NSTableView : a reference to current application’s NSTableView property NSTableColumn : a reference to current application’s NSTableColumn property NSMutableArray : a reference to current application’s NSMutableArray property NSRunningApplication : a reference to current application’s NSRunningApplication property NSAlertSecondButtonReturn : a reference to current application’s NSAlertSecondButtonReturn property NSFullSizeContentViewWindowMask : a reference to current application’s NSFullSizeContentViewWindowMask property theResult : 0 property returnCode : 0 property theDataSource : {} property theColList : {} property theWindow : missing value property selectedColor : missing value set paramObj to {myMessage:"Choose a color", mySubMessage:"Choose an appropriate color from color name list", aTableList:{"redColor", "blueColor", "yellowColor", "greenColor", "grayColor", "blackColor"}} set aRes to my chooseItemByTableView:paramObj on chooseItemByTableView:paramObj set aMainMes to myMessage of paramObj set aSubMes to mySubMessage of paramObj set aTList to (aTableList of paramObj) as list — make table view with scroll view set aScrollWithTable to makeTableView(aTList, 300, 120) of me — 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:aScrollWithTable end tell — customize alert’s window style and color if selectedColor = missing value then set tmpColor to NSColor’s grayColor() else set tmpSel to current application’s NSSelectorFromString(selectedColor) set tmpColor to NSColor’s performSelector:tmpSel end if set theWindow to theAlert’s |window|() theWindow’s setTitleVisibility:false –theWindow’s setStyleMask:( NSFullSizeContentViewWindowMask) theWindow’s setTitlebarAppearsTransparent:true theWindow’s setBackgroundColor:tmpColor — 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 return (aScrollWithTable’s documentView’s selectedRow()) + 1 end chooseItemByTableView: on doModal:aParam set (my returnCode) to aParam’s runModal() end doModal: on makeTableView(aList as list, aWidth as number, aHeight as number) set aOffset to 40 set sourceList to {} repeat with i in aList set the end of sourceList to {dataItem:(contents of i)} end repeat set theDataSource to NSMutableArray’s alloc()’s init() theDataSource’s addObjectsFromArray:sourceList set aScroll to NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, aOffset, aWidth, aHeight)) set aView to NSTableView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, aOffset, aWidth, aHeight)) set aColumn to (NSTableColumn’s alloc()’s initWithIdentifier:"dataItem") (aColumn’s setWidth:aWidth) (aColumn’s headerCell()’s setStringValue:"dataItem") (aView’s addTableColumn:aColumn) 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 NSIndexSet’s indexSetWithIndex:0 aView’s selectRowIndexes:aIndexSet byExtendingSelection:false –強制的にトップにスクロール 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 –TableView Event Handlers on numberOfRowsInTableView:aView return my theDataSource’s |count|() end numberOfRowsInTableView: on tableView:aView objectValueForTableColumn:aColumn row:aRow set selRow to (aRow as number) set aRec to (my theDataSource)’s objectAtIndex:selRow set aTitle to (aColumn’s headerCell()’s title()) as string set aRes to (aRec’s valueForKey:aTitle) as string set my selectedColor to aRes return aRes end tableView:objectValueForTableColumn:row: on tableView:aView shouldEditTableColumn:aTableColumn row:rowIndex return false –Not Editable end tableView:shouldEditTableColumn:row: |