Variable Name Change

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 ((weekday of Currentdate) as text)}

log Currentdate & " " & Wday & " " & Jan1date

set {month of Jan1date, day of Jan1date} to {January, 1}

log Currentdate & " " & Wday & " " & Jan1date & " " & Currentdate2

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

Did you try this?

copy Currentdate to Currentdate2 ?

Thanks I had thought set the value would work and in fact wanted to avoid yet a another variable. Anyway “copy” does the job for now

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.

Copy in: Commands Reference

Set in: Commands Reference

As an aside, it’s gonna take a while to get used to the way URLs are handled.

1 Like

You can avoid it easy:

Untitled.applescript (253 Bytes)

tell (current date) to tell (it - (its time))
copy it to Currentdate
set Jan1date to it
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

Hi KniazidisR.

You can post code on this new MacScripter site by placing three backticks above and below it thus:

```
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
```

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
1 Like

Thank you for this info, Nigel.

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, set can 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
1 Like

Thank you all as always learn a lot on this site

Including how to highlight the code one posts that was far from obvious.