A version of NSPopUpButton

Learning to make a dialog with NSPopUpButton.
It also include NSBox, NSTextfield, NSWindowController and NSWindow.

I really like the result, and next will be to include a image/icon/logo on the right side.

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

property theComments : "This describe the content or purpose of this dialog..."
property arguments : missing value

on run
	set arguments to {label1:"Title 1", label2:"Title 2", comments:theComments}
	if current application's NSThread's isMainThread() as boolean then
		my performDialog:arguments
	else
		my performSelectorOnMainThread:"performDialog:" withObject:arguments waitUntilDone:true
	end if
end run

on performDialog:arguments
	set {theWidth, theHeight} to {600, 300}
	set theWindow to createWindowWithRect(0, 0, theWidth, theHeight)
	set windowController to createWindowController(theWindow)
	
	set theTextfield to createTextfield(arguments's comments, 20, 20, theWidth * 2 / 3, theHeight / 2)
	theWindow's contentView()'s addSubview:theTextfield
	
	set theBorderRect1 to createBorderRect(arguments's label1, "Menlo", 20, theHeight / 2 + 90, theWidth * 2 / 3, 50)
	set theBorderRect2 to createBorderRect(arguments's label2, "Menlo", 20, theHeight / 2 + 30, theWidth * 2 / 3, 50)
	theWindow's contentView()'s addSubview:theBorderRect1
	theWindow's contentView()'s addSubview:theBorderRect2
	
	set thePopUp1 to createPopup({"Red", "Green", "Blue"}, 25, theHeight / 2 + 85, theWidth / 2, 50)
	set thePopUp2 to createPopup({"Strawberry", "Rasberry", "Blackberry"}, 25, theHeight / 2 + 25, theWidth / 2, 50)
	theWindow's contentView()'s addSubview:thePopUp1
	theWindow's contentView()'s addSubview:thePopUp2
	
	windowController's |window|'s |center|()
	windowController's |window|'s makeKeyAndOrderFront:me
end performDialog:

on createTextfield(theString, xMin, yMin, xLen, yLen)
	set textFieldSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
	set theTextfield to current application's NSTextField's alloc()'s initWithFrame:textFieldSize
	theTextfield's setEditable:false
	theTextfield's setStringValue:theString
	theTextfield's setTextColor:(current application's NSColor's whiteColor)
	theTextfield's setBezeled:true
	theTextfield's setBezelStyle:(current application's NSTextFieldSquareBezel)
	theTextfield's setBackgroundColor:(current application's NSColor's blackColor)
	return theTextfield
end createTextfield

on createPopup(entryList, xMin, yMin, xLen, yLen)
	set popUpSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
	set thePopup to current application's NSPopUpButton's alloc()'s initWithFrame:popUpSize
	thePopup's addItemsWithTitles:entryList
	thePopup's selectItemWithTitle:(item 1 of entryList)
	return thePopup
end createPopup

on createBorderRect(title, |font|, xMin, yMin, xLen, yLen)
	set borderRectSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
	set theBorderRect to current application's NSBox's alloc()'s initWithFrame:borderRectSize
	theBorderRect's setTitle:title
	theBorderRect's setTitleFont:(current application's NSFont's fontWithName:|font| |size|:12)
	-- Specify the location of a box’s title with respect to its border.
	theBorderRect's setTitlePosition:(current application's NSBelowTop)
	-- theBorderRect's setTransparent:false
	return theBorderRect
end createBorderRect

on createWindowWithRect(xMin, yMin, xLen, yLen)
	set windowSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
	set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer)
	set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:yes
	return theWindow
end createWindowWithRect

on createWindowController(theWindow)
	set theController to current application's NSWindowController's alloc()'s initWithWindow:theWindow
end createWindowController

In this version I have add little more to the NSBox

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

property theComments : "This describe the content or purpose of this dialog..."
property arguments : missing value

on run
	set arguments to {label1:"Title 1", label2:"Title 2", comments:theComments}
	if current application's NSThread's isMainThread() as boolean then
		my performDialog:arguments
	else
		my performSelectorOnMainThread:"performDialog:" withObject:arguments waitUntilDone:true
	end if
end run

on performDialog:arguments
	set {theWidth, theHeight} to {600, 300}
	set theWindow to createWindowWithRect(0, 0, theWidth, theHeight)
	set windowController to createWindowController(theWindow)
	
	set theTextfield to createTextfield(arguments's comments, 20, 20, theWidth * 2 / 3, theHeight / 2)
	theWindow's contentView()'s addSubview:theTextfield
	
	set theBorderRect1 to createBorderRect(arguments's label1, "Menlo", 20, theHeight / 2 + 90, theWidth * 2 / 3, 50)
	set theBorderRect2 to createBorderRect(arguments's label2, "Menlo", 20, theHeight / 2 + 30, theWidth * 2 / 3, 50)
	theWindow's contentView()'s addSubview:theBorderRect1
	theWindow's contentView()'s addSubview:theBorderRect2
	
	set thePopUp1 to createPopup({"Red", "Green", "Blue"}, 25, theHeight / 2 + 85, theWidth / 2, 50)
	set thePopUp2 to createPopup({"Strawberry", "Rasberry", "Blackberry"}, 25, theHeight / 2 + 25, theWidth / 2, 50)
	theWindow's contentView()'s addSubview:thePopUp1
	theWindow's contentView()'s addSubview:thePopUp2
	
	windowController's |window|'s |center|()
	windowController's |window|'s makeKeyAndOrderFront:me
end performDialog:

on createTextfield(theString, xMin, yMin, xLen, yLen)
	set textFieldSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
	set theTextfield to current application's NSTextField's alloc()'s initWithFrame:textFieldSize
	theTextfield's setEditable:false
	theTextfield's setStringValue:theString
	theTextfield's setTextColor:(current application's NSColor's whiteColor)
	theTextfield's setBezeled:true
	theTextfield's setBezelStyle:(current application's NSTextFieldSquareBezel)
	theTextfield's setBackgroundColor:(current application's NSColor's blackColor)
	return theTextfield
end createTextfield

on createPopup(entryList, xMin, yMin, xLen, yLen)
	set popUpSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
	set thePopup to current application's NSPopUpButton's alloc()'s initWithFrame:popUpSize
	thePopup's addItemsWithTitles:entryList
	thePopup's selectItemWithTitle:(item 1 of entryList)
	return thePopup
end createPopup

on createBorderRect(title, |font|, xMin, yMin, xLen, yLen)
	set borderRectSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
	set theBorderRect to current application's NSBox's alloc()'s initWithFrame:borderRectSize
	theBorderRect's setTitle:title
	theBorderRect's setTitleFont:(current application's NSFont's fontWithName:|font| |size|:12)
	-- Specify the location of a box’s title with respect to its border.
	theBorderRect's setTitlePosition:(current application's NSBelowTop)
	-- theBorderRect's setTransparent:false
	theBorderRect's setBoxType:(current application's NSBoxCustom)
	theBorderRect's setBorderType:(current application's NSLineBorder)
	theBorderRect's setBorderWidth:0.5
	theBorderRect's setCornerRadius:5
	theBorderRect's setFillColor:(current application's NSColor's lightBlueColor)
	theBorderRect's setBorderColor:(current application's NSColor's orangeColor)
	return theBorderRect
end createBorderRect

on createWindowWithRect(xMin, yMin, xLen, yLen)
	set windowSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
	set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer)
	set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:yes
	return theWindow
end createWindowWithRect

on createWindowController(theWindow)
	set theController to current application's NSWindowController's alloc()'s initWithWindow:theWindow
end createWindowController