AppleScript名:Segmented Controlを表示する |
— Created 2017-12-20 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" property NSView : a reference to current application’s NSView 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 NSWindowController : a reference to current application’s NSWindowController property NSSegmentedControl : a reference to current application’s NSSegmentedControl property windisp : false property selSeg : 0 set aWidth to 500 set aHeight to 100 set segTitleList to {"First Segment", "Second Segment", "Third Segment", "Forth Segment"} set aRes to dispSegControl(aWidth, aHeight, "Segemented Control", "OK", 180, segTitleList) of me on dispSegControl(aWidth as integer, aHeight as integer, aTitle as text, aButtonMSG as text, timeOutSecs as number, segTitleList) –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 selSeg to 0 set (my windisp) to true –Segmented Controlをつくる set aSeg to makeSegmentedControl(segTitleList, aWidth, aHeight) 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:aSeg 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 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 return (selSeg + 1) end dispSegControl –Button Clicked Event Handler on clicked:aSender set (my windisp) to false end clicked: on clickedSeg:aSender set aSel to aSender’s selectedSegment() set selSeg to aSel end clickedSeg: –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: on makeSegmentedControl(titleList, aWidth, aHeight) set aLen to length of titleList set aSeg to NSSegmentedControl’s alloc()’s init() aSeg’s setSegmentCount:aLen set aCount to 0 repeat with i in titleList set j to contents of i (aSeg’s setLabel:j forSegment:aCount) set aCount to aCount + 1 end repeat aSeg’s setTranslatesAutoresizingMaskIntoConstraints:false aSeg’s setSegmentStyle:(current application’s NSSegmentStyleTexturedRounded) aSeg’s setFrame:(current application’s NSMakeRect(20, aHeight – 60, aWidth, aHeight – 40)) aSeg’s setTrackingMode:0 aSeg’s setTarget:me aSeg’s setAction:"clickedSeg:" aSeg’s setSelectedSegment:0 return aSeg end makeSegmentedControl |
More from my site
(Visited 23 times, 1 visits today)