Skip to main content
Participating Frequently
February 28, 2025
Question

Problem with Selecting Layers in Illustrator JSX Script for Renaming

  • February 28, 2025
  • 3 replies
  • 584 views

 

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!

3 replies

Participant
March 4, 2025

It should return the selected item. like this,

 


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

Participating Frequently
March 4, 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!

Participating Frequently
March 4, 2025

Legend
March 1, 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.

Participating Frequently
March 4, 2025

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

Stefan Rakete
Inspiring
February 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.

Participating Frequently
March 4, 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!