Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Simple script for selecting a specific layer

Community Beginner ,
Mar 06, 2020 Mar 06, 2020

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.

6.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 06, 2020 Mar 06, 2020
app.activeDocument.activeLayer = app.activeDocument.layers[2]; // activates third layer from top
app.activeDocument.activeLayer.hasSelectedArtwork = true; // selects all in active layer
Translate
Adobe
Community Expert ,
Mar 06, 2020 Mar 06, 2020
app.activeDocument.activeLayer = app.activeDocument.layers[2]; // activates third layer from top
app.activeDocument.activeLayer.hasSelectedArtwork = true; // selects all in active layer
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 06, 2020 Mar 06, 2020

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 06, 2020 Mar 06, 2020

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 06, 2020 Mar 06, 2020

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. 🙂

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 30, 2025 Nov 30, 2025
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 06, 2020 Mar 06, 2020

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines