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

Selecting an Object inside a group which is inside a layer

Community Beginner ,
Jun 02, 2020 Jun 02, 2020

Copy link to clipboard

Copied

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.

 

TOPICS
Scripting

Views

3.9K

Translate

Translate

Report

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 3 Correct answers

Community Expert , Jun 02, 2020 Jun 02, 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

 

Screenshot 2020-06-02 at 11.15.02 PM.png

 

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

Votes

Translate

Translate
Guide , Jun 03, 2020 Jun 03, 2020

Both bits of code work for me.

 

Untitled.jpg

Are "Mandala" and "Path" definitely a groupItem and pathItem, respectively?  You can test them: 

 

alert(app.activeDocument.layers["Objects"].groupItems["Mandala"].typename);

 

 

alert(app.activeDocument.layers["Objects"].groupItems["Mandala"].pathItems["Path"].typename);

 

Do the alerts say "GroupItem" and "PathItem"? 

Votes

Translate

Translate
Community Expert , Jun 05, 2020 Jun 05, 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 documen

...

Votes

Translate

Translate
Adobe
Guide ,
Jun 02, 2020 Jun 02, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
Jun 02, 2020 Jun 02, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
Jun 02, 2020 Jun 02, 2020

Copy link to clipboard

Copied

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

 

Screenshot 2020-06-02 at 11.15.02 PM.png

 

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

Votes

Translate

Translate

Report

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
Advocate ,
Jun 02, 2020 Jun 02, 2020

Copy link to clipboard

Copied

 

 

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

Votes

Translate

Translate

Report

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 ,
Jun 02, 2020 Jun 02, 2020

Copy link to clipboard

Copied

I can't understand what are you saying. Can you plz translate into English?

Votes

Translate

Translate

Report

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 ,
Jun 03, 2020 Jun 03, 2020

Copy link to clipboard

Copied

Hi,

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

Best regards

Votes

Translate

Translate

Report

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 ,
Jun 02, 2020 Jun 02, 2020

Copy link to clipboard

Copied

rahative_0-1591160818332.png

 

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

Votes

Translate

Translate

Report

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
Guide ,
Jun 02, 2020 Jun 02, 2020

Copy link to clipboard

Copied

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;

 



Votes

Translate

Translate

Report

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 ,
Jun 03, 2020 Jun 03, 2020

Copy link to clipboard

Copied

It's not working 😞

Votes

Translate

Translate

Report

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 ,
Jun 03, 2020 Jun 03, 2020

Copy link to clipboard

Copied

femkeblanco, solution will also work in your scenario but another way of doing it where we alert user if in case oath or group or layer does not exists with name specified in the script.

 

 

#target illustrator
var docRef = app.activeDocument;
var layers = docRef.layers;
var layerName = "Objects";
var groupName = "Mandala";
var pathName = "Path";
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
    try {
        var _groupItem = myLayer.groupItems.getByName(groupName);
        try {
            var _pathItem = _groupItem.pathItems.getByName(pathName);
            _pathItem.selected = true;
        } catch (e) {
            alert("Path with name - " + pathName + " doesn't exists");
        }
    } catch (e) {
        alert("Group with name - " + groupName + " doesn't exists");
    }
} catch (e) {
    alert("Layer with name - " + layerName + " doesn't exists");
}

 

 

Let us know if this helps you.

Best regards

Votes

Translate

Translate

Report

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 ,
Jun 03, 2020 Jun 03, 2020

Copy link to clipboard

Copied

rahative_0-1591194067342.png

I tried your code but it's showing this. 

I'm sorry for replying late. The time zone is a bit different in my country 😞

Votes

Translate

Translate

Report

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 ,
Jun 03, 2020 Jun 03, 2020

Copy link to clipboard

Copied

No worries,

The error that you getting, is because "Path" is not actually the pathItem of the Illustrator. So you said this is a file with name "Path", what type of file it is. I would prefer if you can share your ai file, we can help you easily. By looking into your screen shot, "Path" seems to be a group.

 

Best regards

Votes

Translate

Translate

Report

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 ,
Jun 03, 2020 Jun 03, 2020

Copy link to clipboard

Copied

Also, I just noticed, there is a difference in your first and second screenshot, regarding the Path. You can check, first screen shot does not have arrow icon left to the Path, and second screen shot with error alert, have arrow icon on left of the "Path". What I understand it can be anything with name "Path". Please correct me if I am wrong anywhere in understanding your flow.

 

If that is the case, then update the following line in the above script

 

 var _pathItem = _groupItem.pathItems.getByName(pathName);

to

 var _pathItem = _groupItem.pageItems.getByName(pathName);

 

Let us know if this works for you.

 

Best regards

Votes

Translate

Translate

Report

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
Guide ,
Jun 03, 2020 Jun 03, 2020

Copy link to clipboard

Copied

Yes, it appears that Path in the second screenshot is a groupItem and not a pathItem.

Votes

Translate

Translate

Report

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
Guide ,
Jun 03, 2020 Jun 03, 2020

Copy link to clipboard

Copied

Both bits of code work for me.

 

Untitled.jpg

Are "Mandala" and "Path" definitely a groupItem and pathItem, respectively?  You can test them: 

 

alert(app.activeDocument.layers["Objects"].groupItems["Mandala"].typename);

 

 

alert(app.activeDocument.layers["Objects"].groupItems["Mandala"].pathItems["Path"].typename);

 

Do the alerts say "GroupItem" and "PathItem"? 

Votes

Translate

Translate

Report

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 ,
Jun 04, 2020 Jun 04, 2020

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

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 ,
Jun 05, 2020 Jun 05, 2020

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

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