アラートダイアログ上にNSBrowserを表示するAppleScriptの試作品です。
# 本Scriptは実験用であって実用性はありません(^ー^;;
Finderに使われていることで認知度は高いものの、使いどころが難しいうえにサンプル・コードも多くないので登場頻度が少ないNSBrowserです。(Mac OS Xの前身である)NEXTSTEPのFile Managerがこんな感じのUIでした。
(個人的に)敷居の高かったNSBrowserも、いろいろ試してみたらいい感じになってきました。現状では単に2次元配列っぽいものにアクセスしているだけですが、ツリー型の構造データを与えるようにするとか、配列から徐々にデータ抽出する(大分類→中分類→小分類)ようにすれば、だいたい期待どおりの動作になるはずです。
NSBrowserも、使いたいときに調べると情報が見つからずに困りますが、用のないときなら調べておくといろいろ大丈夫そうです。
macOS 10.14上のスクリプトエディタ/Script Debugger上で動くようにハンドラをメインスレッド上で明示的に動作させるように変更しましたが、10.14上でだけ独特な挙動があるため要注意です。ただ、テストプログラムなのでそんなものだろうかと。
AppleScript名:アラートダイアログ上にBrowserを表示 v1.1.scptd |
— – Created by: Takaaki Naganoya – Created on: 2019/03/02 — – Copyright © 2019 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use framework "AppKit" use scripting additions property NSAlert : a reference to current application’s NSAlert property NSColor : a reference to current application’s NSColor property NSBrowser : a reference to current application’s NSBrowser property NSScrollView : a reference to current application’s NSScrollView 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:"Choose a data", mySubMessage:"Choose an appropriate data from tree structured data list", aTableList:{{"1", "1.1", "1.1.1"}, {"2", "2.1", "2.1.1"}, {"3", "3.1", "3.1.1"}, {"4", "4.1", "4.1.1"}}} –set aRes to my chooseItemByBrowser:paramObj my performSelectorOnMainThread:"chooseItemByBrowser:" withObject:(paramObj) waitUntilDone:true on chooseItemByBrowser:paramObj set aMainMes to myMessage of paramObj set aSubMes to mySubMessage of paramObj set aTList to (aTableList of paramObj) as list — make browser view with scroll view set aScrollWithTable to makeBrowserView(aTList, 500, 200) 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 end chooseItemByBrowser: on doModal:aParam set (my returnCode) to aParam’s runModal() end doModal: on makeBrowserView(aList as list, aWidth as number, aHeight as number) set (my theDataSource) to NSMutableArray’s arrayWithArray:aList set aScroll to NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) set aView to NSBrowser’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) aView’s setDelegate:(me) aView’s setMinColumnWidth:120 aView’s setSeparatesColumns:true –aView’s setMaxVisibleColumns:2 aView’s setAutohidesScroller:true –aView’s setBackgroundColor:(NSColor’s grayColor()) aView’s setReusesColumns:true aScroll’s setDocumentView:aView aView’s enclosingScrollView()’s setHasHorizontalScroller:true aView’s enclosingScrollView()’s setHasVerticalScroller:true return aScroll end makeBrowserView –NSBrowser Event Handlers on browser:aView numberOfRowsInColumn:aColumn set aCount to my theDataSource’s |count|() return aCount end browser:numberOfRowsInColumn: on browser:aView willDisplayCell:(aCell) atRow:rowIndex column:colIndex set selRow to (my theDataSource)’s objectAtIndex:(rowIndex) set selCol to selRow’s objectAtIndex:(colIndex) aCell’s setTitle:(selCol as string) set colMax to ((my theDataSource)’s objectAtIndex:0)’s |count|() log {"colMax, colIndex", colMax, colIndex} set exMax to (colIndex < colMax – 1) as boolean aCell’s setLeaf:(not exMax) end browser:willDisplayCell:atRow:column: |