Replacing an item in a list - what am I doing wrong?

I think I must be missing something obvious. I start with a list of unix filenames returned by a shell script that reads a text file, for example:

/Users/myname/ThisFileExists.file
/Users/myname/ThisFileDoesNotExist.file

The first item in the list is a filename that actually exists; the second filename is listed in the original text file, but the actual file does not exist.

The variable that contains the list is named currentDisks (these are disk image files). I try to change the list by running this script:


	set testDisks to every paragraph of currentDisks
	display dialog "Here is the original list:" & return & testDisks
	repeat with theItem in testDisks
		-- display dialog theItem
		try
			set theAlias to POSIX file (theItem) as alias
		on error
			set theItem to (theItem) & space & "(File or path not found!)"
			display dialog "Here is the rewritten item: " & theItem
		end try
	end repeat
	set TID to text item delimiters
	set text item delimiters to return
	display dialog "Here is the revised list:" & return & testDisks as string
	set text item delimiters to TID

The first display dialog shows that theItem has been rewritten to include the string “(File or path not found!)”, but the list at the end is exactly the same as the list at the beginning.

What obvious thing am I getting wrong?

You aren’t doing anything wrong per say, you just need to dereference theItem in your repeat loop. Try this

set testDisks to every paragraph of currentDisks
display dialog "Here is the original list:" & return & testDisks
repeat with theItem in testDisks
	-- display dialog theItem
	try
		set theAlias to POSIX file (theItem) as alias
	on error
		set contents of theItem to (theItem) & space & "(File or path not found!)"
		display dialog "Here is the rewritten item: " & theItem
	end try
end repeat
set TID to text item delimiters
set text item delimiters to return
display dialog "Here is the revised list:" & return & testDisks as string
set text item delimiters to TID

Hi,

This is a common misunderstanding.
The index variable theItem is not the list item itself, it’s a reference to the item
e.g {item 1 of testDisks, item 2 of testDisks}.
To change the list item use contents of


.
 set contents of theItem to theItem & space & "File or path not found!"
.

PS. the parentheses are not necessary

Bwaha, the day is mine Stefan! :smiley:

You deserved it :slight_smile:

Perfect! Thank you - and thank you for helping me get an education in the subtleties of AppleScript!