Files
DP/PSMARK代码块/读取图层json.jsx

51 lines
1.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 检查是否有活动文档
if (app.documents.length > 0) {
var doc = app.activeDocument; // 获取活动文档
// 递归函数来构建所有图层的字符串表示
function buildLayersString(layerSet, indent) {
var result = "";
var currentIndent = Array(indent + 1).join(" "); // 根据缩进级别构建缩进字符串
for (var i = 0; i < layerSet.layers.length; i++) {
var layer = layerSet.layers[i];
var layerName = layer.name.replace(/"/g, '\\"'); // 转义引号
if (layer.typename === 'ArtLayer') {
// 构建图层的字符串表示
result += '\n' + currentIndent + '\"图层: ' + layerName + '\": null';
} else { // LayerSet
// 构建图层组的字符串表示,并递归处理子图层
var childResult = buildLayersString(layer, indent + 1);
if (childResult !== "") {
result += '\n' + currentIndent + '\"图层组: ' + layerName + '\": {' + childResult + '}';
}
}
if (i < layerSet.layers.length - 1) {
result += ','; // 如果不是最后一个图层或图层组,添加逗号
}
}
return result;
}
var layersString = "{" + buildLayersString(doc, 0) + "\n}";
// 删除最后一个逗号
layersString = layersString.replace(/,\s*$/, '');
// 创建文件并写入数据
var desktopPath = Folder.desktop.fsName; // 获取桌面路径
var filePath = desktopPath + "/photoshop_layers.json"; // 文件完整路径
var file = new File(filePath);
// 打开文件进行写入并使用GBK编码
file.open("w");
file.encoding = "GBK";
file.write(layersString);
file.close();
alert("文件已保存至桌面: photoshop_layers.json");
} else {
alert("没有打开的文档");
}