Photoshop CS5 issues (scripts written for older versions)

I have had several issues with scripts I wrote for PS CS4 and earlier now not working in CS5. This is a big one. The “continue_loop” variable gets returned false in CS5, but not in CS4, thus I get the “Oops, something happened” message. I haven’t had enough time to dig into this and guess/check my way out of it, so I thought I’d see what you all think. The purpose of the script is to stack images dragged onto it into a layered file.

global layerDoc
global countStack

on run
	tell me to display dialog ¬
		"Drop image files onto this droplet to stack them into a layered file." with icon note giving up after 30
end run

on open these_items
	set item_count to the count of these_items
	if item_count is 1 then
		display dialog "There is no reason to stack only one file." buttons "OK" default button 1 with icon note giving up after 30
	else
		-- the loop
		repeat with i from 1 to item_count
			set this_item to (item i of these_items)
			set the item_info to info for this_item
			if (alias of the item_info is false) and (folder of the item_info is false) then
				display dialog this_item
				set continue_loop to my process_item(this_item, i)
			else if i is 1 then
				set continue_loop to false
			else
				set continue_loop to true
			end if
			-- error reporting
			if continue_loop is false then
				tell me to activate
				display dialog "Oops, something happened." buttons "OK" default button 1 with icon note giving up after 30
				return
			end if
		end repeat
		-- success
		tell application "Adobe Photoshop CS5.1"
			activate
			display dialog "File stacking completed (" & countStack & ¬
				")." buttons "OK" default button 1 with icon note giving up after 30
		end tell
	end if
	
end open

on process_item(theFile, theNumber)
	tell application "Adobe Photoshop CS5.1"
		if theNumber is 1 then
			activate
			-- make the first file the bottom layer
			try
				open theFile showing dialogs never
			on error errmsg
				display dialog errmsg
				return false -- do not continue the loop
			end try
			set layerDoc to the current document
			if (count layers of layerDoc) is not 1 then flatten layerDoc
			set the name of the background layer of layerDoc to my getNum(name of layerDoc)
			set countStack to 1
			
		else
			-- after the first file
			try
				open theFile showing dialogs never
			on error
				return true -- exit process but continue the loop
			end try
			set docRef to the current document
			if (count layers of docRef) is not 1 then flatten docRef
			set docN to my getNum(name of docRef)
			-- the new layer
			duplicate the background layer of docRef to layerDoc
			close docRef saving no
			set the name of layer 1 of layerDoc to docN
			set countStack to countStack + 1
		end if
		return true
	end tell
end process_item

on getNum(t)
	set trimmed_name to my trimExtension(t)
	set n to text ((length of trimmed_name) - 3) thru (length of trimmed_name) of trimmed_name
	if my isInteger(n) is false then set n to trimmed_name
	return n
end getNum

on isInteger(num)
	try
		set test to num as integer
	on error
		return false
	end try
	return true
end isInteger

on trimExtension(t)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "." -- separated at periods
	if (count t's text items) > 1 then ¬
		set t to t's text 1 thru text item -2 -- if more than two periods
	set t to t's text items -- splits t into a list again at the periods
	tell t to set t to beginning & ({""} & rest) -- preserve encoding
	set AppleScript's text item delimiters to d -- always set them back again!
	return t
end trimExtension

Pass CS5 a string to open and not an alias

When I pass “this_item” as a string to PS, I get:


…which means nothing to me.

However, I put a dialog inside the try/error when Photoshop is opening #1, and that error went. So, it is definitely a file opening issue.

It says:

Any more ideas?

Hi,

           
...
    open file (theFile as text) showing dialogs never 
...

if I remember it right :wink:

Thanks for helping!

No luck though…

Try changing this:

to:

           set this_item to (item i of these_items) as alias

Hi Netdewt

I just copied your code over and introduced Hans-Gerd Classen suggestion, saved the code as an application, dropped two
psd files on it, works sweet here

OSX 10.7.3 PS CS5.1

Frustrating! I’m on 10.5.

I get this message with all of the “as string” or “as text” suggestions so far.

I have to admit, I don’t really understand what’s going on in this repeat loop. The intention is to not process folders that get dropped on the script. I think it’s a little convoluted as is. Maybe changing this will solve it? It’s just always worked so I left it alone. Maybe it would be better to sent only image files, then I can get rid of the open try/error “return false” stuff.

		repeat with i from 1 to item_count
			set this_item to (item i of these_items)
			set the item_info to info for this_item
			if (alias of the item_info is false) and (folder of the item_info is false) then
				display dialog this_item
				set continue_loop to my process_item(this_item, i)
			else if i is 1 then
				set continue_loop to false
			else
				set continue_loop to true
			end if
			-- error reporting
			if continue_loop is false then
				tell me to activate
				display dialog "Oops, something happened." buttons "OK" default button 1 with icon note giving up after 30
				return
			end if
		end repeat

I switched to this:

		repeat with i from 1 to item_count
			set this_item to (item i of these_items)
			tell application "Finder" to set this_ext to name extension of this_item as string
			if {"psd", "jpg", "tif", "png"} contains this_ext then
				set continue_loop to my process_item(this_item, i)
			else if i is 1 then
				set continue_loop to false -- if the first item is not an image file, end script
			else
				set continue_loop to true -- any following item is not image file, skip it and move to next
			end if
			-- error reporting
			if continue_loop is false then
				tell me to activate
				display dialog "Oops, something happened." buttons "OK" default button 1 with icon note giving up after 30
				return
			end if
		end repeat

And now I’m getting “some object wasn’t found”.

Are you doing it outside the tell Photoshop block?

this works for me:



set a_file to choose file
-- this returns an alias reference to a file
	tell application "Adobe Photoshop CS5"
		set a_file to a_file as string
		open file a_file

end

basically you convert the alias reference to a string, then convert it to a file reference.

Don

Yes, I was converting to string outside of PS, then trying to open as usual. I didn’t think it would matter! Never has before… I thought a string is a string! I’ll try that Don.

A combination of this (early on, outside of PS tell):

and this:

open FILE a_file

WORKED!!! Thanks all.

This still doesn’t explain why some of my other scripts without these changes work though… I checked and they send aliases straight from the “on open” command to Photoshop and open with “open theFile” - that’s it.