rename elements of a list

OK, i have a list from file, hopefully, which will be used to affix a .jpg at the end when it wll works.

The idea is that i have 1000 odd lines in a txt file that need to have .jpg fixed at the end.

So far my code is;


--this should get a list from a file, rename the list then save to anotger file.

set oldFile to "Shared G4 HD:Users:karrenmay:Desktop:libraryno.txt" as alias
set newFile to "Shared G4 HD:Users:karrenmay:Desktop:newLibrary.txt" as alias

set listFromFile to read oldFile using delimiter return
repeat with i from 1 to (count listFromFile)
	set theItem to (item i of listFromFile) as string
	my renameList(theItem)
end repeat


on renameList(theItem)
	
	try
		open for access newFile with write permission
		write (theItem & ".jpg" & return) to newFile starting at eof
		close access newFile
	on error
		try
			close access newFile
		end try
	end try
	
end renameList


but doesn’t want to work.

I have displayed dialogs all over and i cant seem to narrow it down, it reads the file, gets the text in the line, it also does go through the method. In the try catch it always comes to error.

As i display dialog “some error” after the first on error and it will always come up with that dialog box.

Any ideas??

The main problem is that you have defined the variable newFile outside of the handler that writes to the new file and so you get the error “The variable newFile is not defined.” Changing the variable from a local variable to a property will solve this. If I understand the problem (from another thread!), though, some of the lines have the “.jpg” extension and some don’t. Even changing your script to change the scope of newFile may not result in the desired outcome. The script below should work for your needs:

Jon

cheers yeah you have understood the problem, but what i am doing is just cutting out the lines that need to be changed and then replacing them with the new ones. Wont be used on all the lines.

Cheers

For a snappier performance, edit the complete text using text item delimiters and save the edited version in one go. This is faster than a repeat loop and several disk writes:

set oldFile to "Shared G4 HD:Users:karrenmay:Desktop:libraryno.txt" as alias
set newFile to "Shared G4 HD:Users:karrenmay:Desktop:newLibrary.txt" as alias

set textFromFile to read oldFile

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".JPG"
set textItems to textFromFile's text items
set AppleScript's text item delimiters to ".jpg"
set textItems to (textItems as string)'s text items
set AppleScript's text item delimiters to ""
set textToFile to textItems as string
set textItems to textToFile's paragraphs
if textItems does not end with "" then set the end of textItems to ""
set AppleScript's text item delimiters to ".jpg" & return
set textToFile to text 1 thru -2 of (textItems as string)
set AppleScript's text item delimiters to astid

try
  set fileRef to open for access newFile with write permission
  write textToFile to fileRef starting at eof -- omit 'starting at eof' to overwrite the file rather than append to it
end try
try
  close access fileRef
end try