アラートダイアログ上にTable Viewを表示するAppleScriptです。
データを表UIで表示する部品としては、Shane Stanleyの「Myriad Tables Lib」が定番ですが、高機能なぶんライブラリ中にFrameworkが含まれていたりして、場合によっては困ることもあります。また、見た目をカスタマイズしたい場合にも、あまりいじくれなくて困ることもあります(とくにデータの文字サイズを変更できなくて困ること多し)。
そのため、AppleScriptだけで書いた表UI表示用の部品も、たまに必要になることがあります。
▲スクリプトエディタで実行@macOS 10.14.5(左:Light Mode、右:Dark Mode)
▲Script Debuggerで実行@macOS 10.14.5(左:Light Mode、右:Dark Mode)
▲AppleScriptアプレットで実行@macOS 10.14.5(左:Light Mode、右:Dark Mode)
AppleScript名:アラートダイアログ上にTable Viewを表示 v4 |
— Created 2019-02-21 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 NSModalPanelWindowLevel : a reference to current application’s NSModalPanelWindowLevel property NSAlertSecondButtonReturn : a reference to current application’s NSAlertSecondButtonReturn property theResult : 0 property returnCode : 0 property theDataSource : {} on run set (my theResult) to 0 –initialize set paramObj to {myMessage:"項目の選択", mySubMessage:"適切なものを以下からえらんでください", aTableList:{{field1:"MacBook Air", field2:"119,800円", field3:"1.6GHzデュアルコアIntel Core i5(Turbo Boost使用時最大3.6GHz)、4MB L3キャッシュ"}, {field1:"MacBook Pro 13", field2:"139,800円", field3:"1.4GHzクアッドコアIntel Core i5(Turbo Boost使用時最大3.9GHz)、128MB eDRAM"}, {field1:"MacBook Pro 15", field2:"258,800円", field3:"2.6GHz 6コアIntel Core i7(Turbo Boost使用時最大4.5GHz)、12MB共有L3キャッシュ"}, {field1:"Mac mini", field2:"122,800円", field3:"3.0GHz 6コアIntel Core i5 Turbo Boost使用時最大4.1GHz 9MB共有L3キャッシュ"}, {field1:"iMac 21.5", field2:"120,800円", field3:"2.3GHzデュアルコアIntel Core i5(Turbo Boost使用時最大3.6GHz)"}, {field1:"iMac 4K 21.5", field2:"142,800円", field3:"3.6GHzクアッドコアIntel Core i3"}, {field1:"iMac 5K 27", field2:"198,800円", field3:"3.0GHz 6コア Intel Core i5(Turbo Boost使用時最大4.1GHz)"}, {field1:"iMac Pro", field2:"558,800円", field3:"3.2GHz Intel Xeon W Turbo Boost使用時最大4.2GHz 19MBキャッシュ"}}, aSortOrder:{"field1", "field2", "field3"}} –my chooseItemByTableView:paramObj–for debug my performSelectorOnMainThread:"chooseItemByTableView:" withObject:paramObj waitUntilDone:true return (my theResult) end run on chooseItemByTableView:paramObj set aMainMes to myMessage of paramObj set aSubMes to mySubMessage of paramObj set aTList to (aTableList of paramObj) as list set labelSortList to (aSortOrder of paramObj) as list set aWidth to 600 set aHeight to 200 set aScroll to makeTableView(aTList, aWidth, aHeight, labelSortList) of me –Detect Dark Mode set dMode to retLIghtOrDark() of me if dMode = true then set tvCol to 1 set tvAlpha to 0.8 set bCol to 0.1 set bAlpha to 0.8 set contentCol to (NSColor’s cyanColor()) else set tvCol to 0 set tvAlpha to 0.1 set bCol to 1 set bAlpha to 0.8 set contentCol to (NSColor’s blackColor()) end if — 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:aScroll set myWindow to its |window| end tell myWindow’s setOpaque:(false) myWindow’s setBackgroundColor:(NSColor’s colorWithCalibratedWhite:(bCol) alpha:(bAlpha)) myWindow’s setLevel:(NSModalPanelWindowLevel) — show alert in modal loop NSRunningApplication’s currentApplication()’s activateWithOptions:0 my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true if (my returnCode) = 1001 then error number -128 set (my theResult) to (aScroll’s documentView’s selectedRow()) + 1 end chooseItemByTableView: on doModal:aParam set (my returnCode) to (aParam’s runModal()) as number end doModal: –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, labelSortList) 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 aLen to length of labelSortList repeat with i in labelSortList 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 aScroll’s setVerticalLineScroll:(30.0 as real) –1行目を選択 set aIndexSet to current application’s 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, -1 * (maxHeight as real)) aScroll’s documentView()’s scrollPoint:aPT return aScroll end makeTableView on alertShowHelp:aNotification display dialog "Help Me!" buttons {"OK"} default button 1 with icon 1 return false –trueを返すと親ウィンドウ(アラートダイアログ)がクローズする end alertShowHelp: –ダークモードの判定。ダークモード時:true、ライトモード時:falseが返る。System Eventsを使っていないのは、macOS 10.14以降対策(承認を取得しなくてもいいように) on retLIghtOrDark() set curMode to (current application’s NSUserDefaults’s standardUserDefaults()’s stringForKey:"AppleInterfaceStyle") as string return (curMode = "Dark") as boolean end retLIghtOrDark |