Photoshop merge layers will not run twice

Hi All

I have a script for Photoshop that works “ once! On the second time around I get a message:

Does anyone have any idea why this is happening? Also feel free to give me any ideas how to improve this script. If it’s any use to you then enjoy!

# merges layers within a specified group. At present it only works on top level groups.

global thisDoc
global groupToTidy

on run
	display dialog "This script will merge all layers within a group you name. You must specify the name exactly and choose one or more Photoshop files to process." default answer "Group name"
	set groupToTidy to text returned of result
	set thesefiles to choose file with multiple selections allowed
	tell application "Adobe Photoshop CS5.1"
		activate
		
		set display dialogs to never
		
		repeat with thisfile in thesefiles
			open thisfile
			set thisDoc to the current document
			log groupToTidy
			my mergeLayers()
			save thisDoc
			close thisDoc
		end repeat
		
		set display dialogs to always
		
	end tell
	
end run

on mergeLayers()
	tell application "Adobe Photoshop CS5.1"
		tell thisDoc
			try -- if there are none invisible it will throw an error
				delete (every layer of layer set groupToTidy whose visible is false)
			end try
			try -- if there are none invisible it will throw an error
				delete (every layer set of layer set groupToTidy whose visible is false)
			end try
			
			try -- there may be no groups within the group
				set mergeGroups to (name of every layer set of layer set groupToTidy whose visible is true)
				log mergeGroups
				
				repeat with i from 1 to (count of mergeGroups)
					set groupToMerge to item i of mergeGroups
					log groupToMerge
					merge layer set groupToMerge of layer set groupToTidy
				end repeat
				
			end try
			
			
			set mergeLayers to (name of every art layer of layer set groupToTidy whose visible is true)
			log mergeLayers
			
			
			repeat with i from 1 to (count of mergeLayers) - 1
				set layerToMerge to item i of mergeLayers
				log layerToMerge
				merge layer layerToMerge of layer set groupToTidy
			end repeat
			
		end tell
	end tell
end mergeLayers

Model: 2 x 2.8 GHz Quad-Core Intel Xeon
AppleScript: 2.1.2
Browser: Firefox 3.6.14
Operating System: Mac OS X (10.6)

Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)

Have you tried to not name the function the same as a variable inside of itself? I don’t know if it really matters (haven’t tried it) but it seems like bad coding to have mergeLayers() and inside of it mergeLayers. Maybe make one of them different.

Thanks SuperMacGuy

That worked perfectly. I’ve learned my lesson “ no more sloppy coding!

I’ve put in an extra bit in case of adjustment layers as well. Here’s my final script:

# merges layers within a specified group. At present it only works on top level groups.


global thisDoc
global groupToTidy

on run
	display dialog "This script will merge all layers within a group you name. You must specify the name exactly and choose one or more Photoshop files to process." default answer "Group name"
	set groupToTidy to text returned of result
	set thesefiles to choose file with multiple selections allowed
	tell application "Adobe Photoshop CS5.1"
		activate
		
		set display dialogs to never
		
		repeat with thisfile in thesefiles
			open thisfile
			set thisDoc to the current document
			my mergeTheseLayers()
			save thisDoc
			close thisDoc
		end repeat
		
		set display dialogs to always
		
	end tell
	
end run


on mergeTheseLayers()
	tell application "Adobe Photoshop CS5.1"
		tell thisDoc
			try -- if there are none invisible it will throw an error
				delete (every layer of layer set groupToTidy whose visible is false)
			end try
			try -- if there are none invisible it will throw an error
				delete (every layer set of layer set groupToTidy whose visible is false)
			end try
			
			try -- there may be no groups within the group
				set mergeGroups to (name of every layer set of layer set groupToTidy whose visible is true)
				log mergeGroups
				
				repeat with i from 1 to (count of mergeGroups)
					set groupToMerge to item i of mergeGroups
					log groupToMerge
					merge layer set groupToMerge of layer set groupToTidy
				end repeat
				
			end try
			
			
			set mergeLayers to (name of every layer of layer set groupToTidy whose visible is true)
			log mergeLayers
			
			
			repeat 2 times -- if you try to merge a layer down into an adjustment layer it will error, so this section is run twice
				repeat with i from 1 to (count of mergeLayers) - 1
					set layerToMerge to item i of mergeLayers
					log layerToMerge
					try
						merge layer layerToMerge of layer set groupToTidy
					end try
				end repeat
			end repeat
		end tell
	end tell
end mergeTheseLayers

Model: 2 x 2.8 GHz Quad-Core Intel Xeon
AppleScript: 2.1.2
Browser: Firefox 3.6.14
Operating System: Mac OS X (10.6)