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

Problem with Selecting Layers in Illustrator JSX Script for Renaming

Community Beginner ,
Feb 28, 2025 Feb 28, 2025

 

Hello,

I’m working on an Illustrator JSX script where I want to rename selected layers according to certain rules. However, I am having trouble getting the selected layers. I’ve tried using selection and other methods, but they don’t seem to work as expected. Here’s the code snippet I’ve been using:

 

var doc = app.activeDocument;
var selectedLayers = doc.selection;

 

But doc.selection is not returning the selected layers as expected. Can anyone help me with the correct way to get the selected layers in Illustrator for renaming purposes? Any suggestions or examples would be greatly appreciated!

Thank you in advance!

TOPICS
Scripting
496
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
Adobe
Engaged ,
Feb 28, 2025 Feb 28, 2025

Only items on the artboard are part of the selection DOM.  You could read all the layers of the document and populate a dialog with these items. Then you could select one or more layers of the list and apply your rules.

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 03, 2025 Mar 03, 2025

Thank you, Stefan! I really appreciate your suggestion. It makes sense that only items on the artboard are part of the selection DOM. I’ll look into creating a dialog to list all layers for selection. Your advice is very helpful!

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 ,
Feb 28, 2025 Feb 28, 2025

Basically, Stefan-san is right. It is better to do it that way.

 

If there is only one target layer, you can get it with Document.activeLayer. But if there are multiple, it is possible but you have to perform a miracle.

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 03, 2025 Mar 03, 2025

Thank you, sttk3! I appreciate your confirmation and the additional explanation about using Document.activeLayer.

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
New Here ,
Mar 03, 2025 Mar 03, 2025

It should return the selected item. like this,

Sharif_Hossain_0-1741048236332.png

 


Would you please share some more details?
some screenshoot or something to get the issue better.

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 03, 2025 Mar 03, 2025

 

// 确保可以使用 Illustrator 对象
#target illustrator

try {
    // 检查是否有文档打开
    if (app.documents.length > 0) {
        var doc = app.activeDocument;
        var selectedLayers = [];
        
        // 递归函数来检查所有图层及其子图层
        function checkLayer(layer) {
            if (layer.hasSelectedArtwork) {
                selectedLayers.push(layer);
            }
            // 检查子图层
            for (var i = 0; i < layer.layers.length; i++) {
                checkLayer(layer.layers[i]);
            }
        }
        
        // 检查所有顶层图层
        for (var i = 0; i < doc.layers.length; i++) {
            checkLayer(doc.layers[i]);
        }
        
        // 如果没有找到选中的图层,则使用当前激活的图层
        if (selectedLayers.length === 0) {
            selectedLayers.push(doc.activeLayer);
        }
        
        alert("选中的图层数量:" + selectedLayers.length);
        
        // 检查是否有选中的图层
        if (selectedLayers.length > 0) {
            var output = "";
            
            // 遍历所有选中的图层
            for (var j = 0; j < selectedLayers.length; j++) {
                var layer = selectedLayers[j];
                output += "图层 " + (j + 1) + " 信息:\n";
                output += "名称: " + layer.name + "\n";
                output += "可见性: " + (layer.visible ? "可见" : "隐藏") + "\n";
                output += "锁定状态: " + (layer.locked ? "已锁定" : "未锁定") + "\n";
                output += "不透明度: " + layer.opacity + "%\n";
                output += "子图层数量: " + layer.layers.length + "\n";
                output += "图稿数量: " + layer.pageItems.length + "\n";
                output += "------------------------\n";
            }

            // 创建文本文件保存输出信息
            var outputFile = new File(Folder.desktop + "/layers_info.txt");
            outputFile.open("w");
            outputFile.write(output);
            outputFile.close();

            alert("已将所有选中图层的信息导出到桌面的 layers_info.txt 文件中");
        } else {
            alert("请先选择要导出的图层");
        }
    } else {
        alert("请先打开一个文档");
    }
} catch(e) {
    alert("发生错误:" + e);
}

 

Hello, Sharif_Hossain.

I’ve already shared my code and a screenshot of my Illustrator interface. What I want to achieve is to output the names of the three selected layers — "layer1", "layer2", and "layer3". However, the result I got only shows "layer1".

What should I do to get the names of all three selected layers?

Once again, thank you so much for your help!

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 03, 2025 Mar 03, 2025

Snipaste_2025-03-04_15-00-59.png

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 14, 2025 Mar 14, 2025
LATEST

Awesome, thank you! I will try again.

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
New Here ,
Mar 05, 2025 Mar 05, 2025

Hi @panghutongxue  thanks for sharing the details.
I just ran your code and it is running fine when i just changed the text from chinese to english.

Sharif_Hossain_0-1741193716187.png

So i think, its more about how extendscript writes file.
It also alerts with the data perfectly

Sharif_Hossain_1-1741194251406.png

 

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