I have a problem with variable names. If I create a variable say “CurrentDate” and then copy that variable to another variable say “Jan1Date” and the change elements of the second variable it seems that the original variable is also changed. I can of course avoid the problem by creating another variable from the same source as the 1st one, but this seems to be very clumsy. I assume I ma missing something very simple and would appreciate some guidance.
set Currentdate to ((current date ) - (time of (current date )))
set Currentdate2 to Currentdate
log Currentdate
set {Jan1date, Wday} to {Currentdate, text 1 thru 3 of ((weekdayof Currentdate) astext)}
log Currentdate & " " & Wday & " " & Jan1date
set {monthof Jan1date, day of Jan1date} to {January, 1}
Dates are objects which means the set x to y statement copies a reference to the object rather than the date object its self.
Use copy y to x to duplicate the date object.
set Currentdate to ((current date) - (time of (current date)))
copy Currentdate to Currentdate2
log Currentdate
copy {Currentdate, text 1 thru 3 of ((weekday of Currentdate) as text)} to {Jan1date, Wday}
log Currentdate & " " & Wday & " " & Jan1date
set {month of Jan1date, day of Jan1date} to {January, 1}
log Currentdate & " " & Wday & " " & Jan1date & " " & Currentdate2
The set command points both variables to the same thing. the copy command points the second variable to the value. It sounds like it’s exactly what you want.
You should read Apple’s discussions on each command.
This has a similar effect to the old [applescript] tags , with an “Open in Script Editor” button beneath the code:
tell (current date) to tell (it - (its time))
copy {it, it} to {Currentdate, Jan1date}
end tell
tell Currentdate to set Wday to its weekday as text
tell Jan1date to set {its month, day} to {January, 1}
return Currentdate & " " & Wday & " " & Jan1date
tell (current date) to copy {it, (it - (its time))} to {Currentdate, Jan1date}
tell Currentdate to set Wday to its weekday as text
tell Jan1date to set {its month, day} to {January, 1}
return Currentdate & " " & Wday & " " & Jan1date
In this case, of course, setcan be used, because the date math creates a new date object anyway. It’s only when changing a date object’s properties that having two variables set to the same object becomes relevant.
tell (current date) to set {Currentdate, Jan1date} to {it, (it - (its time))}
tell Currentdate to set Wday to its weekday as text
tell Jan1date to set {its month, day} to {January, 1}
return Currentdate & " " & Wday & " " & Jan1date