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