Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thank you, sttk3! I appreciate your confirmation and the additional explanation about using Document.activeLayer.
Copy link to clipboard
Copied
It should return the selected item. like this,
Would you please share some more details?
some screenshoot or something to get the issue better.
Copy link to clipboard
Copied
// 确保可以使用 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!
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Awesome, thank you! I will try again.
Copy link to clipboard
Copied
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.
So i think, its more about how extendscript writes file.
It also alerts with the data perfectly
Find more inspiration, events, and resources on the new Adobe Community
Explore Now