Problem renaming files

I wrote a script to incorporate into a photobooth that I’m building. I have a DSLR camera tethered to my laptop, which dumps jpegs into a folder. When you open the script, you specify the folder where the files will be dumped.

After 4 pictures are taken the script renames them to 1.jpg, 2.jpg, 3.jpg and 4.jpg. The only reason for this, is that Photoshop actions require exact filenames. After renaming the files, Photoshop resizes them, puts them in a template, and prints a 4"x6", saves the picture as print.jpg then closes the file.

The files are renamed with the timestamped, and moved to an archive folder. Then the script loops back to the top.

The problem:
The 3rd file gets renamed to 3.jpg, and then to 4.jpg, leaving the 4th file with the original name. So I’m left with 1.jpg, 2.jpg 3.jpg which gets renamed to 4.jpg and the fourth file with its original name.

I would eventually like to write a loop to rename, but for now each rename command is written individually.

Any help?

(* define variables *)
tell application "Finder"
	set workPath to (choose folder with prompt "Where are the DSLR Camera Remote Photos?" default location POSIX file "/Users/lindsayedward/Pictures/")
	set archivePath to (choose folder with prompt "Where do you want to store the originals?" default location POSIX file "/Users/lindsayedward/Pictures/")
	set template to (choose file with prompt "Where is the Photo Template?" default location POSIX file "/Users/lindsayedward/Pictures/")
	set photoTemplateName to name of template
	set photoTemplate to template as string
end tell

(* Loop *)
repeat
	tell application "Finder"
		(* Waits for 4 pictures to be entered *)
		set fileCount to (count files in folder workPath) as integer
		repeat while fileCount < 4
			set fileCount to count files in folder workPath
			delay 1
		end repeat
		
		set current to current date
		set theYear to year of current
		set theMonth to month of current as integer
		if theMonth < 10 then set theMonth to "0" & theMonth
		set theDay to day of current
		if theDay < 10 then set theDay to "0" & theDay
		set SS to time of current
		set HH to SS div 3600
		set SS to SS mod 3600
		set MM to SS div 60
		set SS to SS mod 60
		if HH < 10 then set HH to "0" & HH
		if MM < 10 then set MM to "0" & MM
		if SS < 10 then set SS to "0" & SS
		set timeStamp to (theYear as text) & "-" & theMonth & "-" & theDay & "-" & HH & MM & SS

                (* rename files for Photoshop Action *)
		set name of file 1 of folder workPath to "1.jpg"
		display dialog "1"
		set name of file 2 of folder workPath to "2.jpg"
		display dialog "2"
		set name of file 3 of folder workPath to "3.jpg"
		display dialog "3"
		set name of file 4 of folder workPath to "4.jpg"
		display dialog "4" -- renames file 3 instead of file 4
	end tell
	
	(* Opens template, loads 4 files, prints, saves, closes *)
	tell application "Adobe Photoshop CS5"
		activate
		open alias photoTemplate
		do action "photobooth" from "LuLuEdwardPhotography"
		close every document saving no
	end tell
	
	(* renames and move originals *)
	tell application "Finder"
		set name of file "1.jpg" of folder workPath to timeStamp & "_1.jpg"
		set name of file "2.jpg" of folder workPath to timeStamp & "_2.jpg"
		set name of file "3.jpg" of folder workPath to timeStamp & "_3.jpg"
		set name of file "4.jpg" of folder workPath to timeStamp & "_4.jpg"
		set name of file "print.jpg" of folder workPath to timeStamp & "_print.jpg"
		set filesToMove to every file in workPath
		move filesToMove to archivePath
	end tell
end repeat

I suspect this is changing Finder’s sort order while the script is renaming files. Instead of the ‘display dialog’ you could ask Finder for a list of files, and see what it thinks the order is:

set myfiles to files of folder workPath

Using the file index is possibly not the best way to do this. You can get the actual names, why not use those as-is?:

tell application "Finder"
	(* Waits for 4 pictures to be entered *)
	set fileCount to (count files in folder workPath) as integer
	repeat while fileCount < 4
		set fileCount to count files in folder workPath
		delay 1
	end repeat
	set theFiles to files of folder workPath
	-- if needed, convert to form used by Photoshop Action
end tell

So I figured out that it was a problem with the original filenames. If I pass in something with a filename staring with 1 or 2, by the time it renames file 3, it’s order is:

1.jpg
2.jpg
2012-01-09-123456.jpg
3.jpg

My files will always be in the form of Remote000000XX.jpg, so this shouldn’t occur. However, for sake of tidiness, if I store the file names as myFiles (as was mentioned above), how do I iterate through that list/array/stack to rename the files.

I tried:

set file 1 of myFiles in folder workPath to "1.jpg"

but it didn’t like that myFiles wasn’t a string. If I make it a string, how to I parse the different filenames?

Sorry, this is my first attempt at AppleScript. :stuck_out_tongue:

I’m guessing you want to rename the files as , where is the order of arrival in the folder. That order is not Finder’s sort order. Finder sorts by name: you can sort on other properties in a Finder window, but that does not affect the order in the list you can get through AppleScript.

But it is possible that your camera delivers the files with names that happen to sort ordered by time.
I’d like to see an example, please.

And tell me when I guessed wrongly.

set file 1 of myFiles in folder workPath to "1.jpg"

Yes, myFiles is a list, so you want to get items from that, and do something to them, one by one:

tell application "Finder"
	repeat with aFile in myFiles
		-- some code to make a name goes here
		set name of aFile to "name you made"
	end repeat
end tell
Edit:

It just occurred to me that the creation date of the photo might be all you need.
You could use that as the new name; it has a unique time string, so no need to stick on an index number.
Also, the index you are using does not produce a sorted-by-name list. Finder’s dictionary has this to say about ‘index’:

I think you’ll have to use the creation date anyway. Here’s a bit of code to do that:

tell application "Finder"
	repeat with aFile in myFiles
		set creationDate to creation date of aFile
		set {year:y, month:m, day:d} to creationDate
		tell (y * 10000 + m * 100 + d) as text to set dateStamp to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
		set dateStamp to dateStamp & space & time string of creationDate
		set fileName to dateStamp & ".jpg"
		set name of aFile to fileName
	end repeat
end tell

Most of the date stuff is from Nigel Garvey. When you use this you can delete the section where you build ‘timeStamp’.

Actually, since it’s a photobooth, the photos will be grouped in groups of 4. So I want to name them with the same timestamp, but different indices. That way they group together in a nice, visual way. I have the renaming part working as well as I need it.

Also, the software that I’m using always names the files Remote000000XX where XX is the image number. So no worries about conflicts with renaming, as long as I’m using the images as they arrive.

The 4 images get put into a 2x2 photo grid that’s printed out on a 4x6. Again, order is insignificant.

You’ve been great with your help! I’ll let you know if I run into any more issues.