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
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…
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}
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
@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}}