Oops, my first go wasn't so good...

I just tried my first script. It didn’t go well. Basically I need to rename a bunch of files with a .mpg extension. I found some bits of code online and was trying to put it all together. It did read the file list, and it renamed files…the wrong ones! It renamed all of the files on my desktop with the name that should have been give to the file in the folder. Id does this regardless of the location of the selected folder. Is there any way someone could figure out what I did? Now back to figuring out what these files are on my desktop :frowning:

Here’s the script


on run
	tell application "Finder"
		set photoList to every file of the folder ¬
			(choose folder with prompt "Where are my files?")
	end tell
	renameFiles(photoList)
end run

on open theFolder
	tell application "Finder"
		set photoList to every file of (item 1 of theFolder)
	end tell
	renameFiles(photoList)
end open


to renameFiles(thePhotos)
	tell application "Finder"
		repeat with i from 1 to count of thePhotos
			set theName to name of item i of thePhotos
			set newNameWithExtension to theName & ".mpg"
			set name of item i to newNameWithExtension
		end repeat
	end tell
end renameFiles

Thanks!
Aaron

Your script is nearly there. You forgot to include of thePhotos in the line in which you rename the file. I dont know why it would rename all of the files on your Desktop. Making the minor change, the script worked fine for me. Below is the fixed script.

on run
	tell application "Finder"
		set photoList to every file of the folder ¬
			(choose folder with prompt "Where are my files?")
	end tell
	renameFiles(photoList)
end run

on open theFolder
	tell application "Finder"
		set photoList to every file of (item 1 of theFolder)
	end tell
	renameFiles(photoList)
end open


to renameFiles(thePhotos)
	tell application "Finder"
		repeat with i from 1 to count of thePhotos
			set theName to name of item i of thePhotos
			set newNameWithExtension to theName & ".mpg"
			set name of item i of thePhotos to newNameWithExtension
		end repeat
	end tell
end renameFiles

~Ross

Thanks for the help. I sat there with my jaw on the floor when I was what it was doing! It really stinks. Fortunately most files on the desktop are junk, but there were a few that weren’t. At least it wasn’t recursively doing it through all levels of the volume or something worse!

Thanks again!

  • Aaron

To explain it a little better, in the original script you said:


set name of item i to newNameWithExtension

So you set the name if “item 1” to the new file name… Since you don’t specify a specific disk or folder, the Finder starts from the desktop.

As you’ve seen, specifying 'name of item i of thePhotos" solves the problem.

However, there is one slightly better way to do it which resolves the issue completely, and that is:

to renameFiles(thePhotos) 
   tell application "Finder" 
      repeat with aPhoto in thePhotos 
         set theName to name of aPhoto 
         set newNameWithExtension to theName & ".mpg" 
         set name of aPhoto to newNameWithExtension 
      end repeat 
   end tell 
end renameFiles

In this example, you create a new variable called ‘aPhoto’ which is assigned to each item in thePhotos as you iterate through the list. So the first time through, ‘aPhoto’ equates to the first item in the list, the second time around it equates to the second item, and so on. It’s also clearer to read, and solves the problem of referring to different items (‘item i’ and ‘item i of thePhotos’).

It’s a good habit to get into.