界面

使用

快速保存与读取存储的表达式内容。丢在Scripts\ScriptUI Panels内就有面板了

保存

选中一个带表达式的属性,单击保存即可保存到配置文件

删除

删除当前下拉列表的表达式

应用

把表达式应用到所选的所有属性上

弹窗

如果把配置文件的user_info中的alert后面改为true 。则保存表达式时会弹窗,否则不弹窗

源码

// 构建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.json,也可以自定义目录
var jsonFile = File('config.json')

// 若文件不存在,则初始化
if (jsonFile.exists) {
    jsonData = read_file(jsonFile)
} else {
    jsonFile.open("w")
    jsonData = {
        "user_info": [{ "alert": true }
        ],
        "exp_info": [
            {
                "name": "wiggle",
                "exp": "wiggle(1,10)"
            }
        ]
    }
    jsonFile.write(JSON.stringify(jsonData, null, "\t"));
    jsonFile.close()
}
// 定义表达式信息与下拉列表
var exp_info = jsonData["exp_info"]
var exp_list_array = Array()
for (var i = 0; i < exp_info.length; i++) {
    exp_list_array[i] = exp_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 exp_list = group1.add("dropdownlist", undefined, undefined, { name: "exp_list", items: exp_list_array });
exp_list.selection = 0;
exp_list.preferredSize.width = 110;
exp_list.preferredSize.height = 25;

var save_name = group1.add('edittext {properties: {name: "save_name"}}');
save_name.text = "wiggle";
save_name.preferredSize.width = 50;
save_name.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 exp_save = group2.add("button", undefined, undefined, { name: "exp_save" });
exp_save.text = "保存";
exp_save.preferredSize.width = 50;
exp_save.helpTip = "选中一个带表达式的属性,即可保存表达式"

var exp_delete = group2.add("button", undefined, undefined, { name: "exp_delete" });
exp_delete.text = "删除";
exp_delete.preferredSize.width = 50;
exp_delete.helpTip = "删除当前列表显示的表达式"

var exp_apply = group2.add("button", undefined, undefined, { name: "exp_apply" });
exp_apply.text = "应用";
exp_apply.preferredSize.width = 50;
exp_apply.helpTip = "应用表达式到所有选中的属性上"

// 按钮绑定函数
exp_save.onClick = save_file
exp_delete.onClick = delete_exp
exp_apply.onClick = apply_exp

// 读取json文件:打开、读取内容、关闭
function read_file(jsonFile) {
    jsonFile.open("r")
    data = jsonFile.read()
    jsonFile.close()
    data = JSON.parse(data)
    return data
}

// 保存json文件
function save_file() {
    jsonFile.open("w")
    // 判断选中的属性是否为属性组,是则跳过,直到找到有表达式的属性
    var selProList = app.project.activeItem.selectedProperties
    for (var i = 0; i <= selProList.length; i++) {
        if (selProList[i] instanceof PropertyGroup) {
            continue
        } else {
            exp_text = selProList[i].expression
            break
        }
    }
    // 如果属性有表达式 则保存
    if (exp_text.length > 1) {
        exp_info.push({
            "name": save_name.text,
            "exp": exp_text
        })
        jsonData.exp_info = exp_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('保存失败')
        }

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

// 删除表达式
function delete_exp() {
    // 修改变量内容
    exp_info.splice(current_index, 1)
    jsonData["exp_info"] = exp_info

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

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

// 应用表达式
function apply_exp(exp_content) {
    app.beginUndoGroup("Apply Expression");
    for (var i = 0; i < app.project.activeItem.selectedProperties.length; i++) {
        app.project.activeItem.selectedProperties[i].expression = exp_info[current_index]["exp"];
    }
    app.endUndoGroup();
}

current_index = 0
// 监控列表变换,找到当前列表index
exp_list.onChange = function () {
    current_index = exp_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;

}());

 

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