Help Me Get the File Path, please.

set s to "12 34 56 78 EOP 12 34 56"
set l to {}
repeat with theWord in every word of s
	if theWord is "EOP" then
		exit repeat
	else
		set l to l & theWord
	end if
end repeat

l

Running in Script Editor gives me {“12”, “34”, “56”, “78”, “EOP”, “12”, “34”, “56”} instead of the expected {“12”, “34”, “56”, “78”}?

When you use a variable to iterate through a list, you have to be a bit more specific with the variable using the “contents” of it instead of the variable itself:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Thanks a lot Jon - this is a problem when people like me learn AppleScript a bit here and a bit there… but really, what’s the rationale behind using ‘contents’ to refer to the content of a variable? Isn’t that always the case when a variable is mentioned it’s always its content that’s of interest?

Any pointer so I can learn more? Thank you.

Hi all :slight_smile:

Here one small approach…
In your case, AppleScript store, in the variable, the reference of the object, but not the value of it.
theWord = word 2 of {“12”, “34”, “56”, “78”, “EOP”, “12”, “34”, “56”} --for exemple
If you want to know only the value of the object, you must use the “contents” form with it :
contents of theWord → “34”
:wink:

Thanks for the explanation - this makes much more sense now. I still have a question though: when do you know what you get is a reference? I look back at the code I wrote and found I used repeat loops in many places, and I haven’t used a single “contents” keyword before; e.g.,


set l to {1,2,3,4}
repeat with i in l
  display dialog i
end repeat

works without using “display dialog content of i” - and there’re other usages that seem to work without “content” too, like writing to a file, etc. So what is the criterion here?

Thanks.