Delete Hidden Layers and Page Items in Illustrator using Recursion

Hello All,

I have written a script to delete hidden layers and hidden page items in Adobe Illustrator and it seems to work.

However, it’s not be the most impeccable programming code. Please take a look to see how you would improve this code.

I used the try/on error statements because when you set the variable everyLayer to every layer and then start deleting layers at some point it errors because the layer has been deleted. I couldn’t find a way to identify a layer using a layer ID.

Any tips would be greatly appreciated,
Carbon Quark

Code:


tell application "Adobe Illustrator"
	activate
	try
		set documentName to name of current document
	on error
		display dialog "Please make sure your document is open before running this script." buttons {"OK"} default button "OK" with icon 2
		return 0
	end try
	display dialog "Please make sure your document has been saved before running this script." & return & return & "This will delete all hidden layers." & return & return & "Are you sure you want to do this?" with icon 2
end tell

deleteLayers(0)

tell application "Adobe Illustrator" to display dialog "All hidden layers have been deleted." with icon 1

on deleteLayers(subLayers)
	try
		tell application "Adobe Illustrator"
			repeat with aLayer in subLayers
				if (count of aLayer) is not 0 then
					set asubLayer to every layer of aLayer
					if visible of aLayer is false then
						set locked of aLayer to false
						set visible of aLayer to true
						delete aLayer
					end if
					my deleteLayers(asubLayer)
				end if
				set everyItem to every page item of current document
				repeat with thisItem in everyItem
					if hidden of thisItem is true then delete thisItem
				end repeat
			end repeat
		end tell
	on error
		try
			tell application "Adobe Illustrator"
				set everyLayer to every layer of current document
				repeat with aLayer in everyLayer
					if visible of aLayer is false then
						set locked of aLayer to false
						set visible of aLayer to true
						delete aLayer
					end if
					if (count of layers of aLayer) is not 0 then
						set subLayers to every layer of aLayer
						my deleteLayers(subLayers)
					end if
					set everyItem to every page item of current document
					repeat with thisItem in everyItem
						if hidden of thisItem is true then delete thisItem
					end repeat
				end repeat
			end tell
		on error
			deleteLayers(0)
		end try
	end try
end deleteLayers

Hi Carbon

I ran your script a few times and it didn’t error.
although in the past deleting layers as given me the dreaded “connection invalid” error.

I haven’t wrote this script i found it somewhere on the net so all credit to go to whoever created it but this deletes hidden layers.

deleteHiddenLayers(1)


on deleteHiddenLayers(doc)
	tell application "Adobe Illustrator"
		if class of doc is integer or class of doc is string then set doc to document doc
		set targetLayers to {}
		tell doc
			set allLayers to layers
			repeat with i from 1 to count of layers
				set thisLayer to item i of allLayers
				if visible of thisLayer then
					set tmp to my layersFromLayerForVisible(thisLayer, false)
					if tmp is not {} then set targetLayers to targetLayers & tmp
				else -- it's invisible so just copy it over
					set end of targetLayers to thisLayer
				end if
			end repeat
			repeat with thisLayer in targetLayers
				set (visible of thisLayer) to true
				set (locked of thisLayer) to false
			end repeat
			delete targetLayers
		end tell
	end tell
	return targetLayers
end deleteHiddenLayers

on layersFromLayerForVisible(theLayer, matchVisible)
	tell application "Adobe Illustrator"
		set targetLayers to {}
		set allLayers to layers of theLayer
		tell theLayer
			repeat with i from 1 to count of (layers of theLayer)
				set thisLayer to item i of allLayers
				if visible of thisLayer then
					set tmp to my layersFromLayerForVisible(thisLayer, false)
					if tmp is not {} then set targetLayers to targetLayers & tmp
				else -- it's invisible so just copy it over
					set end of targetLayers to thisLayer
				end if
			end repeat
		end tell
	end tell
	return targetLayers
end layersFromLayerForVisible

Do with it as you wish!!

Hello,
I know this is an older script, but if you guys are around, can you help me with something.
Right now, it does exactly what I want…it deletes both hidden layers and hidden objects on visible layers.
The way it works now, A file is open and I double click it and it runs and asks a few questions.
Is there away to either have it batch a bunch of illustrator files in folder at one time and not ask the questions…just delete the layers and the hidden characters.
thanks!
babs

Hi

There’s a couple of ways you could do this.

  1. You could create a droplet where you would drop the folder of files on it and it would open each file up in turn do its stuff then save and close.
    You would need a simple on open handler for this, you will find a helluva lot of examples of this kind on the forum its very common, then within the handler you would use a repeat loop to go thru all the items within the folder.

  2. You would script the application to choose from a list you would then pick multiple items from with in that list and get your script to repeat thru them opening them up etc etc. close then save.

option 1 has the least user input so maybe the best option, you wouldn’t have to write much code to get your script to do what you want in this manner.

Hope this helps

Cheers

Hello,
Yes, I am trying to get it to be a droplet…but not a hurry, as I am now just running it through Illustrators script mode…so for now that will do, and it is on my list to get it to be a droplet!!!
thanks!!!
babs