アラートダイアログを作成し、その上にScroll View+Table ViewおよびMap Viewを表示して選択肢の選択を行うAppleScriptです。
Map Viewを使用するために、インターネット接続が必要です。地図表示パフォーマンスはインターネット接続速度次第です。
{{placeName:"Hey", aLat:"35.69906613", aLong:"139.77084064"}....}
のように位置データを、
名称, 緯度, 経度
とまとめたリストで選択肢を指定します。実行すると選択肢のアイテム番号(1からはじまる)を返してきます。
▲左から、macOS 10.12.6、10.13.6、10.14.4betaで同じAppleScriptを動かしたところ。同じ場所、同じZoom LevelでもOSバージョンごとに微妙に表示内容が違う
現状の実装では、表UI(Table View)内のデータがダブルクリックで編集できてしまうので、編集できないようにするあたりが改良対象でしょうか。あと、地図・衛星写真の切り替えもつけておくとよいでしょう。
本Scriptに与える緯度・経度情報についてはあらかじめ住所ジオコーダーによって「住所情報→緯度・経度情報」の変換を行なったものを書いておく必要がありますが、Yahoo!の住所ジオコーダーサービスなどを呼び出せば、住所情報をパラメーターとすることも可能です。
サンプルデータの緯度・経度情報は、例によって「戦場の絆」の入っている近所のゲーセンの情報を適当にみつくろって入れてみたものです。
AppleScript名:アラートダイアログ上にTable View+Map Viewを表示 v2 |
— Created 2019-02-22 by Takaaki Naganoya — 2019 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" use framework "MapKit" property NSAlert : a reference to current application’s NSAlert property NSIndexSet : a reference to current application’s NSIndexSet property MKMapView : a reference to current application’s MKMapView 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 zLevel : 18 property aMaxViewWidth : 800 property theResult : 0 property returnCode : 0 property theDataSource : {} property curRow : 0 property aMapView : missing value property nameL : {} property placeL : {} set aPlaceList to {{placeName:"Hey", aLat:"35.69906613", aLong:"139.77084064"}, {placeName:"namco中野", aLat:"35.70859274", aLong:"139.66584339"}, {placeName:"ゲームシティ板橋", aLat:"35.74572771", aLong:"139.67553260"}, {placeName:"ラウンドワンスタジアム板橋", aLat:"35.77661583", aLong:"139.67864491"}, {placeName:"キャロム練馬", aLat:"35.76386421", aLong:"139.66591600"}, {placeName:"アミュージアムOSC", aLat:"35.75308308", aLong:"139.59476696"}} set paramObj to {myMessage:"場所の選択", mySubMessage:"適切な場所を以下のリストからえらんでください", placeList:aPlaceList} my performSelectorOnMainThread:"chooseItemByTableViewWithMap:" withObject:(paramObj) waitUntilDone:true return (my theResult) on chooseItemByTableViewWithMap:paramObj set aMainMes to myMessage of paramObj set aSubMes to mySubMessage of paramObj set aTList to (placeList of paramObj) as list set nameL to {} set placeL to {} repeat with i in aTList set the end of nameL to (placeName of i) set the end of placeL to {contents of (aLat of i), contents of (aLong of i)} end repeat — create a view set theView to current application’s NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aMaxViewWidth, 400)) — make table view with scroll view set aScrollWithTable to makeTableView(nameL, 200, 400) of me set aMapView to MKMapView’s alloc()’s initWithFrame:(current application’s NSMakeRect(210, 0, aMaxViewWidth – 210, 400)) tell aMapView its setMapType:(current application’s MKMapTypeStandard) its setZoomEnabled:true its setScrollEnabled:true its setPitchEnabled:true its setRotateEnabled:true its setShowsCompass:true its setShowsZoomControls:true its setShowsScale:true its setShowsUserLocation:true set aLocation to current application’s CLLocationCoordinate2DMake((first item of first item of placeL) as real, (second item of first item of placeL) as real) its setCenterCoordinate:aLocation zoomLevel:(zLevel) animated:false its setDelegate:me end tell theView’s setSubviews:{aScrollWithTable, aMapView} — 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:theView 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 (my theResult) to (aScrollWithTable’s documentView’s selectedRow()) + 1 end chooseItemByTableViewWithMap: 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 0 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 (tmpRow) to aView’s selectedRow() as number –Table View上で現在選択行「以外の」行が選択されたら、Mapを選択項目で更新 if (my curRow) is not equal to tmpRow then set tmpLat to (first item of item (tmpRow + 1) of placeL) as real set tmpLong to (second item of item (tmpRow + 1) of placeL) as real tell aMapView set aLocation to current application’s CLLocationCoordinate2DMake(tmpLat, tmpLong) its setCenterCoordinate:aLocation zoomLevel:(zLevel) animated:false end tell set (my curRow) to tmpRow end if 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 92 times, 1 visits today)