Manipulating files after a rename

I’m having some trouble dealing with files after I’ve renamed them. If I’ve got a list of files and I rename them, I get an error if I try to do anything further with the list, presumably because the names have changed. For example, I have a list of 10 files. I rename them, and then do something like:

copy theFiles to folder "new" of parentFolder

Doing so gives a completely unreadable error about not being able to deal with “oldfilename.jpg”.

I assume this is a well-known issue and there’s some good way around it, but I can’t figure it out. Any suggestions?

In my particular case, the list of files comes from files dragged onto a droplet.

The script I’m writing is below. To test it, save it as an application, and then drag a few image files onto it. It seems to fail when it gets to the ImageReady part.

on open draggedFiles
	tell application "Finder"
		set draggedFiles to (sort draggedFiles by name)
		copy draggedFiles's length to draggedFileCount
		set parentFolder to container of item 1 of draggedFiles
		set smallFolderName to "small"
	end tell
	
	--Copy all files into a new subfolder
	set smallFiles to copyIntoSubfolder(draggedFiles, smallFolderName)
	
	--Rename original files so they are sequential and uniformly named
	set baseName to text returned of (display dialog "Please enter base filename:
(dialog will close after 15 seconds)" default answer "image" giving up after 15)
	nameNumberFiles(draggedFiles, draggedFileCount, baseName)
	
	--Rename files in small folder to match those in main folder
	nameNumberFiles(smallFiles, draggedFileCount, baseName)
	
	repeat with eachFile in draggedFiles
		tell application "Adobe ImageReady CS2"
			open eachFile
			do script "Create optimized fulls for Lighbox gallery copy"
			close eachFile
		end tell
	end repeat
	
	repeat with eachFile in smallFiles
		tell application "Adobe ImageReady CS2"
			open eachFile
			do script "Create optimized thumbs for Lighbox gallery copy"
			close eachFile
		end tell
	end repeat
	
end open

on run
	display dialog "Please drag files onto my icon."
end run

on nameNumberFiles(theFiles, theFileCount, baseName)
	repeat with i from 1 to (theFileCount)
		tell application "Finder"
			copy (a reference to item i of theFiles) to theFile
			set fileName to baseName & i & "." & (name extension of theFile)
			copy fileName to the name of theFile
		end tell
	end repeat
end nameNumberFiles

on copyIntoSubfolder(theFiles, folderName)
	tell application "Finder"
		set parentFolder to container of (item 1 of theFiles)
		if not (exists folder folderName in parentFolder) then
			make new folder in parentFolder with properties {name:folderName}
		end if
		set newFiles to duplicate theFiles to the folder folderName of parentFolder with replacing
		return newFiles
	end tell
end copyIntoSubfolder

mduser63,

Hi. I don’t have a lot of time to put into this, but this is what I discovered.

I changed your script from a drag-n-drop to a “choose files” type to test it with so the editor would be able to indicate where the problem is. The script is erroring when it gets to the line open eachFile:

repeat with eachFile in draggedFiles
	tell application "Adobe ImageReady CS2"
		open eachFile
		do script "Create optimized fulls for Lighbox gallery copy"
		close eachFile
	end tell
end repeat

It is erroring here because your subroutines rename the original file and then your references (the draggedFiles) no longer exist. I think that you should make your copies of the files and then rename the copies so the originals retain their names or you need to make a reference list of each file with its new name and then use that list to complete your operations in ImageReady. I think retaining the originals is easier than making a list in this case.

PreTech

Thanks for the reply. I realized that there is no reason I need to do the ImageReady actions on the files after renaming them, so I changed the script to use ImageReady and then rename the files. I’m getting a different error now. ImageReady activates, but instead of opening the first image, the image opens in Preview for some reason. Then ImageReady complains that it can’t do the action (presumably because it hasn’t actually opened any image). Any thoughts on why this is occuring? ImageReady’s dictionary only has two entries, one for “open” and one for “do script”. It is obviously getting the “do script” command, but it seems like it’s not getting the “open eachFile” command because Preview is opening them instead.

Here’s the script I’ve got now:

on open draggedFiles
	tell application "Finder"
		set draggedFiles to (sort draggedFiles by name)
		copy draggedFiles's length to draggedFileCount
		set parentFolder to container of item 1 of draggedFiles
		set smallFolderName to "small"
	end tell
	
	--Copy all files into a new subfolder
	set smallFiles to copyIntoSubfolder(draggedFiles, smallFolderName)
	
	
	repeat with eachFile in draggedFiles
		tell application "Adobe ImageReady CS2"
			activate
			open eachFile
			do script "Create optimized fulls for Lighbox gallery copy"
			close eachFile
		end tell
	end repeat
	
	repeat with eachFile in smallFiles
		tell application "Adobe ImageReady CS2"
			activate
			open eachFile
			do script "Create optimized thumbs for Lighbox gallery copy"
			close eachFile
		end tell
	end repeat
	
	
	--Rename original files so they are sequential and uniformly named
	set baseName to text returned of (display dialog "Please enter base filename:
(dialog will close after 15 seconds)" default answer "image" giving up after 15)
	nameNumberFiles(draggedFiles, draggedFileCount, baseName)
	
	--Rename files in small folder to match those in main folder
	nameNumberFiles(smallFiles, draggedFileCount, baseName)
	
end open

on run
	display dialog "Please drag files onto my icon."
end run

on nameNumberFiles(theFiles, theFileCount, baseName)
	repeat with i from 1 to (theFileCount)
		tell application "Finder"
			copy (a reference to item i of theFiles) to theFile
			set fileName to baseName & i & "." & (name extension of theFile)
			copy fileName to the name of theFile
		end tell
	end repeat
end nameNumberFiles

on copyIntoSubfolder(theFiles, folderName)
	tell application "Finder"
		set parentFolder to container of (item 1 of theFiles)
		if not (exists folder folderName in parentFolder) then
			make new folder in parentFolder with properties {name:folderName}
		end if
		set newFiles to duplicate theFiles to the folder folderName of parentFolder with replacing
		return newFiles
	end tell
end copyIntoSubfolder

Model: 12" Rev. C 1.33 GHz PowerBook
AppleScript: 1.10.6
Browser: Safari 416.12
Operating System: Mac OS X (10.4)