How do I add a script to an action in PS_CS2 to skip steps if need to

I have a Photoshop CS2 droplet that does the follows:
Image Open>Image Mode to RGB>Image Size> Set Selection to “Path 1” Inverse>Set Background Delete>Save, Convert to Index…>Close
This droplet does work. We use it to convert outlined CMYK EPS files to GIF files with a tranparent backgound to be used in Powerpoint. My problem I need to address is that when we run the droplet on a batch of images, an image that does not have a path in it will cause the action to stop with an error. We would like to have a droplet that when it comes across an image that does not have a path, to skip the deleting of the background steps and to just save it as a GIF file. Is this possible by adding a script within the action to accomplish this?

Thanks in advance!

Model: MP 2.0Ghz G5
Browser: Safari 417.8
Operating System: Mac OS X (10.4)

That’s fairly simply now that Photoshop is fully Applescriptable - prior to CS - the only applescript support was the do script command.

What you need to do is isolate the image BEFORE you do the action…that’s as simple as :

if path exists “Path 1” then
–do something

I can’t remeber the exact syntax for this - bu it’s something like that - I have a script that does this at work - I’ll be there tomorow - so if you still don’t have it by then - let me know

This is exactly where a scripting is better than actions, you can’t have the conditionals in your batch eg: if this exists do this if not do something else. Here is some code I have which uses the “Clipping Path” only as you can several paths and different names.


	
				if exists (path items whose kind is clipping) then
					set thepath to (name of every path item whose kind is clipping)
					set thepath to item 1 of thepath
					
					tell path item thepath
						create selection feather amount 0 with antialiasing
					end tell
					invert selection
					fill selection with contents {class:RGB color, red:255, green:255, blue:255}
					trim basing trim on top left pixel
					set x to (width as pixels) + 50
					set y to (height as pixels) + 50
					resize canvas width x height y
				end if
	
			delete (every path item whose kind is not clipping)

the way I did it was :

if exists path item "Path 1" then
--do something
else
--so something else
end if

Thanks for your responses. This is what I have so far. The problem is when this script runs, it skips over making a selection if there is a Path 1. We want it to make a selection of Path 1 if there is one, if not, move on to the second half of the script and make the selection as an overall selection. I’m a noob at this and would appreciate any help that comes my way.
Thanks in advance!

tell application "Adobe Photoshop CS2"
	set mydocument to current document
	if (exists path item "Path 1") then
		set mypath to path item "Path 1" of mydocument
		create selection of mypath
	end if
	if not (exists path item "Path 1") then
		
		set shapeRef to select current document region {{0, 0}, {0, 100}, {100, 100}, {100, 0}}
		
	end if
end tell

you were very close - first - you needed to “tell” the current document.

then I just added an else and changed the reference from "elect current document region " to “select region” because we’re in a document tell already - so you don’t want to re-Tell the current document.

tell application "Adobe Photoshop CS2"
	set mydocument to current document
	tell mydocument
		if (exists path item "Path 1") then
			set mypath to path item "Path 1" of mydocument
			create selection of mypath
		else
			set shapeRef to select region {{0, 0}, {0, 100}, {100, 100}, {100, 0}}
		end if
	end tell
end tell

ChrisAdmin,

Thanks!

We knew we were missing something small, but that’s all it takes is something like that. Now that it works, I noticed an error on my part. When the script runs on my image that has no path, the selection is very small. In the second part of the script, we want to select the entire image. Our script only selects by dimension. Is there any way to select the entire canvas of any different sized image?

Rocket Boy, your select reigon is set to 100x100 pixels at top left corner if you want to select all the pixels of various sized images you need to do something like:

tell application "Adobe Photoshop CS"
	set mydocument to current document
	set ruler units of settings to pixel units
	set docHeight to height of mydocument
	set docWidth to width of mydocument
	tell mydocument
		if (mode of mydocument is not RGB) then
			change mode mydocument to RGB
		end if
		if exists (path items whose name is "Path 1") then
			set thepath to (name of every path item whose name is "Path 1")
			set thepath to item 1 of thepath
			set properties of every layer of mydocument to {background layer:false}
			tell path item thepath
				create selection feather amount 0 with antialiasing
			end tell
			invert selection
			clear
		else
			set shapeRef to select region {{0, 0}, {docWidth, 0}, {docWidth, docHeight}, {0, docHeight}}
		end if
	end tell
end tell

See also this script which does more or less what you were after with the option to set an image size based on both the trimmed down image of a file that contains a “clipping path” or a file that doesn’t. The resize is also based on the longest dimention height or width. If you want it to work on “Path 1” just swap the 3 lines from “if exists” of the above script to the script below:

set tempFolderName to "Converted for Power Point"
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 CS"
	set display dialogs to never
	close every document saving no
end tell

repeat with aFile in filesList
	
	set fileIndex to 0
	
	tell application "Finder"
		set theFile to aFile as alias
		set theFileName to name of theFile
	end tell
	
	tell application "Adobe Photoshop CS"
		
		set ruler units of settings to pixel units
		
		open theFile
		
		set docRef to the current document
		
		tell the current document
			
			if (mode of docRef is not RGB) then
				change mode docRef to RGB
			end if
			if (bits per channel of docRef is sixteen) then
				set bits per channel of docRef to eight
			end if
			
			delete (every channel whose kind is not component channel)
			
			delete (every path item whose kind is not clipping)
			
			if (count of art layers) > 1 or (count of layer sets) > 0 or ((count of art layers) is 1 and not background layer of layer 1) then flatten
			
			if exists (path items whose kind is clipping) then
				set thepath to (name of every path item whose kind is clipping)
				set thepath to item 1 of thepath
				
				set properties of every layer of docRef to {background layer:false}
				
				tell path item thepath
					create selection feather amount 0 with antialiasing
				end tell
				invert selection
				set current layer to layer "layer 0" of docRef
				clear
				trim basing trim on top left pixel
				set docHeight to height of docRef
				set docWidth to width of docRef
				if (docHeight > docWidth) then resize image height 990 resolution 150
				if (docWidth > docHeight) then resize image width 990 resolution 150
				set X to (width as pixels) + 10
				set Y to (height as pixels) + 10
				resize canvas width X height Y
				delete every path item
			else
				set docHeight to height of docRef
				set docWidth to width of docRef
				if (docHeight > docWidth) then resize image height 1000 resolution 150
				if (docWidth > docHeight) then resize image width 1000 resolution 150
			end if
			
			set docName to name of docRef
			set docBaseName to getBaseName(docName) of me
			set newFileName to (outputFolder as string) & docBaseName
		end tell
		
		save docRef in file newFileName as CompuServe GIF with options {colors in palette:256, dither:none, forced colors:web, matte:none, palette:local selective, preserve exact colors:false, transparency:true} appending lowercase extension with copying
		close current document without saving
	end tell
end repeat

-- Returns the document name without extension (if present)
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

What does this line mean:

set properties of every layer of docRef to {background layer:false}

In a photoshop document the background layer is always locked to release this you would double click the layer in the layers palette to make it become a floating layer “layer 0” its contents can now be dragged and moved within the canvas. This is a bit of sloppy syntax on my part as there is actually only the 1 layer as all others have previously been flattened it should be more like this.

set properties of layer 1 of docRef to {background layer:false}

layer 1 is always the top layer in the layer stack, 1 is the index not the name

Thanks for the info Mark. Another question: Is it possible to take an applescript and record it or insert it into a photoshop action? So that the action has a script built into it. This way I can add and record other steps to the action. I know this can be done with javascripts.

Not to my knowledge. I have scripts that do this the other way round though and I think that the process to do what you want would be the same. As ChrisAdmin quoted in his second post have your script contain your conditional - if exists path item “Path 1” this is the part that a batch process can’t do. You would require a Java version of whats below and record your action processes so they can be played by the javascript. It is my understanding that java requires you to use photoshops batch process and this can be complicated code: With applescript you use finder to handle this part and its much easier.

tell application "Adobe Photoshop CS"
	tell document 1
		if exists path item "Path 1" then
			do action "Path to selection etc" from "Rockets Actions"
			if (docHeight > docWidth) then
				do action "Resize width" from "Rockets Actions"
			else
				do action "Resize height" from "Rockets Actions"
			end if
		end if
		if not (exists path item "Path 1") then
			do action "Select All etc" from "Rockets Actions"
			if (docHeight > docWidth) then
				do action "Resize width" from "Rockets Actions"
			else
				do action "Resize height" from "Rockets Actions"
			end if
		end if
	end tell
end tell