Date Picker

Hi

I need to set a date and time. I want to use the Date Picker. I can’t find the proper syntax to set it to the current time and date upon openWindow, and then to extract it after Window closed.

Yarnin

It works pretty much like other controls:

on awake from nib the_object
	set object_name to name of the_object as Unicode text
	if object_name = "date_picker" then
		set content of the_object to (current date)
	end if
end awake from nib

on clicked the_object
	set object_name to name of the_object as Unicode text
	if object_name = "date_picker" then
		set the_date to content of the_object
		log the_date
	end if
end clicked

Jon

I guess I wasn’t cleer. so let me try again. (It is just as well can be the way around.) The DatePicker is a cell class from the NIB (it is a date formated text view from the Interface builder app.) The code I try is:


on clicked theobject
   set date of date cell "EndDate" of window "Main" to current date
end clicked

but it doesn’t work.

Yarnin

Right, I understood the first time – and gave you the proper code, did you try it? The code I posted works fine for NSDatePicker objects. Place an NSDatePicker into your NIB window in IB then, with the NSDatePicker still selected, select the AppleScript section of the Info palette and name the object “date_picker” (or whatever) and enable the “Action>Clicked” and the “NIB>awake from nib” commands and link them to your main AppleScript and then, in that script, use the code I posted above (adjust the name if you choose something other than “date_picker”, of course). This will set the date of the date picker to the current date when the NIB is unarchived and, any time it is clicked, it will get the current value as a date object.

Jon

Sorry Jon if I was rude - did not mean to. It looks like a long code for somthind rather simple. Isn’t there a more simple way to do it? more like the way of the code I wrote? - but diddn’t work.

Thank You

Um, the code I provided is not very complex and is much more robust than the code you provided. Your “on clicked” handler only allows for the possibility of one button or object that allows you to click on it. In my code, you can have many buttons and, by testing for the name of the object, perform the desired action. Anyway, here’s a quick project that demonstrates how to use the NSDatePicker: DatePicker.zip.

To put a fine point on it, this is what you want:

--set the content:
set content of control "EndDate" of window "Main" to (current date)

--get the content:
set the_date to content of control "EndDate" of window "Main"

Jon

Thank you very very much. Now it all make saneness. Your code is indeed not that complex and it is very useful. I will use it. Your second code is the one I was looking for, I need to set the date for current date upon App launching.

So again, thank you very much.
Yarnin