What's wrong with this repeat statement

I have been using this repeat statement without problems. This time I having an error which I cannot explain.

set ExternalDisk to {"NIKON D80", "PZV", "NO NAME"}
repeat with x in ExternalDisk
	set VolName to (get name of ExternalDisk)
	display dialog VolName
end repeat

Regards!
Daniel

You want (get name of x).

Thanks Regulus6633,

Improving, unfortunately I still have an error where it says "cannot obtain name of “NIKON D80”.

set ExternalDisk to {"NIKON D80", "PZV", "NO NAME"}
repeat with x in ExternalDisk
	set VolName to (get name of x)
	display dialog VolName
end repeat

Try this:

set ExternalDisk to {"NIKON D80", "PZV", "NO NAME"}
repeat with x in ExternalDisk
   display dialog x
end repeat

x is just a variable containing a string since your list only contains strings. It is not a record, so it does not have the property ‘name’.

A list of literal strings doesn’t have a name property, What do you want to accomplish?

In the set ExternalDisk to {“NIKON D80”, “PZV”, “NO NAME”} statement there are three items.

I want to get the name of each items individually
For x = 1 I am expecting to have VolName equal to NIKON D80
for x = 2 I am expecting to have VolName equal to PZV…

Thanks again!
Daniel

Then use the script (and explanation) in post #4

Sorry Stefan,

I’ve tried to get to post#4 in different ways and do not know how. I also tried with http://macscripter.net/viewtopic.php?id=4

Thanks!
Daniel

And also note the difference between the above and the following:

set ExternalDisk to {"NIKON D80", "PZV", "NO NAME"}
repeat with i from 1 to 3 
   display dialog (ExternalDisk's item i)
end repeat

Same thing.

Instead of name I think you mean content. In the Applescript language guide you have the following example

repeat with i in {1, 2, 3, 4} 
    set listItem to contents of i 
end repeat 
--result: 4 

it’s not exactly like ljr_nbg said. A repeat in is not exactly the same as repeat from to. The repeat variable is not (like many says) the value of that item from the list but it’s a reference to that item. In most cases it goes well without using content but in some cases it doesn’t. For example if we want to change a item in our current loop.

set theList to {1, 2, 3}
repeat with x in theList
	if x is 2 then
		set x to 5
		exit repeat
	end if
end repeat
return x
--result: item 3 of {1, 2, 3}

We can see from the result that the if statment didn’t work but if we’re using content of reference x then our if statement works

set theList to {1, 2, 3}
repeat with x in theList
	if contents of x is 2 then
		set x to 5
		exit repeat
	end if
end repeat
return theList
--result: {1, 2, 3}

But still our list isn’t affected and still have it’s initial values. When we’re setting the content of reference x instead of setting x itself then we’ll change the item in the list

set theList to {1, 2, 3}
repeat with x in theList
	if contents of x is 2 then
		set contents of x to 5
		exit repeat
	end if
end repeat
return theList
--result: {1, 5, 3}

DJ Bazzie Wazzie, thanks for clarifying this. Since I almost never use the ‘repeat with…in…’, I didn’t think about that.

Thank you everybody for your help!

Here is the correct syntax :

 set ExternalDisk to {"NIKON D80", "PZV", "NO NAME"}
repeat with x in ExternalDisk
	set VolName to contents of x
	display dialog VolName
end repeat

To complete the rules, AppleScript dereferences the list item silently in all cases
except boolean comparisons and writing into the list

this works:


set ExternalDisk to {"NIKON D80", "PZV", "NO NAME"}
repeat with x in ExternalDisk
	set VolName to x
	display dialog VolName
end repeat

this doesn’t work:


set ExternalDisk to {"NIKON D80", "PZV", "NO NAME"}
repeat with x in ExternalDisk
	if x is "PZV" then
		display dialog x --> will never display anything
	end if
end repeat

this doesn’t work either


set ExternalDisk to {"NIKON D80", "PZV", "NO NAME"}
repeat with x in ExternalDisk
	if contents of x is "PZV" then
		set x to "NewDisk" --> this line is really reached !
		display dialog (item 2 of ExternalDisk) --> oops: "PZV"
	end if
end repeat


AppleScript dereferences silently in all cases except direct equality tests and setting variables, list positions, etc. to that value. :slight_smile:

“Boolean comparisons” are equality tests and therefore don’t get a silent dereference:


set bools to {false, true, false}
repeat with x in bools
	if (x is true) then display dialog "Yes! It's true!" -- Never executed.
end repeat

But testing referenced booleans as conditions does work:


set bools to {false, true, false}
repeat with x in bools
	if (x) then display dialog "Yes! It's true!"
end repeat

Setting something else to x in the above repeats sets that something to x’s reference value.

Setting x to something else obviously gives a new value to the variable x and does nothing at all to the reference except lose it.

@stefank: Applescript doesn’t always dereference but only if you want to or coerce values. In your first example you say it willl be dereferenced but it fact it doesn’t. Display dialog will show the content of the reference and volName is just a reference like X self. In my example I’ll show you:

set theList to {1, 0, 1}
repeat with x in theList
	set y to x as boolean --y: true
	set z to contents of x --z: 1
	set xref to x --xRef: item 1 of theList and not it's value
	set contents of x to 0 -- change contents of x to proof that xref is a reference just like x
	exit repeat
end repeat
return {y, z, xref}
--result: {true, 1, item 1 of {0, 0, 1}}