アラートダイアログを作成し、その上にScroll View+Table Viewを表示して選択肢の選択を行うAppleScriptです。
1D List(1次元配列)を与え、選択を行うUIです。実行すると選択肢のアイテム番号(1からはじまる)を返してきます。
とくにこれが作りたかったのではなく、位置情報(Geo Location)の選択をTable Viewで行い、該当する場所をMap Viewに表示させると擬似的に複数の固定位置情報から任意のものを選択させるダイアログが作れるだろうかと思って作成した試作品です。
東京への引っ越しの荷物が到着して、ようやく検証用のサブマシンが届いたものの、ネットワークを新規敷設しなくてはならないのでぼちぼちです。
AppleScript名:アラートダイアログ上にTable Viewを表示 v3 |
— 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 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 theResult : 0 property returnCode : 0 property theDataSource : {} set paramObj to {myMessage:"項目の選択", mySubMessage:"適切なものを以下からえらんでください", aTableList:{"なし", "りんご", "ひよこ", "ぎょうざ", "すなぎも", "ぼんじり", "もも"}} 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 –define the matrix size where you’ll put the radio buttons set aScrollWithTable to makeTableView(aTList, 300, 150) 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 — 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 maxHeight to aScroll’s documentView()’s |bounds|()’s |size|()’s height 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 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: |
More from my site
(Visited 201 times, 1 visits today)
アラートダイアログ上にTable Viewを表示 v8_ソート、並び替え、複数選択 – AppleScriptの穴 says:
[…] v1 […]