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.
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
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!"
.