Skip to main content
Participating Frequently
June 2, 2020
Answered

Selecting an Object inside a group which is inside a layer

  • June 2, 2020
  • 6 replies
  • 4851 views

I've a file like this

Object (Layer Name)

> Text

> Main files

> Design

    >        <Group>

          <Path>

> Background

 

Now I want to select only <Path> .

I have a script that will select the Main group (Design) which looks like this

 

var docRef = app.activeDocument;
var layers = docRef.layers;
var myLayer = layers["Objects"]; //this defines the layer that you want to get the selection from

docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.
for(var a=0;a<docRef.groupItems.length;a++){
if (docRef.groupItems[a].name == "Design"){
docRef.groupItems[a].selected = true;
}
}

 

Can anyone help me to make that script?

 

I want to select <Path> file and change it color to a solic/gradient color.

 

This topic has been closed for replies.
Correct answer Charu Rajput

Hi,

First of all, sorry for the late reply. So we understand what you are saying, but it does not match with the screenshot that you have shared with us before. What I understand that you want to select the object either group or file or may be simple rectangle by just it names. Because, you said <Path> is name of the file but you also mention it is just a normal layer. So it a bit of confusion here. If you just want to select any item with the name irrespective of where it exists in the document, either inside the group or inside the layer, this will work.

 

For selected "Mandala" group, 

var docRef = app.activeDocument;
docRef.selection = null;
var _itemName = "Mandala"; // change name as per the requirement
try {
    var _item = docRef.pageItems.getByName(_itemName);
    _item.selected = true;
} catch (e) {
    alert("No object with name - " + _itemName + " exists!")
}

 

For selecting item with name <Path>

 

var docRef = app.activeDocument;
docRef.selection = null;
var _itemName = "<Path>"; // Change name as per requirement
try {
    var _item = docRef.pageItems.getByName(_itemName);
    _item.selected = true;
} catch (e) {
    alert("No object with name - " + _itemName + " exists!")
}

 

So, whatever you want to select just change _itemName value in the above script.  Also, getByName will select only one object even if more objects with same exists in the document.

 

NOTE: Whenever you create the object/path in Illustrator, they don't have their names. For an example : You create the rectangle using rectangle tool, what you see inside the layers panel is as "<Rectangle>", but that is not its name. To name the object you need to double click and type the name as "<Rectangle" or something else, and use that name in the above script. So, make sure <Path> is the name of the object in your document.

 

Let us know if this resolves your query.

6 replies

RahativeAuthor
Participating Frequently
June 4, 2020

Sorry to all of you but I ain't understanding a single thing you guys are saying.

 

Let me be clear again. All I want to do is

 

Scenario: 1

There is a layer named 'Object'

It has a group named 'Background'

The 'Background' Group has a subgroup named 'Mandala'

 

Scenario: 2

There is a layer named 'Object'

It has a group named 'Background'

The 'Background' Group has a subgroup named 'Mandala'

The 'Mandala' group has a file named '<Path>'

the <Path> file is just a normal layer

 

In my post, I showed a script that can select the 'Background' group but not the 'Mandala' group or the '<Path>' file.

Why do I want to select the Mandala Group or the '<Path>' file?

Because Illustrator action doesn't record the selection of a group and that's why I want to select that 'Mandala' group or the '<Path>' file and put it on the action.

 

 

That's all I wanted to do. Now plz give me a fully perfect script for that. I really appreciate it.

Thanks and sorry for the circumstances. 

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
June 5, 2020

Hi,

First of all, sorry for the late reply. So we understand what you are saying, but it does not match with the screenshot that you have shared with us before. What I understand that you want to select the object either group or file or may be simple rectangle by just it names. Because, you said <Path> is name of the file but you also mention it is just a normal layer. So it a bit of confusion here. If you just want to select any item with the name irrespective of where it exists in the document, either inside the group or inside the layer, this will work.

 

For selected "Mandala" group, 

var docRef = app.activeDocument;
docRef.selection = null;
var _itemName = "Mandala"; // change name as per the requirement
try {
    var _item = docRef.pageItems.getByName(_itemName);
    _item.selected = true;
} catch (e) {
    alert("No object with name - " + _itemName + " exists!")
}

 

For selecting item with name <Path>

 

var docRef = app.activeDocument;
docRef.selection = null;
var _itemName = "<Path>"; // Change name as per requirement
try {
    var _item = docRef.pageItems.getByName(_itemName);
    _item.selected = true;
} catch (e) {
    alert("No object with name - " + _itemName + " exists!")
}

 

So, whatever you want to select just change _itemName value in the above script.  Also, getByName will select only one object even if more objects with same exists in the document.

 

NOTE: Whenever you create the object/path in Illustrator, they don't have their names. For an example : You create the rectangle using rectangle tool, what you see inside the layers panel is as "<Rectangle>", but that is not its name. To name the object you need to double click and type the name as "<Rectangle" or something else, and use that name in the above script. So, make sure <Path> is the name of the object in your document.

 

Let us know if this resolves your query.

Best regards
RahativeAuthor
Participating Frequently
June 3, 2020

 

<Path> is the name of that file. I want to select that file.

femkeblanco
Legend
June 3, 2020

At present, <Path> (between angle brackets) is nameless. If you want to reference a path by name, you will have to name it. Once you name it (for example, Path, without angle brackets), you can select it as

 

 

app.activeDocument.layers["Objects"].groupItems["Mandala"].pathItems["Path"].selected = true;

 



RahativeAuthor
Participating Frequently
June 3, 2020

It's not working 😞

renél80416020
Inspiring
June 2, 2020

 

 

Salut!

    app.selection = null;
    var docRef = app.activeDocument;    
    try{
          var myLayer = docRef.layers["Objects"];
           myLayer.groupItems["Design"].selected = true;
    }
	catch(e){
           beep(); //comment...
	}

Avec un peu de chance, si j'ai bien compris ?

elleere

RahativeAuthor
Participating Frequently
June 3, 2020
I can't understand what are you saying. Can you plz translate into English?
Charu Rajput
Community Expert
Community Expert
June 3, 2020

Hi,

You can use translate option available here 🙂 May be you have noticed that. 🙂

Best regards
Charu Rajput
Community Expert
Community Expert
June 2, 2020

If your design looks like attached screen shot and assuming there is only single path inside the main group "Design", then following snippet will work to select the path

 

 

var docRef = app.activeDocument;
var layers = docRef.layers;
var layerName = "Objects";
docRef.selection = null;  //ensure there is nothing in the document selected already. this way you only get the selection you want.
try {
    var myLayer = layers.getByName(layerName); //this defines the layer that you want to get the selection from
    for (var a = 0; a < docRef.groupItems.length; a++) {
        if (docRef.groupItems[a].name == "Design") {
            var pathItems = docRef.groupItems[a].pathItems;
            if(pathItems.length ==  1){
                pathItems[0].selected = true;
            }
        }
    }
} catch (e) {
    alert("Layer with name - " + layerName + " doesn't exists");
}

 

If you have different work flow scenario, please let us know

 

Best regards
Charu Rajput
Community Expert
Community Expert
June 2, 2020

Hi, Could you give more explanation

What is

  • Text ? Is it textframe
  • Main Files ? is it group or layer with name "Main files" or something else
  • What do you mean by "I want to select <Path> file", What is Path file? It is path or a file?

If you provide more information, then we can help to select the path inside the group, also which path you want to slelect, there can be more paths inside the group. Is there any specifc name of the Path.

 

If you can share screenshot of the layers panel of your design that would be great.

 

Best regards
femkeblanco
Legend
June 2, 2020

How do you plan to select "Path"?  Do you plan to selct it based on it's name ("Path")?  Do you plan to selct it based on it being the bottom-most item in the "Design" group?  Do you plan on selecting it based on it being a non-group item in the "Design" group? ...