Re: Variable keeps changing with date manipulation in Snow Leopard
Re: Variable keeps changing with date manipulation in Snow Leopard
- Subject: Re: Variable keeps changing with date manipulation in Snow Leopard
- From: Deivy Marck Petrescu <email@hidden>
- Date: Sat, 12 Sep 2009 22:44:32 -0400
On Sep 12, 2009, at 20:48, Mark J. Reed wrote:
I don't have access to SL to test, but I suspect that dates are
objects now, handled by reference, and you need to use "copy" instead
of "set" on the calls to MakeTimesRelativeToDate if you want to get
different dates instead of just a whole bunch of variables pointing to
DateVar.
On Sat, Sep 12, 2009 at 3:59 PM, Max Bonilla
<email@hidden> wrote:
Hello,
Adding to the discussion currently going on. I’m having a problem
with a
script in Snow Leopard. I wrote a handler to manipulate dates
according (I
think) to the new requirements, but, strangely, the variables I use
can’t
hold the values well. In the handler below (createRandomTime) the
variables
change values when they should not. For example, the variable
LunchEndRelV
should have a value of 1:30 PM, and it does when it is created.
But you’ll
notice at the end of the handler, the time stamp in it has changed
to the
time of the last variable created using the MakeTimesRelativeToDate
handler
(which is startTime, and not LunchEndRelV). (In the snippet below,
I’ve
eliminated most of the handlers in the original script replacing
them with
sample data).
on createRandomTime(AvailableDates, NonAvailablestartTime,
NonAvailableEndTime, DayBeginsV, DayEndsV, lunchbeginsv, LunchEndsV)
set DayStart to 9
set DayEndNUM to 16
set xV to random number from 1 to (count of AvailableDates)
set DateVar to item xV of AvailableDates
set HourV to (random number from DayStart to (DayEndNUM))
set HourV to HourV & ":00" as string
set LunchStartRelV to my MakeTimesRelativeToDate(lunchbeginsv,
DateVar)
--Lunch ends at 1:30 PM on the next variable (LunchEndRelV) and
it
should stay that way for the duration of the script. Yet, see next
note
below.
set LunchEndRelV to my MakeTimesRelativeToDate(LunchEndsV,
DateVar) -->
time 1:30 PM of a given date
set DayStartRelV to my MakeTimesRelativeToDate(DayBeginsV,
DateVar)
set DayEndRelV to my MakeTimesRelativeToDate(DayEndsV, DateVar)
set startTime to my MakeTimesRelativeToDate(HourV, DateVar)
--It seems that the time keeps changing in all variables
depending on
the values that are sent to the handler MakeTimesRelativeToDate,
and that
the variables just follow that. They are not "set" to stay;
instead, they
--change internally. For example if you step through this handler
and look
at the value of LunchEndRelV, you'll notice that lunch ends at 1:30
PM the
first (and only) time that the
--MakeTimesRelativeToDate is invoked for setting the LunchEndRelV
variable.
--But when you look at the value of LunchEndRelV in the last
instance
(below), the value has changed (as have all variables in the
handler that
use the MakeTimesRelativeToDate) even if no explicit
--call was made to change the value of LunchEndRelV.
return LunchEndRelV --> no longer 1:30 PM
end createRandomTime
The MakeTimesRelativeToDate handler has this code:
on MakeTimesRelativeToDate(TimeStringVar, DateStamp)
set TimePropertiesV to my ConvertTimeToNumbers(TimeStringVar)
set hours of DateStamp to item 1 of TimePropertiesV
set minutes of DateStamp to item 2 of TimePropertiesV
return DateStamp
end MakeTimesRelativeToDate
And a functional sample script that shows the problem is this:
on ConvertTimeToNumbers(TimeString)
if TimeString contains ":" then
--This section makes a time string into a number (hours only)
set x to offset of ":" in TimeString
set HourStringV to (characters 1 thru (x - 1)) of
TimeString as
string
set HourVar to HourStringV as number
--This section makes a time string into a number (minutes
only)
set MinutesStringV to (characters (x + 1) thru (x + 2)) of
TimeString as string
set MinutesVar to MinutesStringV as number
set TimeStringV to {HourVar, MinutesVar}
else
try
set HourVar to TimeString as number
on error
set HourVar to 0
end try
set TimeStringV to {HourVar, 0}
end if
end ConvertTimeToNumbers
on MakeTimesRelativeToDate(TimeStringVar, DateStamp)
set TimePropertiesV to my ConvertTimeToNumbers(TimeStringVar)
set hours of DateStamp to item 1 of TimePropertiesV
set minutes of DateStamp to item 2 of TimePropertiesV
return DateStamp
end MakeTimesRelativeToDate
on createRandomTime(AvailableDates, NonAvailablestartTime,
NonAvailableEndTime, DayBeginsV, DayEndsV, lunchbeginsv, LunchEndsV)
set DayStart to 9
set DayEndNUM to 16
set xV to random number from 1 to (count of AvailableDates)
set DateVar to item xV of AvailableDates
set HourV to (random number from DayStart to (DayEndNUM))
set HourV to HourV & ":00" as string
set LunchStartRelV to my MakeTimesRelativeToDate(lunchbeginsv,
DateVar)
--Lunch ends at 1:30 PM on the next variable (LunchEndRelV) and
it
should stay that way for the duration of the script. Yet, see next
note
below.
set LunchEndRelV to my MakeTimesRelativeToDate(LunchEndsV,
DateVar)
set DayStartRelV to my MakeTimesRelativeToDate(DayBeginsV,
DateVar)
set DayEndRelV to my MakeTimesRelativeToDate(DayEndsV, DateVar)
set startTime to my MakeTimesRelativeToDate(HourV, DateVar)
--LunchEndRelV changes to a different time every time the
script is run
(because HourV is a random number). But this is not what I want.
return LunchEndRelV
end createRandomTime
on run
set DayBeginsV to "8:30"
set DayEndsV to "16:30"
set lunchbeginsv to "12:00"
set LunchEndsV to "13:30"
set NonAvailablestartTime to {date "Monday, November 23, 2009
12:00:00
PM", date "Wednesday, November 7, 2007 1:30:00 PM", date "Sunday,
April 19,
2009 9:15:00 AM", date "Monday, October 8, 2007 9:00:00 AM"}
set NonAvailableEndTime to {date "Monday, November 23, 2009
1:00:00 PM",
date "Wednesday, November 7, 2007 2:30:00 PM", date "Sunday, April
19, 2009
10:05:00 AM", date "Monday, October 8, 2007 12:00:00 PM"}
set BlockedDatesV to {date "Thursday, March 19, 2009 12:00:00
AM", date
"Thursday, September 25, 2008 12:00:00 AM", date "Tuesday, May 12,
2009
12:00:00 AM", date "Tuesday, April 7, 2009 12:00:00 AM"}
set AvailableDates to {date "Monday, September 14, 2009
12:00:00 AM",
date "Tuesday, September 15, 2009 12:00:00 AM", date "Wednesday,
September
16, 2009 12:00:00 AM", date "Thursday, September 17, 2009 12:00:00
AM", date
"Friday, September 18, 2009 12:00:00 AM"}
set RandomTime to createRandomTime(AvailableDates,
NonAvailablestartTime, NonAvailableEndTime, DayBeginsV, DayEndsV,
lunchbeginsv, LunchEndsV)
end run
Of course, the RandomTime variable is not supposed to receive the
LunchEndRelV value; I changed the handler to do so as an
illustration of the
problem.
The script worked really well before, but under Snow Leopard the
variables
are no longer holding the value in the way I would expect them. I
think it
must have something to do with changing the property of the date,
as Snow
Leopard is supposed to handle dates now, but I am not sure. The
documentation and the discussion on dates on the list has not
helped me see
the cause. Can someone explain why and perhaps suggest a way to
make the
variables hold the times? Thanks.
Max
--
Mark J. Reed <email@hidden>
_______________________________________________
Max,
Mark is absolutely right.
And the problem has nothing to do with SL. It also happens in Leopard.
To fix the script change your handler :
on MakeTimesRelativeToDate(TimeStringVar, DateStamp)
copy DateStamp to nuDate
set TimePropertiesV to my ConvertTimeToNumbers(TimeStringVar)
set hours of nuDate to item 1 of TimePropertiesV
set minutes of nuDate to item 2 of TimePropertiesV
return nuDate
end MakeTimesRelativeToDate
Deivy Petrescu
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden