A pretty descriptive name, yeah? This script is similar to Illustrator’s “Flatten Layers”, but it will leave your visible layers intact, rather than squishing everything onto one.
n.b. if you’ve carefully locked certain layers, this will reset everything to unlocked. The script could certainly be tweaked (by someone less lazy than me) to remember which layers were locked.
Download Delete invisible layers.rb.zip
What I like about this is the use of Appscript.its to operate on a bunch of items at once, rather than having to loop through them. I suspect (though I’m not sure) that this is an advantage of an Apple Events approach as opposed to ExtendScript, Adobe’s JavaScript based alternative. For more on the powerful – and sexy – filtering that Appscript.its lets you achieve, check out this section of Matt Neuberg’s book.
The source code is playfully chasing its shadow in that leafy glade over there.
#!/usr/bin/arch -i386 ruby
# Illustrator won't let me delete hidden layers containing artwork, so we'll
# invert the visibility of all the layers first (visible become invisible and
# vice versa) and then delete the visible ones.
require "rubygems"
require "appscript"
ill = Appscript.app("Adobe Illustrator")
doc = ill.current_document
# first, save a list of the layers we want to keep (i.e. that are currently
# visible)
save_these = doc.layers[Appscript.its.visible.eq(true)].get
# unlock all the layers
begin
doc.layers[Appscript.its.visible.eq(false)].locked.set(false)
rescue Appscript::CommandError
puts "There don't seem to be any invisible layers for me to delete."
exit
end
# turn on visibility of all the layers
doc.layers.visible.set(true)
# loop through our list of layers to save, making them invisible
save_these.each { |l| l.visible.set(false) }
# delete the ones we can see …
doc.layers[Appscript.its.visible.eq(true)].delete
# and turn the others back on again.
doc.layers.visible.set(true)
2 Comments
Oops! Clicking the download link throws an xml from AWS with “AccessDenied” message…
Hey Cristian, thanks for that – I’ve fixed the permissions so it should be OK now. Ian