A simple example to use NSSwitch to control something…
It use IBAction with property theSwitch, we ask for the state() in the action handler
and later use if/else to get its status.
use framework "Foundation"
use framework "AppKit"
use scripting additions
property arguments : missing value
property theWindow : missing value
property theSwitch : missing value
on run
try
if my NSThread's isMainThread() as boolean then
my performDialog:arguments
else
my performSelectorOnMainThread:"performDialog:" withObject:"arguments" waitUntilDone:true
end if
on error the errorMessage number the errorNumber
set the ErrorText to "Error: " & the errorNumber & ". " & the errorMessage
return the ErrorText
end try
end run
on performDialog:arguments
-- Window size.
set {width, height} to {600, 300}
-- We make a box view with label.
set {offsetX, offsetY, boxWidth, boxHeight} to {24, height / 2 + 18, width - 50, height - (height / 2) - 34}
set textFieldBox1 to createBoxWithRect("Label 1", "System Small", 14, offsetX, offsetY, boxWidth, boxHeight)
set {offsetX, offsetY, boxWidth, boxHeight} to {24, 34, width - 50, height / 2 - 34}
set textFieldBox2 to createBoxWithRect("Label 2", "System Small", 14, offsetX, offsetY, boxWidth, boxHeight)
set theSwitch to createSwitch(50, 40, 100, 100, "switchAction:")
set subviewItems to {textFieldBox1, textFieldBox2, theSwitch}
set theWindow to createWindowWithRectAndSubview(subviewItems, 0, 0, width, height)
theWindow's |center|()
theWindow's makeKeyAndOrderFront:me
end performDialog:
on switchAction:sender
set theState to theSwitch's state()
if theState is true as integer then
display dialog "ON"
else
display dialog "OFF"
end if
end switchAction:
on createSwitch(x, y, width, height, selector)
set frameSize to current application's NSMakeRect(x, y, width, height)
set theSwitch to current application's NSSwitch's alloc()'s initWithFrame:frameSize
theSwitch's setTarget:me
theSwitch's setAction:selector
theSwitch's setState:(current application's NSControlStateValueOff)
return theSwitch
end createSwitch
on createTextFieldWithRect(x, y, width, height)
set textFieldSize to current application's NSMakeRect(x, y, width, height)
set theTextField to current application's NSTextField's alloc()'s initWithFrame:textFieldSize
return theTextField
end createTextFieldWithRect
on createBoxWithRect(title, fontName, fontSize, x, y, width, height)
set boxSize to current application's NSMakeRect(x, y, width, height)
set theBox to current application's NSBox's alloc()'s initWithFrame:boxSize
theBox's setTitle:title
theBox's setTitleFont:(current application's NSFont's fontWithName:fontName |size|:fontSize)
-- Specify the location of a box’s title with respect to its border.
theBox's setTitlePosition:(current application's NSAtTop)
-- theBox's setTransparent:false
-- theBox's setBoxType:(current application's NSBoxCustom)
theBox's setBorderType:(current application's NSLineBorder)
theBox's setBorderWidth:0.5
theBox's setCornerRadius:1
-- theBox's setFillColor:(current application's NSColor's lightBlueColor)
-- theBox's setBorderColor:(current application's NSColor's orangeColor)
return theBox
end createBoxWithRect
on createView(x, y, width, height, subviews)
set viewRectSize to current application's NSMakeRect(x, y, width, height)
set theView to current application's NSView's alloc()'s initWithFrame:viewRectSize
theView's setSubviews:subviews -- list
return theView
end createView
on createWindowWithRectAndSubview(subviewItems, x, y, width, height)
set windowSize to current application's NSMakeRect(x, y, width, height)
set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer) + (current application's NSWindowStyleMaskMiniaturizable as integer) + (current application's NSWindowStyleMaskResizable as integer)
set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:true
repeat with anSubview in subviewItems
(theWindow's contentView()'s addSubview:anSubview)
end repeat
return theWindow
end createWindowWithRectAndSubview