Archive for the 'NSTableView' Category

2016/01/07 テーブルビューを表示

Cocoaの機能を用いて、NSTableViewを表示するAppleScriptです。

asoctable.png

Xcode上のAppleScriptでNSTableViewを表示するプログラムを書くのなら、片手間でできるぐらい簡単ですが、通常のScript Editor上でプログラマティカルに100%AppleScriptから作ろうとすると、ものすごく苦労させられました。

Mac上ではXcode+InterfaceBuilderによるGUIツールによる開発が推奨されており、このようにプログラムからGUI部品を生成するケースはあまり見られません。ゆえに、たいへんに調査に苦労させられたわけですが(足掛け2か月以上かかってます)、表示してしまうとけっこうあっけないものです。

本Scriptの実行には、Script Editor上でControl-Command-Rの操作で行ってください。

AppleScript名:ASOCでテーブルビューを表示
– Created 2016-01-07 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”

property theDataSource : {}

set aWidth to 300
set aHeight to 300

set aTitle to “NSTableView Test”

set aScroll to current application’s NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
set aView to current application’s NSTableView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))

set aColumn to current application’s NSTableColumn’s alloc()’s initWithIdentifier:“field1″
set bColumn to current application’s NSTableColumn’s alloc()’s initWithIdentifier:“field2″
aColumn’s setWidth:150
bColumn’s setWidth:150

aColumn’s headerCell()’s setStringValue:“field1″
bColumn’s headerCell()’s setStringValue:“field2″

aView’s addTableColumn:aColumn
aView’s addTableColumn:bColumn

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″}}

set theDataSource to current application’s NSMutableArray’s alloc()’s init()
theDataSource’s addObjectsFromArray:aDic

–aView’s setDataSource:theDataSource
–aView’s reloadDataForRowIndexes:1 columnIndexes:1

–aView’s setDelegate:me
aView’s setDataSource:me
–aView’s reloadData()

aScroll’s setDocumentView:aView
aView’s enclosingScrollView()’s setHasVerticalScroller:true

set aWin to makeWinWithView(aScroll, aWidth, aHeight, aTitle, 1.0)
–aWin’s makeKeyAndOrderFront:(missing value)
–aWin’s makeFirstResponder:aView

–NSWindowControllerを作ってみた
set wController to current application’s NSWindowController’s alloc()
wController’s initWithWindow:aWin

wController’s showWindow:me

delay 5
my closeWin:aWin

–make Window for Input
on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV)
  set aScreen to current application’s NSScreen’s mainScreen()
  
set aFrame to {{0, 0}, {aWinWidth, aWinHeight}}
  
set aBacking to current application’s NSTitledWindowMask –NSBorderlessWindowMask
  
set aDefer to current application’s NSBackingStoreBuffered
  
  
– Window
  
set aWin to current application’s NSWindow’s alloc()
  (
aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen)
  
aWin’s setBackgroundColor:(current application’s NSColor’s whiteColor())
  
  
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:

★Click Here to Open This Script