スクリプトエディターでNSWindow作成の一時まとめ [AppleScript辞書はつくれるか?]
use AppleScript version "2.7" use scripting additions use framework "Foundation" use framework "AppKit"
property debugMode : true global theWindow global closeFlg global currentAppName --スクリプトエディタを使用して実行しているのか、アプリケーション済から起動されたかの判断用
on run set closeFlg to false -- set currentAppName to name of (current application) if currentAppName ≠ "Script Editor" then set debugMode to false if debugMode then --デバグモードの状態を表示 log "debugMode : ON" else log "debugMode : Off" end if -- my performSelectorOnMainThread:"mainMakeObject:" withObject:(missing value) waitUntilDone:true -- log "End" end run
on mainMakeObject:inputObj -- メインルーチン set aTitle to "test window" set aRect to {0, 0, 300, 450} set theWindow to my makeNewWindow(aTitle, aRect, true) -- my makeObject() -- if debugMode then repeat if closeFlg then my closeWin:theWindow exit repeat end if delay 0.2 end repeat end if end mainMakeObject:
on makeNewWindow(aTitle, aRect, moveCenter) set {windowX, windowY, windowW, windowH} to aRect set theRect to current application's NSMakeRect(windowX, windowY, windowW, windowH) set aScreen to current application's class "NSScreen"'s mainScreen() set aStyle to (current application's NSWindowStyleMaskTitled as integer) set aStyle to aStyle + (current application's NSWindowStyleMaskClosable as integer) set aStyle to aStyle + (current application's NSWindowStyleMaskMiniaturizable as integer) set aStyle to aStyle + (current application's NSWindowStyleMaskResizable as integer) set aBacking to current application's NSBackingStoreBuffered set aWindow to (current application's class "NSWindow"'s alloc()'s initWithContentRect:theRect styleMask:aStyle backing:aBacking defer:false screen:aScreen)'s autorelease() tell aWindow setAlphaValue_(0.0) --作成時のチラつき回避のため、いったん不透明度を0.0にして透明に setDelegate_(me) setTitle_(aTitle) setMinSize_(current application's NSMakeSize(windowW, windowH)) setMaxSize_(current application's NSMakeSize(10000, 10000)) setBackgroundColor_(current application'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 -- |windowをセンターに移動| aWindow's |center|() --システム任せで中央に移動(※少し上にずれてしまう)下記のScreen命令で取得できなかった場合の避難処理 -- set theScreen to current application's class "NSScreen"'s screens() set {windowCenterX, winsowCenterY} to {windowX + (windowW / 2), windowY + (windowH / 2)} repeat with screenNo from 1 to (count of theScreen) set {{screenX1, screenY1}, {screenWidth, screenHeight}} to (item screenNo of theScreen)'s frame() set {screenX2, screenY2} to {screenX1 + screenWidth, screenY1 + screenHeight} if (windowCenterX > screenX1) and (windowCenterX < screenX2) and (winsowCenterY > screenY1) and (winsowCenterY < screenY2) then set originX to screenX1 + (screenWidth / 2) - (windowW / 2) set originY to screenY1 + (screenHeight / 2) - (windowH / 2) (aWindow's setFrameOrigin:(current application's NSMakePoint(originX, originY))) exit repeat end if end repeat end if -- aWindow's setAlphaValue:1.0 --不透明度を戻して表示 -- return aWindow end makeNewWindow
(* 配置する各オブジェクトを配置する *) on makeObject() set {{windowX, windowY}, {windowWidth, windowHeight}} to theWindow's contentView's frame() try -- 新しくオブジェクトを配置する部分 -------------------- set theRect to current application's NSMakeRect(90, 5, 120, 24) set button1 to current application's class "NSButton"'s alloc()'s initWithFrame:theRect tell button1 setBezelStyle_(1) setButtonType_(0) setTitle_("閉じる") setTarget_(me) setAction_("action1:") end tell theWindow's contentView()'s addSubview:button1 ----------------------------------------------------------- on error errText log errText end try end makeObject
on action1:sender set closeFlg to true end action1:
(* ウインドウバーの閉じるボタンアクションを受けた時 *) on windowShouldClose:sender set closeFlg to true end windowShouldClose:
(* ウインドウを閉じるための実際のアクション *) on closeWin:aWindow aWindow's orderOut:me if currentAppName ≠ "Script Editor" then quit me end closeWin: |
コメント 0