use AppleScript version "2.8"

use scripting additions

use framework "Foundation"

use framework "AppKit"


global myApp

global theWindow, mainView

global closeFlg

global minXMargin, maxXMargin, minYMargin, maxYMargin, widthSizable, heightSizable


property theDataSource : {}

global aTableView, recipeSource


on run

my performSelectorOnMainThread:"mainMakeObject:" withObject:(missing value) waitUntilDone:true

--

log "End"

end run


on mainMakeObject:inputObj

set myApp to current application

set closeFlg to false

--

set aTitle to "TableView View-Based テスト2"

set aRect to {0, 0, 380, 250}

set moveCenter to true

set theWindow to my makeNewWindow(aTitle, aRect, moveCenter)

--

my makeObject()

--

theWindow's setAlphaValue:1.0

--

repeat

if closeFlg then

my closeWin:theWindow

exit repeat

end if

delay 0.2

end repeat

end mainMakeObject:


on makeNewWindow(aTitle, aRect, moveCenter)

set {windowX, windowY, windowW, windowH} to aRect

set theRect to myApp's NSMakeRect(windowX, windowY, windowW, windowH)

set aScreen to myApp's class "NSScreen"'s mainScreen()

set aStyle to (myApp's NSWindowStyleMaskTitled as integer)

set aStyle to aStyle + (myApp's NSWindowStyleMaskClosable as integer)

set aStyle to aStyle + (myApp's NSWindowStyleMaskMiniaturizable as integer)

set aStyle to aStyle + (myApp's NSWindowStyleMaskResizable as integer)

set aBacking to myApp's NSBackingStoreBuffered

set aWindow to myApp's class "NSWindow"'s alloc()'s initWithContentRect:theRect styleMask:aStyle backing:aBacking defer:false screen:aScreen

tell aWindow

setAlphaValue_(0.0)

setDelegate_(me)

setTitle_(aTitle)

setMinSize_(myApp's NSMakeSize(300, 200))

setMaxSize_(myApp's NSMakeSize(10000, 10000))

setBackgroundColor_(myApp's class "NSColor"'s colorWithCalibratedRed:0.95 green:0.95 blue:0.95 alpha:1.0)

setDisplaysWhenScreenProfileChanges_(true)

setReleasedWhenClosed_(true)

makeKeyAndOrderFront_(me)

end tell

--

if moveCenter then aWindow's |center|()

--

set minXMargin to (myApp's NSViewMinXMargin) as integer -- 左の余白を柔軟に

set maxXMargin to (myApp's NSViewMaxXMargin) as integer -- 右の余白を柔軟に

set minYMargin to (myApp's NSViewMinYMargin) as integer -- 下の余白を柔軟に

set maxYMargin to (myApp's NSViewMaxYMargin) as integer -- 上の余白を柔軟に

set widthSizable to (myApp's NSViewWidthSizable) as integer -- 横方向の幅を柔軟に

set heightSizable to (myApp's NSViewHeightSizable) as integer -- 縦方向の幅を柔軟に

--

return aWindow

end makeNewWindow


(* 各オブジェクトの配置 *)

on makeObject()

set mainView to theWindow's contentView

set theWindowFrame to mainView's frame()

set {{windowX, windowY}, {windowWidth, windowHeight}} to theWindowFrame

--

try

-- 新しくオブジェクトを配置する部分 --------------------

set {x, y, w, h} to {0, 0, windowWidth, windowHeight}

set columnLevel to 3

my makeTableView(x, y, w, h, columnLevel)

-- オブジェクトのレシピ

set recipeSource to {}

--(row 1)

set aCodeText1 to "class : NSTextField

selectable : no

bordered : false

stringValue : テキスト 1

fontSize:18.0

textColor: red,blue=1.0,0.3

textAlignment:left

fontName:HiraMinProN-W3"

set aCodeText2 to "class : NSButton

title : チェック 1

buttonType: switch

allowsMixedState:false

swichState:ON"

set aCodeText3 to "class : NSButton

title : ボタン 1

bezelStyle:Rounded"

set recipeSource to {{aCodeText1, aCodeText2, aCodeText3}}

--(row 2)

set aCodeText1 to "class : NSTextField

selectable : yas

bordered : true

stringValue : テキスト 2

fontSize:14.0

textColor: green,blue=1.0,0.3"

set aCodeText2 to "class : NSButton

title : チェック 2

bezelStyle : Rounded

buttonType: switch

allowsMixedState:false

swichState:Off"

set aCodeText3 to "class : NSButton

title : ボタン 2

bezelStyle:RegularSquare

BezelColor:blue,green=1.0,0.8"

set recipeSource to recipeSource & {{aCodeText1, aCodeText2, aCodeText3}}

--(row 3)

set aCodeText1 to "class : NSDatePicker

textColor:orangeColor"

set aCodeText2 to "class : NSSwitch

swichState:On"

set aCodeText3 to "class : NSPopUpButton

pullsDown : true

autoenablesItems:Yes

addTitleItem:アイテム 1

addTitleItem:アイテム 2

addTitleItem:アイテム 3"

set recipeSource to recipeSource & {{aCodeText1, aCodeText2, aCodeText3}}

--レシピからインスタンス作成をしてTableViewに反映

my tableViewReload()

----------------------------------------------------------

on error errText

log errText

end try

end makeObject


----------// Table View //

on makeTableView(x, y, w, h, columnLevel)

(* データーソース()を用意する *)

set theDataSource to current application's class "NSMutableArray"'s alloc()'s init()

(* テーブルの作成 *)

set aTableView to current application's class "NSTableView"'s alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, w, h))

tell aTableView

setDelegate_(me)

setDataSource_(me)

setUsesAlternatingRowBackgroundColors_(true)

end tell

(* コラムデータ(列)を作成 *)

repeat with _n from 1 to columnLevel

set aColumn to (current application's class "NSTableColumn"'s alloc()'s initWithIdentifier:("data" & _n))

tell aColumn

(headerCell()'s setTitle:("データ " & _n))

setWidth_(100)

end tell

(aTableView's addTableColumn:aColumn)

end repeat

(* スクロールビューのコンテンツビューにテーブルデータをのせて配置 *)

set aScroll to current application's class "NSScrollView"'s alloc()'s initWithFrame:(current application's NSMakeRect(x, y, w, h))

tell aScroll

setDocumentView_(aTableView)

setAutoresizingMask_(widthSizable + heightSizable)

end tell

mainView's addSubview:aScroll

end makeTableView


on tableViewReload()

set theDataSource to current application's class "NSMutableArray"'s alloc()'s init()

set sourceList to {}

repeat with _row in recipeSource

set _c to count of _row

set aCodeText to item 1 of _row

-- 1

set _item to {data1:(my makeInstance(aCodeText))}

-- 2

if _c ≥ 2 then

set aCodeText to item 2 of _row

set _item to _item & {data2:(my makeInstance(aCodeText))}

end if

-- 3

if _c ≥ 3 then

set aCodeText to item 3 of _row

set _item to _item & {data3:(my makeInstance(aCodeText))}

end if

-- 4

if _c ≥ 4 then

set aCodeText to item 4 of _row

set _item to _item & {data4:(my makeInstance(aCodeText))}

end if

-- (5列以降は未実装)

----- 集合

set sourceList to sourceList & {_item}

end repeat

(theDataSource's addObjectsFromArray:sourceList)

aTableView's reloadData()

end tableViewReload


(* TableViewの表示行数処理(必須) *)

on numberOfRowsInTableView:aTableView

set c to count of (my theDataSource)

return c

end numberOfRowsInTableView:


(* TableViewデータの処理(必須) *)

on tableView:aTableView viewForTableColumn:aColumn row:aRow

set aRec to (my theDataSource)'s objectAtIndex:(aRow as number)

set aIdentifier to (aColumn's identifier()) as string

set columnData to aRec's valueForKey:aIdentifier

return columnData

end tableView:viewForTableColumn:row:


----------// Table View ここまで//


----------// オブジェクトアイテムの作成 //

on makeInstance(_CodeText)

-- クラスとコードテキスト から オブジェクト を返す

if _CodeText does not start with "Class" then return (missing value)

set _n to offset in _CodeText of "NS"

if _n = 0 then return (missing value)

set aClass to text _n thru -1 of ((paragraph 1 of _CodeText) as text)

set aCodeText to text from paragraph 2 to paragraph -1 of _CodeText

try

set theObject to myApp's (class aClass)'s alloc()'s init()

on error

log ("不明な形式Error : " & aClass)

return (missing value)

end try

repeat with oneLine in (paragraphs of aCodeText)

set oneLine to oneLine as text

set c to offset in oneLine of ":"

if c > 0 then

set akey to text 1 thru (c - 1) of oneLine

try

set aCommend to text (c + 1) thru -1 of oneLine

on error

set aCommend to ""

end try

-- 除外するための半角スペース & 全角スペース & タブキャラクターの見本を作成

set spCodes to (ASCII character 32) & (character id 12288) & (ASCII character 9)

repeat 10 times

try

if not (spCodes contains (text 1 of aCommend)) then exit repeat

set aCommend to text 2 thru -1 of aCommend

on error

exit repeat

end try

end repeat

--// コマンドの分岐 //

try

if akey contains "deregate" then

(theObject's setDelegate:me)

else if akey contains "flipped" then

(theObject's setFlipped:(my convertTextToBoolean(aCommend)))

else if akey contains "selectable" then

(theObject's setSelectable:(my convertTextToBoolean(aCommend)))

else if akey contains "editable" then

(theObject's setEditable:(my convertTextToBoolean(aCommend)))

else if akey contains "bordered" then

(theObject's setBordered:(my convertTextToBoolean(aCommend)))

else if akey contains "frame" then

set {x, y, w, h} to my convertTextToNumber(aCommend)

(theObject's setFrame:(myApp's NSMakeRect(x, y, w, h)))

else if akey contains "stringValue" then

(theObject's setStringValue:aCommend)

else if akey contains "textColor" then

set aColor to my returnColor(aCommend)

(theObject's setTextColor:aColor)

else if akey contains "backgroundColor" then

set aColor to my returnColor(aCommend)

(theObject's setBackgroundColor:aColor)

else if akey contains "drawsBackground" then

(theObject's setDrawsBackground:(my convertTextToBoolean(aCommend)))

else if akey contains "bordered" then

(theObject's setBordered:(my convertTextToBoolean(aCommend)))

else if akey contains "textAlignment" then

if aCommend = "Left" then

(theObject's setAlignment:(myApp's NSTextAlignmentLeft))

else if aCommend contains "Center" then

(theObject's setAlignment:(myApp's NSTextAlignmentCenter))

else if aCommend contains "Right" then

(theObject's setAlignment:(myApp's NSTextAlignmentRight))

end if

else if akey contains "fontName" then

try

set _pointSize to (theObject's |font|()'s pointSize()) as real

on error

set _pointSize to 14.0

end try

(theObject's setFont:(myApp's class "NSFont"'s fontWithName:aCommend |size|:_pointSize))

else if akey contains "fontSize" then

(theObject's setFont:(myApp's class "NSFont"'s userFontOfSize:(aCommend as number)))

else if akey contains "bezelStyle" then

set aCode to ("use scripting additions

use framework \"AppKit\"

current application's NSBezelStyle" & aCommend)

try

set _style to run script aCode

on error

log "ButtonStyle Err."

set _style to current application's NSBezelStyleRounded()

end try

(theObject's setBezelStyle:_style)

else if akey contains "Target" then

if aCommend = "me" then

(theObject's setTarget:me)

end if

else if akey contains "enabled" then

(theObject's setEnabled:(my convertTextToBoolean(aCommend)))

else if akey contains "swichState" then

set _state to 0

if "1ON On on" contains aCommend then set _state to 1

if "-1MIX Mix mix" contains aCommend then set _state to -1

(theObject's setState:_state)

else if akey contains "allowsMixedState" then

(theObject's setAllowsMixedState:(my convertTextToBoolean(aCommend)))

else if akey contains "state" then

(theObject's setState:(my convertTextToBoolean(aCommend)))

else if akey contains "action" then

(theObject's setAction:aCommend)

else if akey contains "buttonType" then

if aCommend = "switch" then

(theObject's setButtonType:(myApp's NSButtonTypeSwitch))

else if aCommend = "radio" then

(theObject's setButtonType:(myApp's NSButtonTypeRadio))

end if

else if akey contains "bezelColor" then

set aColor to my returnColor(aCommend)

(theObject's setBezelColor:aColor)

else if akey contains "pullsDown" then

(theObject's setPullsDown:(my convertTextToBoolean(aCommend)))

else if akey contains "autoenablesItems" then

(theObject's setAutoenablesItems:(my convertTextToBoolean(aCommend)))

else if akey contains "removeAllItems" then

(theObject's removeAllItems())

else if (akey contains "addTitleItem") then

(theObject's addItemsWithTitles:{aCommend})


else if akey contains "title" then --悪さをするので最後に

(theObject's setTitle:aCommend)

else

log ("未知のコードError : [" & aClass & "] " & oneLine)

end if


on error

log ("無効なコードError : [" & aClass & "] " & oneLine)

end try

end if

end repeat

return theObject

end makeInstance


on convertTextToNumber(_command)

-- テキスト から 数字のリスト を返す

--|| "{{123,456},{789.0,1111.1}}" => {123, 456, 789, 1111.1}

set _text to ""

repeat with oneChara in (characters of _command)

if "0123456789." contains oneChara then

set _text to _text & oneChara

else if "-" contains oneChara then

set _text to _text & " minus "

else

set _text to _text & " "

end if

end repeat

set _values to {}

set _minus to 1

repeat with _obj in (words of _text)

set _obj to _obj as text

if _obj = "minus" then

set _minus to -1

else

set _values to _values & {(_obj as number) * _minus}

set _minus to 1

end if

end repeat

return _values

end convertTextToNumber


on convertTextToBoolean(_command)

--テキスト から 真偽値(bool) を返す

-- ("Yes","yes","True","true"  =>  ture)  または  ("No","no","False","false"  =>  false) 

-- というか、true項目以外は全てfalse

set _flg to (_command contains "true") or (_command contains "yes")

--  _command as boolean でもよかったかな?

return _flg

end convertTextToBoolean


on returnColor(_command)

-- 色を表すテキスト から NSColorを作成して返す

(* // Default color //

"blackColor"

"blueColor"

"clearColor"

"cyanColor"

"darkGrayColor"

"grayColor"

"greenColor"

"lightGrayColor"

"magentaColor"

"orangeColor"

"purpleColor"

"redColor"

"whiteColor"

"yellowColor"

 // make color //

"red_green_blue_alpha_(数字,数字,数字,数字)" または

"red:数字,green:数字,blue:数字,alpha:数字" など。

数字には0.01.0の実数または整数が入る

不要な色は省略可能 *)

if _command ends with "Color" then

--Default color

set aCode to "use scripting additions

use framework \"AppKit\"

get (current application's class \"NSColor\"'s " & _command & "())"

try

set _color to run script aCode

on error

log "Default Color Err."

set _color to current application's class "NSColor"'s grayColor()

end try

else

--make color

set {_red, _green, _blue, _alpha} to {0.0, 0.0, 0.0, 1.0}

set _colorList to {}

repeat with _colorName in {"red", "green", "blue", "alpha"}

set _colorName to _colorName as text

set _c to offset in _command of _colorName

if _c > 0 then set _colorList to _colorList & {{_colorName, _c}}

end repeat

set _n to count of _colorList

if _n > 1 then

repeat with _i from 1 to (_n - 1)

repeat with _j from (_i + 1) to _n

if (item 2 of item _i of _colorList) > (item 2 of item _j of _colorList) then

set _d to item _i of _colorList

set item _i of _colorList to item _j of _colorList

set item _j of _colorList to _d

end if

end repeat

end repeat

end if

set _numberList to my convertTextToNumber(_command)

if (count of _numberList) = 0 then return (missing value)

repeat with _i from 1 to _n

set _colorName to (item 1 of item _i of _colorList) as text

if _colorName = "red" then

set _red to item _i of _numberList

else if _colorName = "green" then

set _green to item _i of _numberList

else if _colorName = "blue" then

set _blue to item _i of _numberList

else if _colorName = "alpha" then

set _alpha to item _i of _numberList

end if

end repeat

set _color to myApp's class "NSColor"'s colorWithCalibratedRed:_red green:_green blue:_blue alpha:_alpha

end if

return _color

end returnColor


----------// オブジェクトアイテムの作成 ここまで//


(* ウインドウバーの閉じるボタンアクションを受けた時 *)

on windowShouldClose:sender

set closeFlg to true

end windowShouldClose:


(* ウインドウを閉じるための実際のアクション *)

on closeWin:aWindow

aWindow's orderOut:me

if (name of myApp) ≠ "Script Editor" then quit me

end closeWin:

inserted by FC2 system