Copy link to clipboard
Copied
I have been trying to find/write a script to select a layer with a specific name. However, I have no JavaScript experience, I found rather little information on scripting in Illustrator, and loads of examples I did find online do not work anymore in the latest releases of Illustrator.
There are two separate scripts I'm trying to write, namely:
– Select/highlight the layer with the name "Road"
– Select all on the currently active layer only
I got as far as this:
app.activeDocument.layers["Road"].hasSelectedArtwork = true;Which sort of does what I want. It both highlights and selects everything on the specific layer named "Roads". However, there are two problems:
– It also selects everything on this layer, even if it is locked
– Only if the layer is unlocked, the layer is selected/highlighted
I cannot seem to be able to come up with a way to select/highlight a specific (un)locked layer without also selecting everything in it. And I cannot seem to find a way to only select everything on the currently active/highlighted layer.
I hope someone can help me out.
app.activeDocument.activeLayer = app.activeDocument.layers[2]; // activates third layer from top
app.activeDocument.activeLayer.hasSelectedArtwork = true; // selects all in active layer
Copy link to clipboard
Copied
app.activeDocument.activeLayer = app.activeDocument.layers[2]; // activates third layer from top
app.activeDocument.activeLayer.hasSelectedArtwork = true; // selects all in active layer
Copy link to clipboard
Copied
Thank you, CarlosCanto, for the quick reply! That works great!
The only thing I see is that the "Select all in active layer" script also selects locked objects on that layer. Is there a simple way for the script to omit the locked objects?
Copy link to clipboard
Copied
Hello carel, yes. Instead of using hasSelectedArtwork property, loop through all items in the layer, evaluate if they're locked or not and select each accordingly using
pgItem.selected = true;
Copy link to clipboard
Copied
Thank you Carlos. That looks a bit more difficult that a single rule script, but the explanation is clear enough. I'll find my way around it. 🙂
Copy link to clipboard
Copied
kinda old thread, but I found myself here.
The fastest solution I found is to actually use hasSelectedArtwork as it evaluates way faster, then we loop through the top-level items and deselect them if they are hidden/locked (as they are not always present).
As a bonus to the speed, I get the bounding box flashing when selecting hidden items, reminding me of them.
var doc = app.activeDocument;
var layer = doc.activeLayer;
layer.hasSelectedArtwork = true;
var top = layer.pageItems;
for (var i = 0; i < top.length; i++) {
var it = top[i];
if (it.hidden || it.locked) {
it.selected = false;
}
I set this code to ctrl+A, and double-tapping Ctrl+a would select everything on the document
Copy link to clipboard
Copied
you're welcome, I could give you the code but it'll be more educational if you try doing it yourself.
post back if you get stuck. We'll help you get you back on track.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more