Photoshop: save to folder, if exists save elsewhere.. possible?

Hello all…

This script works as is, but would like it to do more… it’s simply a script to open Photoshop files in Photoshop CS2, do some stuff and save to a prexisting folder. I’ve reduced as much as possible to the point that it still works. The final saves out 4 versions of the file to different folders, but I should be able to take the single answer and repeat it for the other saves that I left out.

What I want… when I save to the ‘alphabetical’ subfolder, if that file already exists, it overwrites the file. I can’t have that! I’d like it to make a folder named “Duplicates” in the parent folder and save in there; I can even have the folder “Duplicates” there ahead of time because the folders are stationary.

Also, is it odd the way I have the files saving to the appropriate subfolders? the parent folder has 36 subfolders (0-9 and A-Z) and the file is suppose to save to the corresponding subfolder determined by the first letter in the name of the file.

set inputFolder to choose folder

tell application "Finder"
	activate
	set filesList to files in inputFolder
end tell

tell application "Adobe Photoshop CS2"
	set display dialogs to never
end tell

repeat with aFile in filesList
	
	tell application "Finder"
		set theFile to aFile as alias
		set theFileName to name of theFile
		set alphabetical to ""
		set alphabetical to the first character of theFileName
		set outputfolder4 to folder alphabetical of folder "BAMPF" of folder "Desktop" of folder "work" of folder "Users" of startup disk
	end tell
	
	tell application "Adobe Photoshop CS2"
		activate
		open theFile
		
		set docRef to the current document
		set docHeight to height of docRef
		set docWidth to width of docRef
		set myOptions2 to {class:JPEG save options, embed color profile:false, format options:standard, quality:6, scans:3}
		set docName to name of docRef
		set newFileName4 to (outputfolder4 as string) & docName
		
		-- Photoshop stuff			
		if (mode of docRef is not grayscale) then change mode docRef to grayscale
		
		save docRef in file newFileName4 as JPEG with options myOptions2 appending lowercase extension with copying
		close docRef without saving
	end tell
end repeat

I suppose one more question: I find it odd I go from Finder, to Photoshop, to Finder and again to Photoshop… think I can eliminate or move around some of my steps?

Thanks in advance for any help!!

Jacques, thanks for the time you spent on this… it didn’t quite work for me out of the box though. I did get it working so I’ll mention what I needed to change.

First it errored out here, with ‘docName’ highlited:

		set newFileName4 to tempFolder & docName as Unicode text

so I changed it to:

		set newFileName4 to (tempFolder as string) & docName as Unicode text

This worked; it saved out my files to the Temp folder but just left it there. I took your code:

		try
			tell application "Finder" to move (files in tempFolder whose name contains docName) to outputfolder4
		on error
			tell application "Finder" to move (files in tempFolder whose name contains docName) to folder "Duplicates" of folder FolderBAMPF
		end try

and simply deleted some of it to this and it worked just fine:

		try
			tell application "Finder" to move files in tempFolder to outputfolder4
		on error
			tell application "Finder" to move files in tempFolder to folder "Duplicates" of folder FolderBAMPF
		end try

Now it will move the files to the correct folders. If the file exists in the Duplicates folder, it will error out… but I don’t think that should ever happen.

So thanks very much Jacques; I hadn’t thought of having the Finder move the files after they’ve been changed, but it makes total sense.