File naming problems

I am trying to write a script to do some tasks in Photoshop. I have cobbled together a few scripts and I am fairly happy with the script (as it does work!). I pinched the first bit from a script that was included as an example batch script from Adobe however I do not want it to select a folder and it’s contents just a specific file. This is where I am having the problems! Whatever I try I either get an error or the file is renamed wrong! What I need to do is select a Photoshop file run the script and save it under an amended name. To clarify:

Select this file:
“Any name-WHITE.tif”
script then changes background colour and saves as:
“Any name-c5m6y2k5.tif”
then changes image size and saves new file as:
“Any nameSml-c5m6y2k5.tif”

The files will always have “WHITE” at the end of the name so I need to remove it and replace it with the “c5m6y2k5” which is the variable that is asked in the dialogs. The files will always be saved in the same folder the original file came from and I ideally need to make sure the files aren’t too long. Is it possible for the script to give a warning if the file name is too long?

set tempFolderName to "Temp"
set inputFolder to choose folder

tell application "Finder"
	set filesList to files in inputFolder
	if (not (exists folder ((inputFolder as string) & tempFolderName))) then
		set outputFolder to make new folder at inputFolder with properties {name:tempFolderName}
	else
		set outputFolder to folder ((inputFolder as string) & tempFolderName)
	end if
end tell

tell application "Adobe Photoshop 7.0"
	set display dialogs to never
	close every document saving no
end tell

repeat with aFile in filesList
	
	set fileIndex to 0
	
	tell application "Finder"
		-- The step below is important because the 'aFile' reference as returned by
		-- Finder associates the file with Finder and not Photoshop. By converting
		-- the reference below 'as alias', the reference used by 'open' will be
		-- correctly handled by Photoshop rather than Finder.
		set theFile to aFile as alias
		set theFileName to name of theFile
		
		set cyanValue to text returned of (display dialog "Cyan Value?" default answer "" buttons {"Cancel", "OK"} default button 2 with icon 1)
		set magentaValue to text returned of (display dialog "Magenta Value?" default answer "" buttons {"Cancel", "OK"} default button 2 with icon 1)
		set yellowValue to text returned of (display dialog "Yellow Value?" default answer "" buttons {"Cancel", "OK"} default button 2 with icon 1)
		set blackValue to text returned of (display dialog "Black Value?" default answer "" buttons {"Cancel", "OK"} default button 2 with icon 1)
				
	end tell
	
	tell application "Adobe Photoshop 7.0"
		
		open theFile
		
		set docRef to the current document
		set myTiffOptions to {class:TIFF save options, image compression:LZW, byte order:Mac OS, save alpha channels:true, save spot colors:true, embed color profile:true, save layers:false}
		
		if (mode of docRef is not CMYK) then
		change mode docRef to CMYK
		end if
		if (bits per channel of docRef is sixteen) then
			set bits per channel of docRef to eight
		end if
		
		make new art layer in docRef with properties {blend mode:multiply}
		do action "Path to selection" from "Default Actions.atn"
		
		fill selection of docRef with contents {class:CMYK color, cyan:(cyanValue & ".0"), magenta:(magentaValue & ".0"), yellow:(yellowValue & ".0"), black:(blackValue & ".0")}
		
		set docName to name of docRef
		set docBaseName to getBaseName(docName) of me
		set newFileName to (outputFolder as string) & docBaseName & "-" & {"c" & cyanValue, "m" & magentaValue, "y" & yellowValue, "k" & blackValue}
		save docRef in file newFileName as TIFF with options myTiffOptions appending lowercase extension with copying
		
		--resize image and save as small version
		resize image current document width 10
		
		set docName to name of docRef
		set docBaseName to getBaseName(docName) of me
		set newFileName to (outputFolder as string) & docBaseName & "S-" & {"c" & cyanValue, "m" & magentaValue, "y" & yellowValue, "k" & blackValue}
		save docRef in file newFileName as TIFF with options myTiffOptions appending lowercase extension with copying
		close docRef without saving
		
	end tell
end repeat

-- Returns the document name without extension (if present)
--I'm not sure what this bit does and if I need it?
on getBaseName(fName)
	set baseName to fName
	repeat with idx from 1 to (length of fName)
		if (item idx of fName = ".") then
			set baseName to (items 1 thru (idx - 1) of fName) as string
			exit repeat
		end if
	end repeat
	return baseName
end getBaseName

I hope this is clear enough to understand and any comments or suggestions about the script will help me to try and understand the whole AppleScript thing more than I do now, which isn’t much!

Many thanks for any help.

Don’t know if i’ll be much help, i’m probably even newer to applescript than you are, but maybe i can help clarify your problem so other more knowledgeable people can chime in.

It might have to do with the basename that you are trying to extract. check out this string where i was asking for the same sort of help :
http://bbs.applescript.net/viewtopic.php?t=5221

i ended up using the first suggestion in that string. i tried a couple different subroutine things (even the one you used, i think) and they always seemed buggy to me. even though the first suggestion may cause more overall work by checking filenames outside of the scripts memory, it is still just one line of code, fewer things to go wrong.

have you put in any simple debugging/tracking statements (i forget what an actual programmer would call them)? something like this perhaps:


      set docName to name of docRef 
      set docBaseName to getBaseName(docName) of me 
display dialog "The base name i got was "& docBaseName -- checks the return from the subroutine
      set newFileName to (outputFolder as string) & docBaseName & "-" & {"c" & cyanValue, "m" & magentaValue, "y" & yellowValue, "k" & blackValue} 
display dialog "This is the name I'll attempt to save under - "& newFileName -- checks new file and path
      save docRef in file newFileName as TIFF with options myTiffOptions appending lowercase extension with copying 


simple breaks like that might let you and then the rest of the forum know where your errors are appearing. (sorry if you have already done this and laugh at my plebian suggestion, like i said, i’m fairly new to applescript, too and this is an “old reliable” basic debugging solution i always use.

also, i don’t think you need to call the getDocBaseName() subroutine twice, you should have the same docBaseName for each file format.

hope any of this helps,

david[/url]

Thanks a lot for your interest David, don’t worry I am quite literally inept at AppleScript and don’t really know what I’m doing but I do want to try and learn! I am probably missing too much knowledge to try and do the scripts I want, I think I need to sort all the basics out first!! Did you decide what book to buy, any recommendations?

Thanks for the amendment to the script. I can see how this can help. But the main problem I am having is trying to choose a file rather than a folder. When I try to change the script it all goes wrong! I just need to choose the one file and do the Photoshop stuff on that and then have it save in the same folder the original came from.

Any more help from anyone would be great.

Cheers
Tim