展示

用法

保存:弹出保存预设对话框,请保存到脚本同级目录下的Custom_Presets文件夹内

删除:删除当前预设,注意:本地文件也会被删除

应用:应用所选预设到所有选择的图层上

源码

// 构建UI Panel 函数
var panelGlobal = this;
var palette = (function () {

    // JSON的polyfill
    "object" != typeof JSON && (JSON = {}), function () { "use strict"; var rx_one = /^[\],:{}\s]*$/, rx_two = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, rx_three = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, rx_four = /(?:^|:|,)(?:\s*\[)+/g, rx_escapable = /[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, rx_dangerous = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, gap, indent, meta, rep; function f(t) { return t < 10 ? "0" + t : t } function this_value() { return this.valueOf() } function quote(t) { return rx_escapable.lastIndex = 0, rx_escapable.test(t) ? '"' + t.replace(rx_escapable, function (t) { var e = meta[t]; return "string" == typeof e ? e : "\\u" + ("0000" + t.charCodeAt(0).toString(16)).slice(-4) }) + '"' : '"' + t + '"' } function str(t, e) { var r, n, o, u, f, a = gap, i = e[t]; switch (i && "object" == typeof i && "function" == typeof i.toJSON && (i = i.toJSON(t)), "function" == typeof rep && (i = rep.call(e, t, i)), typeof i) { case "string": return quote(i); case "number": return isFinite(i) ? String(i) : "null"; case "boolean": case "null": return String(i); case "object": if (!i) return "null"; if (gap += indent, f = [], "[object Array]" === Object.prototype.toString.apply(i)) { for (u = i.length, r = 0; r < u; r += 1)f[r] = str(r, i) || "null"; return o = 0 === f.length ? "[]" : gap ? "[\n" + gap + f.join(",\n" + gap) + "\n" + a + "]" : "[" + f.join(",") + "]", gap = a, o } if (rep && "object" == typeof rep) for (u = rep.length, r = 0; r < u; r += 1)"string" == typeof rep[r] && (o = str(n = rep[r], i)) && f.push(quote(n) + (gap ? ": " : ":") + o); else for (n in i) Object.prototype.hasOwnProperty.call(i, n) && (o = str(n, i)) && f.push(quote(n) + (gap ? ": " : ":") + o); return o = 0 === f.length ? "{}" : gap ? "{\n" + gap + f.join(",\n" + gap) + "\n" + a + "}" : "{" + f.join(",") + "}", gap = a, o } } "function" != typeof Date.prototype.toJSON && (Date.prototype.toJSON = function () { return isFinite(this.valueOf()) ? this.getUTCFullYear() + "-" + f(this.getUTCMonth() + 1) + "-" + f(this.getUTCDate()) + "T" + f(this.getUTCHours()) + ":" + f(this.getUTCMinutes()) + ":" + f(this.getUTCSeconds()) + "Z" : null }, Boolean.prototype.toJSON = this_value, Number.prototype.toJSON = this_value, String.prototype.toJSON = this_value), "function" != typeof JSON.stringify && (meta = { "\b": "\\b", "\t": "\\t", "\n": "\\n", "\f": "\\f", "\r": "\\r", '"': '\\"', "\\": "\\\\" }, JSON.stringify = function (t, e, r) { var n; if (indent = gap = "", "number" == typeof r) for (n = 0; n < r; n += 1)indent += " "; else "string" == typeof r && (indent = r); if ((rep = e) && "function" != typeof e && ("object" != typeof e || "number" != typeof e.length)) throw new Error("JSON.stringify"); return str("", { "": t }) }), "function" != typeof JSON.parse && (JSON.parse = function (text, reviver) { var j; function walk(t, e) { var r, n, o = t[e]; if (o && "object" == typeof o) for (r in o) Object.prototype.hasOwnProperty.call(o, r) && (void 0 !== (n = walk(o, r)) ? o[r] = n : delete o[r]); return reviver.call(t, e, o) } if (text = String(text), rx_dangerous.lastIndex = 0, rx_dangerous.test(text) && (text = text.replace(rx_dangerous, function (t) { return "\\u" + ("0000" + t.charCodeAt(0).toString(16)).slice(-4) })), rx_one.test(text.replace(rx_two, "@").replace(rx_three, "]").replace(rx_four, ""))) return j = eval("(" + text + ")"), "function" == typeof reviver ? walk({ "": j }, "") : j; throw new SyntaxError("JSON.parse") }) }();

    // 构建UI  在线构建网址:https://scriptui.joonas.me/

    var palette = (panelGlobal instanceof Panel) ? panelGlobal : new Window("palette");
    if (!(panelGlobal instanceof Panel)) palette.text = "预设小助手";
    palette.orientation = "column";
    palette.alignChildren = ["center", "top"];
    palette.spacing = 10;
    palette.margins = 16;

    // 定义保存文件,当前目录的config_preset.json,也可以自定义目录
    var jsonFile = File('./Presets/config_preset.json')
    var pre_folder_PATH = jsonFile.path
    var pre_folder = Folder(pre_folder_PATH)


    // 若文件夹不存在 则创建
    if (!pre_folder.exists) {
        pre_folder.create()
    }

    // 若文件不存在,则初始化
    if (jsonFile.exists) {
        jsonData = read_file(jsonFile)
    } else {
        jsonFile.open("w")
        jsonData = {
            "user_info": [{ "alert": true }
            ],
            "preset_info": [
                {
                    "name": ""
                }
            ]
        }
        jsonFile.write(JSON.stringify(jsonData, null, "\t"));
        jsonFile.close()
    }

    // 定义预设信息与下拉列表

    var preset_info = jsonData["preset_info"]
    var preset_list_array = Array()
    for (var i = 0; i < preset_info.length; i++) {
        preset_list_array[i] = preset_info[i]['name']
    }

    // UI GROUP1
    var group1 = palette.add("group", undefined, { name: "group1" });
    group1.orientation = "row";
    group1.alignChildren = ["left", "center"];
    group1.spacing = 10;
    group1.margins = 0;

    var preset_list = group1.add("dropdownlist", undefined, undefined, { name: "preset_list", items: preset_list_array });

    preset_list.selection = 0;
    preset_list.preferredSize.width = 170;
    preset_list.preferredSize.height = 25;

    // UI GROUP2
    var group2 = palette.add("group", undefined, { name: "group2" });
    group2.orientation = "row";
    group2.alignChildren = ["left", "center"];
    group2.spacing = 10;
    group2.margins = 0;

    var preset_save = group2.add("button", undefined, undefined, { name: "preset_save" });
    preset_save.text = "保存";
    preset_save.preferredSize.width = 50;
    preset_save.helpTip = "请在指定文件夹保存预设"

    var preset_delete = group2.add("button", undefined, undefined, { name: "preset_delete" });
    preset_delete.text = "删除";
    preset_delete.preferredSize.width = 50;
    preset_delete.helpTip = "删除当前列表显示的预设,注意:本地文件也会删除"

    var preset_apply = group2.add("button", undefined, undefined, { name: "preset_apply" });
    preset_apply.text = "应用";
    preset_apply.preferredSize.width = 50;
    preset_apply.helpTip = "应用预设到所有选中的图层上"

    // 按钮绑定函数
    preset_save.onClick = save_file
    preset_delete.onClick = delete_preset
    preset_apply.onClick = apply_preset

    // 读取json文件:打开、读取内容、关闭
    function read_file(jsonFile) {

        // 读取初始化文件
        jsonFile.open("r")
        data = jsonFile.read()
        data = JSON.parse(data)
        jsonFile.close()

        // 更新json文件
        jsonFile.open("w")
        var file_list = pre_folder.getFiles()

        preset_info = []
        for (var i = 0; i < file_list.length; i++) {
            var nm = file_list[i].displayName
            if (nm.substr(-3, 3) == "ffx") {
                nm = nm.substr(0, nm.length - 4)
                var arr = { "name": nm };
                preset_info.push(arr)
            }
        }
        data.preset_info = preset_info
        jsonFile.write(JSON.stringify(data, null, "\t"));
        jsonFile.close()

        return data
    }


    // 保存json文件
    function save_file() {
        jsonFile.open("w")
        preset_list.removeAll()
        app.executeCommand(3075)
        var file_list = pre_folder.getFiles()
        preset_info = []

        for (var i = 0; i < file_list.length; i++) {
            var nm = file_list[i].displayName
            if (nm.substr(-3, 3) == "ffx") {
                nm = nm.substr(0, nm.length - 4)
                var arr = { "name": nm };
                preset_info.push(arr)
                preset_list.add('item', nm)
            }
        }

        jsonData.preset_info = preset_info

        jsonFile.write(JSON.stringify(jsonData, null, "\t"));

        // // 保存成功 则提示
        var res = jsonFile.close()
        if (res == true) {
            // 配置文件alert为true 则弹窗提醒,否则不弹窗
            if (jsonData["user_info"][0]["alert"] == true) {
                alert('保存成功')
            }
        } else {
            alert('保存失败')
        }

        // 更新下拉列表
        // preset_list.add('item', save_name)
        preset_list.selection = jsonData["preset_info"].length - 1;

    }

    // 删除预设
    function delete_preset() {
        // 修改变量内容
        var ff = File(pre_folder_PATH + '/' + preset_info[current_index]["name"] + '.ffx')
        ff.remove()

        preset_info.splice(current_index, 1)
        jsonData["preset_info"] = preset_info

        // 更新配置文件
        jsonFile.open("w")
        jsonFile.write(JSON.stringify(jsonData, null, "\t"));
        jsonFile.close()


        // 更新下拉列表
        preset_list.remove(preset_list.items[current_index])
        preset_list.selection = jsonData["preset_info"].length - 1;
    }

    // 应用预设
    function apply_preset() {
        app.beginUndoGroup("Apply preset");
        var selectedLayers = app.project.activeItem.selectedLayers
        var shiftKey = ScriptUI.environment.keyboardState.shiftKey;
        if (shiftKey) {

        } else {

            for (var i = 0; i < selectedLayers.length; i++) {
                var ff = File(pre_folder_PATH + '/' + preset_info[current_index]["name"] + '.ffx')
                selectedLayers[i].applyPreset(ff);
            }
        }
        app.endUndoGroup();
    }

    current_index = 0
    // 监控列表变换,找到当前列表index
    preset_list.onChange = function () {
        current_index = preset_list.selection.index
    }

    // 面板自适应以及显示
    palette.layout.layout(true);
    palette.layout.resize();
    palette.onResizing = palette.onResize = function () { this.layout.resize(); }

    if (palette instanceof Window) palette.show();

    return palette;

}());

 

 

您的电子邮箱地址不会被公开。 必填项已用*标注