Update of NSSlider

I didn’t know the NSSlider could use setFrame I didn’t find it, So here is a update of that handler. NSSlider have other options but I think that could be done elsewhere.

on createSlider(sliderValue, minValue, maxValue, x, y, width, height, selector)
	set frameSize to current application's NSMakeRect(x, y, width, height)
	set theSlider to current application's NSSlider's sliderWithValue:sliderValue minValue:minValue maxValue:maxValue target:me action:selector
	theSlider's setFrame:frameSize
	return theSlider
end createSlider

Here is examples…

use framework "Foundation"
use framework "AppKit"
use scripting additions

property arguments : missing value
property theWindow : missing value

property theSwitch : missing value
property theSlider : missing value
property theSliderValue : 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 {defaultValue, minValue, maxValue} to {0, 0, 100}
	set theSlider to createSlider(defaultValue, minValue, maxValue, 170, 80, 250, 24, "sliderAction:")
	set theSliderValue to createTextFieldWithRect(width - 100, 80, 55, 24)
	theSliderValue's setStringValue:defaultValue
	
	set subviewItems to {textFieldBox1, textFieldBox2, theSwitch, theSlider, theSliderValue}
	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
		set theState to "On"
	else
		set theState to "Off"
	end if
	display dialog "The state of Switch is: " & theState & linefeed & "The value of slider is: " & theSliderValue's intValue()
end switchAction:

on sliderAction:sender
	set theNumber to sender's intValue()
	theSliderValue's setStringValue:theNumber
end sliderAction:

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)
	theSwitch's setAutoresizingMask:((current application's NSViewMaxXMargin as integer) + (current application's NSViewMinYMargin as integer))
	return theSwitch
end createSwitch

on createSlider(sliderValue, minValue, maxValue, x, y, width, height, selector)
	set frameSize to current application's NSMakeRect(x, y, width, height)
	set theSlider to current application's NSSlider's sliderWithValue:sliderValue minValue:minValue maxValue:maxValue target:me action:selector
	theSlider's setFrame:frameSize
	return theSlider
end createSlider

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

1 Like