Photoshop CS3 Save for Web

I’m very new to AppleScript, and I’m stumped on the following code. I’m having a problem either with setting the folder path and file name, or exporting as save for web…or both. Can anyone tell me what is wrong with my syntax? I’ve simplified the script to just the essentials. Thanks in advance!

tell application "Finder"
	set mainFolder to choose folder with prompt "Select target folder:"
	set testFolder to make new folder at mainFolder with properties {name:"Test Folder"}
end tell

tell application "Adobe Photoshop CS3"
	activate
	set thisDoc to current document
	tell thisDoc
		flatten
		set filePath to (testFolder as string) & "123.jpg"
		export in filePath as save for web with options {class:save for web export options, web format:JPEG, quality:80}
	end tell
end tell

Model: Intel Mac Pro
AppleScript: 2.0.1
Browser: Safari 530.18
Operating System: Mac OS X (10.5)

The finder has a unique way of setting a file or folder path by default, for example:

folder “Test Folder” of folder “Documents” of folder “myusername” of folder “Users” of startup disk of application “Finder”

That’s the result of your Finder tell block, which cannot be combined with a file name as:

“Macintosh HD:Users:myusername:Documents:Test Folder:” & “new file name”

Trick is to coerce the Finder path into an alias with “as alias” when setting the variable (be sure to set ‘as string’ when combining two strings, too; to avoid getting a list of two items):

tell application "Finder"
	set mainFolder to choose folder with prompt "Select target folder:"
	set testFolder to (make new folder at mainFolder with properties {name:"Test Folder"}) as alias
end tell

tell application "Adobe Photoshop CS3"
	activate
	set thisDoc to current document
	tell thisDoc
		flatten
		set filePath to ((testFolder as string) & "123.jpg") as string
		export in filePath as save for web with options {class:save for web export options, web format:JPEG, quality:80}
	end tell
end tell

The above line is fine. The problem is with the export line. I use CS2 and this works for me…

tell application "Finder"
	set mainFolder to choose folder with prompt "Select target folder:"
	set testFolder to make new folder at mainFolder with properties {name:"Test Folder"}
end tell


tell application "Adobe Photoshop CS2"
	activate
	set thisDoc to current document
	tell thisDoc
		flatten
		set filePath to (testFolder as string) & "123.jpg"
		export in file filePath as save for web with options {file type:JPEG, quality:80}
	end tell
end tell

Thanks!

The trick seemed to be adding “as alias” when creating the Test Folder. Both the export line and the “set filePath” line were fine as I originally had them. This is what finally worked for me:

tell application "Finder"
	set mainFolder to choose folder with prompt "Select target folder:"
	set testFolder to (make new folder at mainFolder with properties {name:"Test Folder"}) as alias
end tell

tell application "Adobe Photoshop CS3"
	activate
	set thisDoc to current document
	tell thisDoc
		flatten
		set filePath to (testFolder as string) & "123.jpg"
		export in filePath as save for web with options {class:save for web export options, web format:JPEG, quality:80}
	end tell
end tell

I’m glad you got it working… but your solution doesn’t make sense to me. The only time you use the variable “testFolder” after you initially create it with the Finder is in this line…
set filePath to (testFolder as string) & “123.jpg”

The result of that line is a string no matter if you use (as alias) or not when you create the folder… so what has changed by using it? That’s strange.

Here’s where I thought your problem was. If you look in the photoshop applescript dictionary the export command requires a “file specification”. Since your file path is a string you would need to use the following in order to be using a file specification…
export in file filePath

Notice the word “file” before “filePath” there which turns the file path string into a file specification. That’s where I thought your problem arose. So I can’t explain why the (as alias) worked but to me your real problem is with a string versus a file specification.

Anyway, I know it’s working for you but if you could test my theory (stop using “as alias” and try using the word “file” before filePath) and let me know if that fix works too it would make more sense to me.

Not within a Finder tell block. For example:

tell application "Finder"
	set aVariable to (make new folder at desktop with properties {name:"Test Folder"})
	--result: folder "Test Folder" of folder "Desktop" of folder "username" of folder "Users" of startup disk of application "Finder"
	
	set aVariable to (make new folder at desktop with properties {name:"Test Folder"}) as string
	--result: "Macintosh_HD:Users:username:Desktop:Test Folder:"
	
	set aVariable to (make new folder at desktop with properties {name:"Test Folder"}) as alias
	--result: alias "Macintosh_HD:Users:username:Desktop:Test Folder:"
end tell

I think you’re right; if a Finder folder specification is later used as a string it sometimes coerces into an alias path, just not always depending on which application is coercing the Finder’s unique folder specification - in this case, Photoshop, which probably cannot coerce ‘folder “Test Folder” of folder “Desktop”.’ into “.:desktop:Test Folder:” to be combined as a string with “newfilename”.

You’re also right in that I forgot to specify the string as ‘file “string:path:combined:”’ in the Photoshop tell block - I hadn’t tested it, just noticed a bump in the file spec/alias/string problem when the Finder set the folder path.

regulus, I noticed no difference in the result of the script when inserting “file” into the export line. It was the “as alias” that got the script to work. Now maybe there’s a more elegant way to write the script, but I’m not sure of all the terminology yet - this is literally the first day I’ve ever played with AppleScript.

Thanks again for all your help, and if anyone has any other suggestions to make the script better, I’d love to hear them!

Hank is right, a Finder file specifier will always be coerced to a string path using as text or as string
regardless of any wrapping application tell block


tell application "Finder"
	set a to desktop
end tell

tell application "Adobe Photoshop CS3"
	a as string
end tell
--> MyStartupDisk:Users:myUser:Desktop:"

the second coercion is also useless. For a math operation or a string concatenation the class of the result is always the class of the leftmost operand

Very, very, VERY cool. I hadn’t noticed since coming from AppleScript 1.3.7 that it did all that. Used to be that nothing else could interpret a Finder file/folder specifier except the Finder, and I don’t know when I locked-in my paranoia about getting a list when trying to concatenate two or more strings (unless I just used to be real sloppy with my variable classes prior to learning programatic approaches). I’ve been held back as “just a typesetter” at work so I never bothered to clean up old workflow solutions written originally on OS 7.1 and hacked into functionality for OS 8.6.

Thanks for pointing out my mistake, StefanK - saves me the trouble of fixing old scripts and justifies a total re-write from scratch that’s arguably beyond a typesetter’s job duty (HR thinks titles aren’t important unless I call myself something I’m not on a loan or job application, like supervisor or even graphic arts system support technician).

I’d like to finally make clear that string concatenation works only with operands which are or could be coerced to string.
A string and an alias results a list, then your global as string coercion is necessary