AppleScript名:ASOCでテーブルビューを表示 v5a.scpt |
— Created 2017-12-20 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" use framework "Quartz" property |NSURL| : a reference to current application’s |NSURL| property NSData : a reference to current application’s NSData property NSView : a reference to current application’s NSView property NSString : a reference to current application’s NSString property NSScreen : a reference to current application’s NSScreen property NSButton : a reference to current application’s NSButton property NSWindow : a reference to current application’s NSWindow property MKMapView : a reference to current application’s MKMapView property NSURLRequest : a reference to current application’s NSURLRequest property NSURLConnection : a reference to current application’s NSURLConnection property NSJSONSerialization : a reference to current application’s NSJSONSerialization property NSWindowController : a reference to current application’s NSWindowController property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding property windisp : false property theDataSource : {} on run my performSelectorOnMainThread:"disp:" withObject:(missing value) waitUntilDone:true end run on disp:aParam set aWidth to 400 set aHeight to 200 set aDic to {{field1:"test 10", field2:"test 20"}, {field1:"test 11", field2:"test 21"}, {field1:"test 12", field2:"test 22"}, {field1:"test 13", field2:"test 23"}, {field1:"test 14", field2:"test 24"}, {field1:"test 15", field2:"test 25"}, {field1:"test 16", field2:"test 26"}, {field1:"test 17", field2:"test 27"}, {field1:"test 18", field2:"test 28"}, {field1:"test 19", field2:"test 29"}, {field1:"test 10", field2:"test 20"}, {field1:"test 11", field2:"test 21"}, {field1:"test 12", field2:"test 22"}, {field1:"test 13", field2:"test 23"}, {field1:"test 14", field2:"test 24"}, {field1:"test 15", field2:"test 25"}, {field1:"test 16", field2:"test 26"}, {field1:"test 17", field2:"test 27"}, {field1:"test 18", field2:"test 28"}, {field1:"test 19", field2:"test 29"}} dispTableView(aWidth, aHeight, "Result", "OK", 180, aDic) of me end disp: on dispTableView(aWidth as integer, aHeight as integer, aTitle as text, aButtonMSG as text, timeOutSecs as number, aDictList) –Check If this script runs in foreground if not (current application’s NSThread’s isMainThread()) as boolean then error "This script must be run from the main thread (Command-Control-R in Script Editor)." end if set (my windisp) to true set aTableView to makeTableView(aDictList, aWidth, aHeight – 40) of me –Buttonをつくる set bButton to (NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(aWidth / 4, 0, aWidth / 2, 40))) bButton’s setTitle:aButtonMSG bButton’s setButtonType:(current application’s NSMomentaryLightButton) bButton’s setBezelStyle:(current application’s NSRoundedBezelStyle) bButton’s setKeyEquivalent:(return) bButton’s setTarget:me bButton’s setAction:("clicked:") –NSViewをつくる set aNSV to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aHeight, aWidth)) aNSV’s addSubview:aTableView aNSV’s addSubview:bButton aNSV’s setNeedsDisplay:true set aWin to makeWinWithView(aNSV, aWidth, aHeight, aTitle, 1.0) set wController to NSWindowController’s alloc() wController’s initWithWindow:aWin aWin’s makeFirstResponder:aTableView wController’s showWindow:me aWin’s makeKeyAndOrderFront:me set aCount to timeOutSecs * 10 –timeout seconds * 10 repeat aCount times if (my windisp) = false then exit repeat end if delay 0.1 end repeat my closeWin:aWin end dispTableView –Button Clicked Event Handler on clicked:aSender set (my windisp) to false end clicked: –make Window for Input on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV) set aScreen to NSScreen’s mainScreen() set aFrame to {{0, 0}, {aWinWidth, aWinHeight}} set aBacking to current application’s NSTitledWindowMask set aDefer to current application’s NSBackingStoreBuffered — Window set aWin to NSWindow’s alloc() (aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen) aWin’s setTitle:aTitle aWin’s setDelegate:me aWin’s setDisplaysWhenScreenProfileChanges:true aWin’s setHasShadow:true aWin’s setIgnoresMouseEvents:false aWin’s setLevel:(current application’s NSNormalWindowLevel) aWin’s setOpaque:false aWin’s setAlphaValue:alphaV –append aWin’s setReleasedWhenClosed:true aWin’s |center|() aWin’s makeKeyAndOrderFront:(me) — Set Custom View aWin’s setContentView:aView return aWin end makeWinWithView –close win on closeWin:aWindow repeat with n from 10 to 1 by -1 (aWindow’s setAlphaValue:n / 10) delay 0.02 end repeat aWindow’s |close|() end closeWin: –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) 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 aFirstRec to current application’s NSDictionary’s dictionaryWithDictionary:(first item of (aDicList as list)) set keyList to (aFirstRec’s allKeys()) as list set aLen to length of keyList repeat with i in keyList 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 –1行目を選択 set aIndexSet to current application’s 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 |
More from my site
(Visited 33 times, 1 visits today)