How do I make this (intended) multiple "save as" script work?

Hey all,

I work in a graphic related environment, and produce quite an amount of graphic files per week. For compatibility reasons, I save both Illustrator and Photoshop files in EPS format, but, to do a more efficient backup, resave them to .AI and (compressed) .EPS format every friday. This is, you can imagine quite a tedious task.
I’m a complete rookie when it comes to scripting, but I tried to write a script that would automate this task, saving me both time and patience, yet, it turns out, I messed up somewhere.
If it’s not much of a problem, I would like to post the code, and the error message I get, this is as far as I got, this is where I’m stuck, this is, where the frustration of an ignorant comes to new heights.

The second thing I wanted to know (if anyone can help me out), is how to make this script work, not only with the chosen folder, but with every subfolder within.

Anyways, here’s the error message:

  • Can’t make alias “Bigfoot Main:Users:giga:Desktop:ot 16932:Lf-vmol-95x90 x1.eps” into a «class alst». (that’s one file where it doesn’t work)
    Note: The odd thing is, it works just fine with some files. And I have no clue why.

Here’s the code, as far as I got:

on SaveFilesAsPhotoshopCS2(fileListA, sourceFolder)
	set destPath to sourceFolder as string
	repeat with aFile in fileListA
		tell application "Finder" to set fileName to name of aFile
		set newFilePath to destPath & fileName
		tell application "Adobe Photoshop CS2"
			open aFile
			save current document in file newFilePath as Photoshop EPS ¬
				with options {class:EPS save options, encoding:high quality JPEG, preview type:eight bit TIFF, transparent whites:false, vector data:false}
			close current document saving no
		end tell
	end repeat
end SaveFilesAsPhotoshopCS2

on SaveFilesAsIllustratorCS2(fileListB, sourceFolder)
	set destPath to sourceFolder as string
	repeat with aFile in fileListB
		tell application "Finder" to set fileName to name of aFile
		set newFilePath to destPath & fileName
		tell application "Adobe Illustrator"
			open aFile
			save current document in file newFilePath as Illustrator ¬
				with options {class:Illustrator save options, PDF compatible:false}
			close current document saving no
		end tell
	end repeat
end SaveFilesAsIllustratorCS2

--Call Handler
set sourceFolder to choose folder with prompt "Source Folder?"
tell application "Finder"
	set fileListA to (every file of sourceFolder whose file type is "EPSF" and creator type is "8BIM") as alias list
	set fileListB to (every file of sourceFolder whose file type is "EPSF" and creator type is "ART5") as alias list
end tell
SaveFilesAsPhotoshopCS2(fileListA, sourceFolder)
SaveFilesAsIllustratorCS2(fileListB, sourceFolder)
tell application "Finder"
	delete (every file of sourceFolder whose file type is "EPSF" and creator type is "ART5")
end tell

that’s it. First script. It’s ok, you can laugh. But please, help, help, help. I really would like to see this working.

Thanks a lot for your time and patience
Pat aka Klayman

PD: I hope i got it right this time. :confused:

Model: G4 460
Browser: Safari 312.6
Operating System: Mac OS X (10.3.9)

Nothing to laugh about there - you should be proud of yourself.

With respect to your error, «class alst» refers to an alias list. Sometimes as alias list doesn’t work. Try replacing that with as alias as list.

Addendum: if you don’t care about the subfolders themselves but only want the files backed up, then this gets them all:

tell application "Finder" to set fileNameList to name of files of entire contents of (choose folder)

If you want to recreate the structure of the chosen folder, there’s a bit more work to do.

To elaborate on that, there’s been an issue with the Finder’s as alias list ever since it was introduced some years ago: it errors if the reference to which it’s applied only returns one item. There’s usually no problem with more than one item, or with none. The (by now) traditional way to cover the possibilities is to use a try block.

tell application "Finder"
	try
		set fileListA to (every file of sourceFolder whose file type is "EPSF" and creator type is "8BIM") as alias list
	on error number -1700
		set fileListA to (first file of sourceFolder whose file type is "EPSF" and creator type is "8BIM") as alias as list
		-- Or: set fileListA to {(first file of sourceFolder whose file type is "EPSF" and creator type is "8BIM") as alias}
	end try
	try
		set fileListB to (every file of sourceFolder whose file type is "EPSF" and creator type is "ART5") as alias list
	on error number -1700
		set fileListB to (first file of sourceFolder whose file type is "EPSF" and creator type is "ART5") as alias as list
		-- Or: set fileListB to {(first file of sourceFolder whose file type is "EPSF" and creator type is "ART5") as alias}
	end try
end tell

I always change every to first in the on error section in the hope that looking for only one matching item will make it faster. But I don’t think it makes any difference really. :wink:

But alias lists may not actually be necessary for Pat’s purposes. It looks as though he simply wants to resave the files in situ in a different format. I don’t have either of his applications, so I don’t know if that’s possible. If it is, Adam’s entire contents idea would make handling subfolders very simple. Simply coerce the Finder reference for each file to string or Unicode text, which gives the required paths without having to fool around with names and parent folders.

I don’t know if the PhotoShop and Illustrator bits work here, but the rest of it’s sound, if I’ve correctly understood the goal.

on SaveFilesAsPhotoshopCS2(fileListA)
	repeat with aFile in fileListA
		-- Coerce each Finder reference to Unicode text for the path to the file.
		tell application "Finder" to set filePath to aFile as Unicode text
		tell application "Adobe Photoshop CS2"
			open file filePath
			save current document in file filePath as Photoshop EPS ¬
				with options {class:EPS save options, encoding:high quality JPEG, preview type:eight bit TIFF, transparent whites:false, vector data:false}
			close current document saving no
		end tell
	end repeat
end SaveFilesAsPhotoshopCS2

on SaveFilesAsIllustratorCS2(fileListB)
	repeat with aFile in fileListB
		-- Coerce each Finder reference to Unicode text for the path to the file.
		tell application "Finder" to set filePath to aFile as Unicode text
		tell application "Adobe Illustrator"
			open file filePath
			save current document in file filePath as Illustrator ¬
				with options {class:Illustrator save options, PDF compatible:false}
			close current document saving no
		end tell
	end repeat
end SaveFilesAsIllustratorCS2

--Call Handler
set sourceFolder to choose folder with prompt "Source Folder?"
tell application "Finder"
	-- Just get lists of Finder references.
	set fileListA to (every file of entire contents of sourceFolder whose file type is "EPSF" and creator type is "8BIM")
	set fileListB to (every file of entire contents of sourceFolder whose file type is "EPSF" and creator type is "ART5")
end tell
SaveFilesAsPhotoshopCS2(fileListA)
SaveFilesAsIllustratorCS2(fileListB)
tell application "Finder"
	delete (every file of entire contents of sourceFolder whose file type is "EPSF" and creator type is "ART5")
end tell

Thank you thank you thank you thank you thank you thank you thank you!

Thanks Adam for the alias reference, and thank you Nigel, your modifications worked perfectly, saving the whole alias part. Brilliant. It does exactly what I wanted it to do, you guys rock! Thanks again.

Here’s one little addendum (I might be pushing it here, but it’s worth the try since you guys are on an entirely different level). I’ve been trying to add a little fail save to the Delete part, basically, verifying first, that the corresponding .ai file has been created. It hasn’t failed so far, but I think it would be much safer if it did the comparison first. Any suggestions? Thanks for all the help. :slight_smile:

Cheers
Pat aka Klayman

I don’t have your graphics programs either, so can’t test for you, but you do have a list of all the original files in fileListB, and I assume that when AI saves them in a new format and appends the .ai to it that the base name (without the extension) remains the same. The code below (by jj) is one way to extract it:

set jobNum to "123 456.pdf" as Unicode text
getBaseName(jobNum)

to getBaseName(fullName)
	set k to reverse of fullName's characters
	set kCount to count k
	--> find first dot in reverse
	repeat with i from 1 to kCount
		if k's item i is "." then exit repeat
	end repeat
	if kCount is i and k's item -1 is "." then --> ie, fullname was ".txt" (no base name)
		return "" as Unicode text
	else if kCount is i then --> ie, t was "blah" (no dot)
		return fullName --> no dot
	else
		return text 1 thru (-i - 1) of fullName
	end if
end getBaseName

and then you can compare base names.

Alternatively: In your AI handler you might make these changes (Not compiled because I don’t have AI):

on SaveFilesAsIllustratorCS2(fileListB)
   repeat with aFile in fileListB
       -- Coerce each Finder reference to Unicode text for the path to the file.
try
       tell application "Finder" to set filePath to aFile as Unicode text
       tell application "Adobe Illustrator"
           open file filePath
           save current document in file filePath as Illustrator ¬
               with options {class:Illustrator save options, PDF compatible:false}
           close current document saving no
       end tell
-- if this much doesn't error then you can delete the original right here instead of later
on error theError
display dialog theError
-- and what ever else you want to do, e.g. save the file name to a list so you can see what happened.
end try
   end repeat
end SaveFilesAsIllustratorCS2