Script works... once. Gets Finder error with repeat

With the generous help of StefanK I was able to get my original script to work flawlessly. While his recommended shell script did the trick in getting it to work I ran into trouble when I tried to modify it for another application. I have a difficult enough time grasping basic Applescript so deciphering and tweaking a shell script was way beyond my scope and I went back to trying my hand at reworking it with the basic Applescript code I know.

Amazingly, I got it to work… kind of.

The script creates a folder at the root of the HD with a unique name generated by a filemaker database record field and a text string

It then monitors a watch folder for image files downloaded to it randomly from a tethered camera and processes those files with a Photoshop Action, prints and saves the file with it’s original name back to the watch folder.

The script then renames the processed file with the database prefix, a text string, and then what I assumed would be a unique date and time stamp suffix (I added a numbered sequence loop to the file name just to ensure the name would truly be unique) and then moves the renamed file from the watch folder to the folder generated at the beginning of the script.

Everything seems to work fine with the first file the script sees and processes but for some reason, which I can’t for the life of me figure out, the script seems to name the subsequent files with the exact same name as the first file so I get an error message that the file already exists in the new folder.

Could someone please tell me where I’ve gone wrong in my logic (now that’s a loaded question)?

Thanks again to all those members who help us newbies out so patiently.

Regards,

kevlar

--This gets receipt number from Filemaker Sales Receipt database customer record
tell application "FileMaker Pro"
	set myCell to cell "Receipt_Number" of current record
end tell
--This creates folder at root of HD with receipt number as folder name prefix and current date as suffix
tell application "Finder"
	set D to current date
	set myShortDate to short date string of D as string
	make new folder at "Macintosh HD" with properties {name:myCell & "_" & "ForesidePhotography.com" & "_" & myShortDate}
	set newFolderPath to result as Unicode text
end tell
--This loop checks to see if watch folder "Camera_Images" has had any images downloaded into it
repeat
	tell application "Finder" to set FileList to files of folder "Macintosh HD:Camera_Images:"
	set CountOfFileList to (count of FileList)
	if CountOfFileList > 0 then
		exit repeat
	end if
end repeat
--Files detected in watch folder are then processed with Photoshop action, printed and saved with the original file name back to watch folder
activate application "Adobe Photoshop CS4"
set item_no to 0
repeat with FileToOpen in FileList
	tell application "Adobe Photoshop CS4"
		open (FileToOpen as alias)
		do action "Photo Process" from "Photo Actions"
	end tell
	--GUI and Sysytem Events are used here to print because particular printer used requires a unique paper size
	tell application "System Events"
		tell process "Photoshop"
			keystroke "p" using {command down}
			key code 36
			delay 2
			key code 36
			delay 40
			keystroke "s" using {command down}
			delay 2
			key code 36
		end tell
	end tell
	tell application "Adobe Photoshop CS4" to close document 1
	--File is then renamed with Filemaker data prefix, a text string, and then a date and time stamp suffix
	tell application "Finder"
		set DTime to time string of D as string
		set myDateTime to (myShortDate & "_" & DTime)
		set the name of file (FileToOpen as alias) to (myCell & "_" & "Image_Processed" & "_" & myDateTime & "_" & (item_no + 1) & ".jpg")
		set theRenamedFile to result
		set CameraImagePath to "Macintosh HD:Camera_Images:"
		set theRenamedFilePath to CameraImagePath & theRenamedFile as Unicode text
	end tell
	--Renamed File is then moved to the unique customer folder created in beginning of script
	tell application "Finder"
		move file theRenamedFilePath to folder newFolderPath
	end tell
	--process repeats until watch folder is empty
end repeat
return (0.1 * minutes)

Model: MacBook Pro 2.66 OSX10.6.4
AppleScript: 2.1.2
Browser: Safari 533.16
Operating System: Mac OS X (10.6)

Well if it’s a naming problem then we need to look at the line in your script that defines the name. That seems to be this line…

set the name of file (FileToOpen as alias) to (myCell & "_" & "Image_Processed" & "_" & myDateTime & "_" & (item_no + 1) & ".jpg")

So from what you’re saying that line always produces the same name. So which variables in that line do you think should vary with each loop? Let’s look at the variables from that:

  1. myCell - that won’t change because it’s defined at the top of the script and doesn’t vary
  2. myDateTime - that comes from “myShortDate & “_” & DTime” and they won’t change either.
  3. item_no + 1 - that won’t change because that always equals 1. item_no is always 0 and you add 1 to it.

What I think you want is for item_no to change with each loop. If so then you need to do this…

set item_no to item_no + 1
set the name of file (FileToOpen as alias) to (myCell & "_" & "Image_Processed" & "_" & myDateTime & "_" & (item_no as text) & ".jpg")

Thank you so much for your help. It worked perfectly when I incorporated your suggestion. I’m not sure why the time stamp wasn’t sufficient as a file name differentiator since each process loop should be offset from the previous one by at least a second so the name should be different without the need for a numerical sequence. Thank you again for explaining so clearly where I went wrong, it really helps me to better understand scripting.

Regards,

kevlar

Browser: Firefox 3.6.8
Operating System: Mac OS X (10.6)

Your welcome. FYI, your date doesn’t change because you get the date (D) in this part which is outside the repeat loop.

tell application "Finder"
   set D to current date
end tell

So if you wanted the date to change it needs to be inside the repeat loop. Note that you do not need the Finder to get the date. So put this in your repeat loop and your date will change.

set D to current date
set myDateTime to short date string of D & "_" & time string of D