Illustrator: Delete Invisible Layers

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)
This entry was posted in illustrator. Bookmark the permalink. Trackbacks are closed, but you can post a comment.

2 Comments

  1. Cristian
    Posted July 21, 2011 at 2:29 am | Permalink

    Oops! Clicking the download link throws an xml from AWS with “AccessDenied” message…

  2. Posted July 21, 2011 at 8:56 am | Permalink

    Hey Cristian, thanks for that – I’ve fixed the permissions so it should be OK now. Ian

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • What’s this all about then?

    rb-appscript is a powerful way to automate your Mac. It's similar to Applescript but it's built in Ruby, which means you get to leverage that language's powerful syntax and libraries. Giving you more time to make toasted sandwiches. You want long-winded explanations? We got those.

    Please note that these scripts are by no means bulletproof, and are intended to provide examples of application scripting. I take no resposibility for any loss of data or mishaps; you download and run everything at your own risk. That said, I use all of these scripts in a production environment every day (well – more accurately, I use some of these scripts in a production environment most days).