Problems controlling graphic layers of image in InDesign

Cheers!
A new problem which is making me crazy… :wink:

(Script needs a photoshop image named PHOTOSHOPIMAGE containing two photoshop layers placed on the InDesign page)

When running the script as you see it below it switches graphics layer 1 on and off on each run.

if i activate this part…

if current visibility of graphic layer 2 is false then
set current visibility of graphic layer 2 to true
else
if current visibility of graphic layer 2 is true then
set current visibility of graphic layer 2 to false
end if
end if

…seperately it switches layer 2 on and of on each run, but if I activate both I get an Error -1728 “Can’t get current visibility of graphic layer 2…”

Another curiosity is if I activate:
set TheLayer to graphic layer 1 of graphic layer options of theGraph
set TheLayerName to name of TheLayer
return TheLayerName

I get the name of the InDesign Document …and I KNOW it points to the layer of the image… how can this be? A bug?

Greatful for any clues!!! :cool:

tell application "Adobe InDesign CC 2014"
	tell active document
		
		set TheGraphics to all graphics of every spread
		repeat with i from 1 to number of items in TheGraphics
			set theGraph to (item i of TheGraphics)
			
			set theGraphicsName to name of item link of theGraph
			
			
			if theGraphicsName begins with "PHOTOSHOPIMAGE" then
				set TheGraphLayers to graphic layers of graphic layer options of theGraph
				--set TheLayer to graphic layer 1 of graphic layer options of theGraph
				--set TheLayerName to name of TheLayer
				--return TheLayerName
				
				tell graphic layer options of theGraph
					
					set update link option to keep overrides
					if current visibility of graphic layer 1 is true then
						set current visibility of graphic layer 1 to false
					else
						
						if current visibility of graphic layer 1 is false then
							set current visibility of graphic layer 1 to true
						end if
					end if
					
					--if current visibility of graphic layer 2 is false then
					--set current visibility of graphic layer 2 to true
					--else
					--if current visibility of graphic layer 2 is true then
					--set current visibility of graphic layer 2 to false
					--end if
					--end if
					
				end tell
			end if
		end repeat
	end tell
end tell

Hi. I believe that, when the options are being updated, they are unhooked from a database; the expected object can’t receive commands, as it has essentially ceased to exist. I made modifications to the way the reference is specified and also simplified your conditionals; you don’t need the secondary if statements for a boolean value.

tell application "Adobe InDesign CS3"'s document 1 to repeat with anImage in all graphics
	tell anImage
		if its item link's name contains "PHOTOSHOPIMAGE" then
			tell graphic layer options's graphic layer 1 to if current visibility is true then
				set current visibility to false
			else
				set current visibility to true
			end if
			tell graphic layer options's graphic layer 2 to if current visibility is true then
				set current visibility to false
			else
				set current visibility to true
			end if
		end if
	end tell
end repeat

Wow, it works! I struggled for several hours without getting this right, I will sit down and study what you did there…
Your script is so elegant compared to mine :rolleyes:

Big thanks Marc!!!
Johan