I have an NSdatepicker object with its value bound back to the AppDelegate property theDate like
property theDate: (current date) - (24*60*60)
The goal is to have the date selector preselected to ‘yesterday’ relative to the time the user runs the application. In Xcode, this works fine. Unfortunately, when I archive the project to a local application it is apparently? hard-coding in my ‘yesterday’ date.
For example, I previously archived a version of the app on 11/28…so, the date selector is preset to 11/27. But, if I ran it today it still shows 11/27.
I just re-archived the app again and it shows 12/1 as expected when run. Tomorrow it should show 12/2, but it will still show 12/1.
Is the some other way to do what I’m trying to achieve?
Hi.
The value in a property declaration is set when the script’s compiled. If you want the value to be the date when the script’s run, you have to set it with a command that’s executed when the script’s run:
property theDate : missing value -- Or any placeholder value.
set theDate to (current date) - days -- 'days' is an AS "constant" equal to 86400.
Thanks for the reply.
What you’re saying makes complete sense. But, I’m not sure how to do it.
I’m only able to get the NSdatepicker to take a value I give it from the AppDelegate if I assign it in the property declaration. If I set the property to ‘missing value’ then, the MainMenu.xib just takes its value from whatever I have in date field under Show Attributes of the element.
I just verified this by putting my theDate ‘yesterday’ declaration inside the applicationWillFinishLaunching_ handler like so
on applicationWillFinishLaunching_(aNotification)
property theDate: (current date) - (24*60*60)
I verified with a dialog that theDate was properly set. But, the NSdatepicker did not pick it up as it does when assigned in the property declaration.
Catch-22? Or am I missing something? Im still pretty green at this obj-c stuff.
You’re missing what Nigel said. The property declaration goes at the beginning of the script, and then you set its value in applicationWillFinishLaunching:.
Sorry if I wasn’t clear–that’s exactly what I said I did in my last post, and explained why it wasn’t working.
To help illustrate further, I just created a new project just to test this issue–here’s the complete app delegate:
script AppDelegate
property parent : class "NSObject"
property theDate: missing value --variable taken from main_menu.xib but also needs to show current date initially
property search_month: "" --used for testing to show the integer value of the month selected of theDate
-- IBOutlets
property theWindow : missing value
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
set theDate to (current date)
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
on showtheDate_(sender) --shows the integer value of the month selected in the main_menu.xib when the button is clicked
set search_month to month of (theDate as date) as integer
display alert search_month
end showtheDate_
end script
in the main_menu.xib there is only an NSdate picker bound to theDate and an NSbutton that calls showtheDate_ which should show the integer value of the month of (current date)
When I run the application, there is no preselected date in the selector object–just the date defined in the atrributes selector of the NSdatepicker.
It is noteworthy that the VALUE is passed correctly. If you run the app and don’t change the datepicker, the actual current month is returned. It is just not shown in the NSdatepicker object. The only way I can get the value to show in the NSdatepicker is to define it in the property declaration–which I’ve been told is written at compile time instead of script run time.
That’s not setting the value anywhere. As I said, set its value in applicationWillFinishLaunching:.
so, am I to understand that I am not “setting” the value here by “setting the value” here? English is my first language. But, this is frustrating.
on applicationWillFinishLaunching_(aNotification)
set theDate to (current date)
end applicationWillFinishLaunching_
Sorry, I see the issue now. Your code is creating a new local variable called theDate. Change it to:
set my theDate to (current date)
wow. that was simple.
I thought there was an implicit bridging between ASOC and applescript variables. Without the ‘my’ pronoun, was I addressing two different variables? I have not been able to find any documentation on using ‘my’ when addressing variables in Xcode. This is not something I’ve ever seen in vanilla Applescript.
thanks.
The same thing can happen in vanilla AS. It’s safest to always use “my” when setting a property within a handler.