Array tell targets and `it`

I ran across this AppleScript oddity today. If I write this:

set abc to {1, 2, 3, 4}

tell item 2 of abc
	it --> 2
end tell

It works as you might expect and returns 2, the value of the second item of abc.

But if you try and increment the value of it, AppleScript refuses to compile it:

set abc to {1, 2, 3, 4}

tell item 2 of abc
	set it to it + 1
end tell

Anyone have any ideas?

I have no idea why that does not compile, but this works:

set abc to {1, 2, 3, 4}
tell abc
	set item 2 to (item 2) + 1
end tell

I haven’t used a tell with a list before. What is the advantage that I’m missing? :smile:

I assume the compiler reckons you’re trying to directly set a value for it, rather than what it represents, and won’t allow it. You get the same error if you try to assign any value to it or me.

I was producing a video and I had code that read like this:

set item 2 of abc to item 2 of abc + 1
item 2 of abc
...

I wanted to somehow shorten the code and make it more readable and had the idea that this might work:

tell item 1 of abc
    set it to it + 1
    ...
end tell

I’m not sure this is any more readable, but its shorter. Unfortunately, the compiler stopped me.

I experimented with this a bit and it seems I might not correctly understand what “it” is.

To be more specific about what Shane said the “it” can not appear by itself on the left side of the “to” but a part of the “it” can be referenced on the left side of the “to.” What fails is referencing “it” directly.

This works: set item 2 of it to (item 2 of it) + 1

This won’t even compile: set it to (item 2 of it) + 1

The part that is interesting is:
Example 1

set abc to {1, 2, 3, 4}
tell item 2 of abc
	set B to a reference to it
	set B to B + 1
end tell
(B as string) & "  ———  " & abc as string

Returns: 3 ——— 1234

So a reference to “it” can be increment while “set it to it + 1” can not even be compiled.

In example 1 B references it.
When I add 1 to what B references I get 3 as the first part of the output shows.
In the second part of the output when I display the value of ABC, I can see item 2 of ABC, which B references, is 2.

When I run this:
Example 2

set abc to {1, 2, 3, 4}
tell abc
	set B to a reference to it
	set item 2 of B to (item 2 of B) + 1
end tell
abc

{1, 3, 3, 4} is returned showing item 2 of the referenced B changes from 2 to 3.

I expected the “a reference to” in example 1 to have to have changed the value in the ABC string and returned {1, 3, 3, 4}. But this didn’t happen. It’s like it referenced something other then item 2, something other then ABC string or both. My question is does “it” return a copy of the targeted object, instead of the actual referenced value in the targeted object?

Hey Mark,

I don’t think I’ve ever seen an instance where any AppleScript construct would let you set the value of it from within a Tell-Block (and I’ve tried plenty over many years).

I don’t completely understand the why of it, but Shane is probably right. Hamish probably knows for sure ( but I’m afraid to ask :).

This will work:

set abc to {1, 2, 3, 4}

set theRef to a reference to item 2 of abc

tell theRef
   set its contents to (its contents) + 1
end tell

abc


Take Care,
Chris