Simple date picker

Hi, I have never done a date picker before but I was interesting to know how it works.
So I made a little example as runModal in NSAlert with accessoryvew.

The code work it will return the date I pick in date picker view. But it will not display it
in the view so I have not figure out yet why… or what I have missing.

Ps.
The problem was Appearance mode: (I used first Light mode), but change to (Dark mode)
So I guess the solution would be to change the background colour of the date picker.

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

(**
* NSDatePicker
**
* A display of a calendar date with controls for editing the date value.
*)

property arguments : missing value
property returnCode : missing value
property cancelButton : "Cancel"

on run
	try
		set arguments to {setDate:(current date) + (days * 1), buttons:{"OK", "Cancel"}}
		
		if its NSThread's isMainThread() as boolean then
			my performDialog:arguments
		else
			my performSelectorOnMainThread:"performDialog:" withObject:arguments waitUntilDone:true
		end if
		
		if button returned of returnCode is cancelButton then error number -128
		return returnCode
	on error error_message number error_number
		tell me to activate
		display alert "" & error_number & "" message error_message
		return returnCode
	end try
end run

on performDialog:arguments
	set theDatePicker to createDatePicker(10, 10, 400, 200, arguments's setDate)
	set theView to createView(0, 0, 400, 200)
	theView's setSubviews:{theDatePicker}
	
	set theAlert to createAlert("The message...", "", theView, arguments's buttons)
	
	set button to theAlert's runModal()
	set buttonArray to (arguments's buttons)'s reverseObjectEnumerator()'s allObjects()
	set button to item (button mod 1000 + 1) of buttonArray
	set theDate to theDatePicker's dateValue()
	set its returnCode to {button returned:button as text, |date value|:theDate as date}
end performDialog:

on createDatePicker(x, y, width, height, theDateValue)
	set pickerRectSize to current application's NSMakeRect(x, y, width, height)
	set theDatePicker to current application's NSDatePicker's alloc()'s initWithFrame:pickerRectSize
	theDatePicker's setDatePickerStyle:(current application's NSClockAndCalendarDatePickerStyle)
	theDatePicker's setDatePickerElements:((current application's NSYearMonthDayDatePickerElementFlag) + (current application's NSHourMinuteSecondDatePickerElementFlag as integer))
	theDatePicker's setDateValue:theDateValue
	return theDatePicker
end createDatePicker

on createView(x, y, width, height)
	set viewRectSize to current application's NSMakeRect(x, y, width, height)
	set theView to current application's NSView's alloc()'s initWithFrame:viewRectSize
	return theView
end createView

on createAlert(theMessage, theInfo, theView, buttons)
	set theAlert to current application's NSAlert's alloc()'s init()
	theAlert's setMessageText:theMessage
	-- Set the alert without icon.
	theAlert's setIcon:(current application's NSImage's new())
	theAlert's setInformativeText:theInfo
	theAlert's setAccessoryView:theView
	
	repeat with anItem in reverse of (buttons as list)
		(theAlert's addButtonWithTitle:anItem)
	end repeat
	return theAlert
end createAlert

This is only test code to use NSWindow to set subview of NSView with NSDatePicker.
Each method is a handler that is used in performDialog handler.

The approach is simply… if we like to test other view and control it would be simple
to change the createDatePicker handler to something else. For me I find this to be much easier
to understand Apple’s frameworks.

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

property arguments : missing value

on run
	if its 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 theDatePicker to createDatePicker(10, 10, 400, 150, current date)
	set theView to createView(0, 0, 300, 180, {theDatePicker})
	set theWindow to createWindowWithRectAndSubview({theView}, 0, 0, 300, 200)
	theWindow's |center|()
	theWindow's makeKeyAndOrderFront:me
end performDialog:

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

on createDatePicker(x, y, width, height, theDateValue)
	set pickerRectSize to current application's NSMakeRect(x, y, width, height)
	set theDatePicker to current application's NSDatePicker's alloc()'s initWithFrame:pickerRectSize
	theDatePicker's setDatePickerStyle:(current application's NSClockAndCalendarDatePickerStyle)
	theDatePicker's setDatePickerElements:((current application's NSYearMonthDayDatePickerElementFlag) + (current application's NSHourMinuteSecondDatePickerElementFlag as integer))
	theDatePicker's setDateValue:theDateValue
	theDatePicker's setDrawsBackground:true
	theDatePicker's setBezeled:true
	theDatePicker's setBordered:true
	return theDatePicker
end createDatePicker