file rename script - stop me before I re-invent the wheel

Before I waste a lot of time writing a script please listen to the following psuedo code and let me know if you know of any existing scripts or have any code along these lines:


drop a folder of digital images (jpegs) onto a script
sort the files alphabeticly (or by date - I want the shots numbered in the order they were shot)
then rename, in order, as follows:

first image >>> "image1.jpg"
second image >>> "image2.jpg"

etc up to a limit of 999

I’m a Flash Actionscripter, not an Applescripter so I plan on doing what I do with PHP (whose language I don’t know either) - take an existing script and hammer away at it with help from what on-line documentation I can find. I have searched google and these boards and of course did find some starting points but would like something a little closer to my final idea. Any help in this matter would be appreciated.

(a finished script would probably be worth $20 dollars or so to me, but I don’t know if it’s appropriate to make that suggestion here)((this is for a client and would have a deadline of Wednesday 10-22-03))

Thanks

Jim

For this particular task, I would use this BETA CODE:

(*
Usage: drop a folder containing jpg files.
We will create a simple executable file containing the necessary
instructions to rename progressivelly the jpg files from
"picture0001.jpg" to a maximum of "picture9999.jpg".

Please, make some tests with dummy files before using this forever
*)

on open singleFolderWithJPGs
	set singleFolderWithJPGs to POSIX path of singleFolderWithJPGs
	set x to (do shell script "find " & quoted form of singleFolderWithJPGs & " -iname *.jpg -maxdepth 1 -type f")
	
	--> create executable file
	set fCount to 1
	repeat
		try
			do shell script "echo "mv " & quoted form of x's paragraph fCount & space & quoted form of (singleFolderWithJPGs & "/picture" & text 1 thru 4 of ("000" & fCount) & ".jpg") & "" | cat >> /tmp/renamer;"
		on error --> no more jpgs
			exit repeat
		end try
		set fCount to fCount + 1
	end repeat
	
	--> execute and delete executable file
	do shell script "chmod 755 /tmp/renamer; /tmp/renamer; rm /tmp/renamer"
end open
  • Compile and save as application

Skip the AppleScript and go for a tool like R-Name. More options than you can shake a stick at, and I have never had a problem with it. Open source and completely free, it allows find and replace within file name, changing file extensions, and sequantial numbering.

OK works, magically (ie I can’t figure it out :slight_smile: Thanks for the help

I was able to replace the word “picture” with “image” so I have 3 questions about this code.

1 - is it trivial to get rid of the leading zeros in the file name? (I tried:

do shell script "echo "mv " & quoted form of x's paragraph fCount & space & quoted form of (singleFolderWithJPGs & "/image" & fCount & ".jpg") & "" | cat >> /tmp/renamer;"

And the app broke.

2 - is there a way to sort by date?

3 - when run a second time on a folder where there are already named images and more are added does it actually delete the previous files? (it seems to if the folder was last viewed as column view)

Plaid Cow

Thanks for the Rname suggestion - great app, I see myself using this all the time, but my client needs something that cannot be screwed up - this is going to be run by many people with very little computer experience and I would prefer if they were not allowed to change anything. These files need to be named exactly the same way each time or my Flash application will not work.

2
If you are looking only in a single directory, use ls (and the sort by date option) and perhaps the -C1 option to make a single column of names.

1
It would be fairly trivial. Pipe the output from the ls through sed (or a perl command line) and set up a regular expression that found any string of 0s occuring before a number and replace them with nothing. (I don’t know the syntax for either to actually try it.)

3
It depends. If you will be runnign this script more than once, you need to rename all of the files that may match your replacement name, then change them all back.