﻿var desktop = Folder.desktop;
var file = new File(desktop + "/名称数据.json");
if (file.open('r')) {
    var content = file.read();
    file.close();

    // 移除双引号
    var content1 = content.replace(/"/g, '');

    // 移除[]并使用逗号分割字符串
    var 名称数组 = content1.replace(/^\s*\[|\]\s*$/g, '').split(',');

    for (var k = 0; k < 名称数组.length; k++) {
        var 名称 = 名称数组[k].replace(/^\s+|\s+$/g, ''); // 使用正则替代trim函数移除前后空格
        alert(名称);
        var matches = 名称.match(/\(([^)]+)\)/);
        if (matches) {
            var 图案名称 = matches[1];
            var 素材填充 = app.activeDocument.layerSets.getByName("填充底图").layers.getByName(图案名称);
            app.activeDocument.activeLayer = 素材填充;
            载入选区();
            填充图案(名称);
        }
    }
} else {
    alert("找不到名称数据文件查看是否写入！");
}


        function 载入选区()	//载入选区
    {

        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection"));
        d.putReference(stringIDToTypeID("null"), r);
        var r1 = new ActionReference();
        r1.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("transparencyEnum"));
        d.putReference(stringIDToTypeID("to"), r1);
        executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
   
    }


function 填充图案(名称)	//新建图案填充图层
    {
   
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putClass(stringIDToTypeID("contentLayer"));
        d.putReference(stringIDToTypeID("null"), r);
        var d1 = new ActionDescriptor();
        var d2 = new ActionDescriptor();
        var d3 = new ActionDescriptor();
        d3.putString(stringIDToTypeID("name"), 名称);
        
        d2.putObject(stringIDToTypeID("pattern"), stringIDToTypeID("pattern"), d3);
        d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("patternLayer"), d2);
        d.putObject(stringIDToTypeID("using"), stringIDToTypeID("contentLayer"), d1);
        executeAction(stringIDToTypeID("make"), d, DialogModes.NO);
       
    }