illustrator插件-画板功能开发-多图层转多画板-js脚本开发-ai插件

1.算法程序

  illustrator是矢量编辑软件,画板是绘制处理的重要容器,开发一个多图层转多画板功能,源代码如下所示:

var doc = activeDocument;
doc.rulerOrigin = [0, 0];
var layerN = doc.layers.length;
for (var i = 1; i < layerN; i += 1) {
    doc.artboards.add([(i * doc.width) - 10000, doc.height, (doc.width * (i + 1)) - 10000, 0]);
}
doc.rearrangeArtboards(DocumentArtboardLayout.GridByRow, Math.ceil(Math.sqrt(layerN)), 20, false);
var abArr = [];
doEx("artboards").each(function() {
    abArr.push([this.artboardRect[0], this.artboardRect[3]]);
});
var i = 0;
doEx("layers").each(function() {
    this.locked = false;
    this.visible = true;
    this.hasSelectedArtwork = true;
    doEx("selection").each(function() {
        this.top += abArr[i][1];
        this.left += abArr[i][0];
    });
    doc.selection = null;
    i++;
});
Function.prototype.curry = function() {
    var slice = Array.prototype.slice;
    var args = slice.apply(arguments);
    var that = this;
    return function() {
        return that.apply(this, args.concat(slice.apply(arguments)));
    };
};

function() {
    doEx = function() {
        return new doEx.prototype.init(Array.prototype.slice.apply(arguments));
    };
    doEx.selection = doEx.curry("selection");
    doEx.prototype = {
        init: function() {
            if (!this.length) {
                this.length = 0
            }
            var arg = Array.prototype.slice.apply(arguments)[0];
            this.log.push("doEX_init : " + arg.toString());
            for (var i = 0; i < arg.length; i += 1) {
                var s = arg[i];
                try {
                    switch (s.constructor.name) {
                        case "Object":
                            try {
                                for (var x in s) {
                                    this[x](s[x]);
                                }
                            } catch (e) {
                                this.error.push("init_Object_error : " + e);
                            }
                            break;
                        case "Function":
                            return doEx(s());
                        case "String":
                            try {
                                if (typeof this.objectShortName[s] == "string") {
                                    s = this.objectShortName[s]
                                }
                                s = app.activeDocument[s];
                                this.add(s);
                            } catch (e) {
                                this.error.push("objectShortName_error : " + e);
                            }
                            break;
                        case "Array":
                            this.add(s);
                            break;
                        default:
                            this.add(s);
                    }
                } catch (e) {
                    this.add(s);
                }
            }
            return this;
        },
        add: function(s) {
            this.log.push("doEX_add : " + s.toString());
            var len = this.length;
            try {
                if (typeof s === "string") {
                    s = doEx(s)
                }
                if (s.length) {
                    s = Array.prototype.slice.call(s, 0);
                    for (var i = 0; i < s.length; i += 1) {
                        this[len + i] = s[i];
                    }
                    this.length = len + s.length;
                } else {
                    this[len] = s;
                    this.length++;
                }
            } catch (e) {
                this[len] = s;
                this.length++;
                this.error.push("add_error : " + e);
            }
            return this;
        },
        toArray: function() {
            return Array.prototype.slice.call(this, 0);
        },
        saveDoc: function(path) {
            this.each(function() {
                if (this.constructor.name == "Document") {
                    this.saveAs(File(path));
                }
            });
            return this;
        },
        position: 0,
        get: function(num) {
            if (typeof num == "number" && num < this.length && num >= 0) {
                return this[num];
            }
            return false;
        },
        now: function() {
            return this.get(this.position);
        },
        next: function() {
            return this.get(this.position++);
        },
        prev: function() {
            return this.get(--this.position);
        },
        loop: function() {
            if (this.position > this.length) {
                this.position = 0
            }
            return this.get(this.position++);
        },
        dofunc: function(func, arg) {
            if (func == undefined) {
                return this;
            }
            var res = true;
            if (func.constructor.name == "Function") {
                res = func.call(this)
            }
            if (res) {
                return this;
            } else {
                return {
                    each: function() {

                    }
                };
            }
        },
        eachReverse: function(fnc, arg) {
            var l = this.length;
            var res = [];
            if (typeof fnc == "function") {
                for (var i = this.length; i > 0; i--) {
                    ret = fnc.call(this[i], this[i], arg);
                    typeof ret != "undefined" && res.push(ret);
                }
            }
            if (res.length > 0) {
                return doEx(res);
            } else {
                return this;
            }
        },
        each: function(fnc, arg) {
            this.log.push("each : " + this.toArray().toString());
            var l = this.length;
            var res = [];
            if (typeof fnc == "function") {
                for (var i = 0; i < l; i += 1) {
                    ret = fnc.call(this[i], this[i], arg);
                    typeof ret != "undefined" && res.push(ret);
                }
            }
            if (res.length > 0) {
                return doEx(res);
            } else {
                return this;
            }
        },
        filter: function() {
            this.log.push("filter : " + Array.prototype.slice.apply(arguments).toString());
            if (arguments.length == 0) {
                return this;
            }
            var s = {
                TRUE: [],
                FALSE: []
            };
            var caseBy = {
                function: function(arg) {
                    while (x = this.next()) {
                        arg(x) ? s.TRUE.push(this) : s.FALSE.push(this);
                    }
                },
                string: function(arg) {
                    if (typeof this.objectShortName[arg] == "string") {
                        arg = this.objectShortName[arg]
                    }
                    if (typeof this.filters[arg] == "function") {
                        this.filters[arg].call(this, s);
                    } else {
                        var fd = this.attr(arg).toArray();
                        s.TRUE = s.TRUE.concat(fd);
                    }
                }
            };
            var extractarg = function(args) {
                for (var i = 0; i < args.length; i += 1) {
                    var arg = args[i];
                    switch (arg.constructor.name) {
                        case "Function":
                            caseBy.
                            function.call(this, arg);
                            break;
                        case "String":
                            var ARG = arg.split(">");
                            for (var j = 0; j < ARG.length; j += 1) {
                                caseBy.string.call(this, ARG[j]);
                            }
                    }
                }
            };
            extractarg.call(this, arguments);
            try {
                if (s.TRUE.length > 0) {
                    return doEx(s.TRUE);
                } else {
                    var x = doEx();
                    for (var i in x) {
                        x[i] = function() {
                            alert("ERROR:Nothing!");
                        }
                    }
                    return x;
                }
            } catch (e) {
                this.error.push("filter_error : " + e);
                return false;
            }
        },
        find: function(text) {
            this.log.push("doEX_find : " + text.toString());
            try {
                var ARG = text.split(">");
                var that = this;
                for (var i = 0; i < ARG.length; i += 1) {
                    var SP = ARG[i].split(",");
                    var it = doEx();
                    for (var j = 0; j < SP.length; j += 1) {
                        it.add(that.attr(SP[j]));
                    }
                    that = it;
                }
                this.log.push("doEX_find_return : " + that.toArray().toString());
                return that;
            } catch (e) {
                this.error.push("find_command_error : " + text);
                return this;
            }
        },
        attr: function(x, y) {
            var that = this;
            var it = doEx(that.each(function() {
                try {
                    if (typeof x == "string" && y) {
                        try {
                            if (typeof y == "string") {
                                return this[x] = y;
                            }
                            if (typeof y == "function") {
                                this[x] = y.call(this, this);
                            }
                        } catch (e) {
                            that.error.push("attr_value_set_error " + e);
                        }
                    }
                    if (typeof x == "object") {
                        for (var i in x) {
                            if (this.hasOwnProperty(i)) {
                                try {
                                    switch (typeof x[i]) {
                                        case "function":
                                            this[i] = x[i].call(this, this);
                                            break;
                                        default:
                                            this[i] = x[i];
                                    }
                                } catch (e) {
                                    that.error.push("attr_value_error : " + e);
                                }
                            }
                        }
                    }
                    if (typeof x == "string") {
                        var gx = this[x];
                        try {
                            if (gx.toString() == "") {
                                return "";
                            }
                            switch (typeof gx) {
                                case "string":
                                    return gx;
                                case "number":
                                    return gx;
                                case "function":
                                    return x + " is function";
                                case "undefind":
                                    return undefined;
                            }
                            if (gx.length) {
                                return app.name.match(/InDe/) ? Array.prototype.slice.call(gx, 0) : Array.prototype.slice.call(gx, 0);
                            } else {
                                return gx;
                            }
                        } catch (e) {
                            that.error("attr_value_error_x : " + e);
                            return false;
                        }
                    }
                    if (!x && !y) {
                        for (var i in this) {
                            try {
                                this.hasOwnProperty(i) && $.writeln(i + " : " + this[i]);
                            } catch (e) {
                                that.error.push("attr_value_error : " + e);
                            }
                        }
                    }
                } catch (e) {
                    that.error.push("attr_error_ :" + e);
                    return false;
                }
            })).toArray();
            var rt = [];
            while (r = it.shift()) {
                rt = rt.concat(r)
            }
            return doEx(rt);
        },
        search: function(x, y) {
            return doEx(this.each(function() {
                if (typeof this[x] == "string") {
                    if (this[x] == y) {
                        return this;
                    }
                }
                if (typeof this[x] == "object") {
                    if (this[x].constructor.name == y) {
                        return this;
                    }
                }
            }));
        },
        filters: {},
        error: [],
        temp: [],
        log: [],
        duplicate: function(x, y) {
            return this.each(function() {
                return this.duplicate(x, y);
            });
        },
        type: function() {
            return this.each(function() {
                return this.constructor.name;
            });
        },
        debug: function() {
            this.each(function() {
                $.bp();
            });
        },
        remove: function() {
            this.each(function() {
                this.remove();
            });
        },
        test: function(x, fnc, arg) {
            typeof fnc == "function" && fnc.call(this[x], this[x], arg);
        },
        first: function(fnc, arg) {
            typeof fnc == "function" && fnc.call(this[0], this[0], arg);
        },
        constructor: {
            name: "DocumentObjectsExtract"
        },
        saveLog: function() {

        },
        alertLog: function() {
            alert(this.log.join("\n"));
            this.log = [];
            return this;
        },
        alertError: function() {
            alert(this.error.join("\n"));
            this.log = [];
            return this;
        },
        view: function() {
            var i = 0;
            var that = this;
            var O = {};
            while (O = that.next()) {
                var D = myDialogMaker({
                    type: "dialog",
                    title: "DataBrowse",
                    opt: {},
                    pos: [0, 0, 400, 400],
                    layout: [{
                        type: "group",
                        name: "Group",
                        label: "Group",
                        pos: [0, 0, 398, 398],
                        option: {},
                        items: [{
                            type: "edittext",
                            name: "T",
                            label: "",
                            option: {},
                            pos: [16, 13, 386, 35]
                        }, {
                            type: "listbox",
                            name: "L",
                            label: "ListBox",
                            pos: [15, 46, 384, 354],
                            option: {
                                onChange: function() {
                                    this.selection && this.parent.items.T.text = this.selection.key + " : " + this.selection.value;
                                },
                                onDoubleClick: function() {
                                    if (this.selection) {
                                        doEx(O[this.selection.key]).view();
                                    }
                                }
                            }
                        }, {
                            type: "statictext",
                            name: "OT",
                            label: "",
                            pos: [16, 363, 200, 387],
                            option: {}
                        }, {
                            type: "button",
                            name: "E",
                            label: "Next",
                            pos: [250, 363, 300, 387],
                            option: {
                                onClick: function() {
                                    this.parent.parent.close();
                                }
                            }
                        }, {
                            type: "button",
                            name: "E",
                            label: "Close",
                            pos: [320, 363, 387, 387],
                            option: {
                                onClick: function() {
                                    this.parent.parent.close(2);
                                }
                            }
                        }, {
                            type: "button",
                            name: "E",
                            label: "Load",
                            pos: [200, 363, 250, 387],
                            option: {
                                onDraw: function() {
                                    this.parent.items.OT.text = O.toString() + "[" + i.toString() + "] of " + that.length;
                                    var L = this.parent.items.L;
                                    L.removeAll();
                                    for (var v in O) {
                                        try {
                                            var x = L.add("item", v + " : " + O[v].toString().slice(0, 40));
                                            x.key = v;
                                            x.value = O[v].toString();
                                        } catch (e) {
                                            var x = L.add("item", v + " : " + e);
                                            x.key = v;
                                        }
                                    }
                                }
                            }
                        }]
                    }]
                });
                D.wD.center();
                if (D.wD.show() == 2) {
                    break;
                }
                i++;
            }
            return this;
        },
        hvg: function(Sc) {
            if (!Sc) {
                Sc = 1
            }
            if (app.name == "Adobe Illustrator") {
                var rulerOrigin_Original = app.activeDocument.rulerOrigin;
                app.activeDocument.rulerOrigin = [-9600, -9600];
            }
            var hvgObject = {
                group: [],
                p_x: 0,
                p_y: 0,
                getItem: function(y, x) {
                    try {
                        return this.group[y][x];
                    } catch (e) {
                        return false;
                    }
                },
                next: function() {
                    if (this.group[this.p_y].length > this.p_x) {
                        var get = this.getItem(this.p_y, this.p_x);
                        this.p_x++;
                    } else {
                        this.p_y++;
                        this.p_x = 0;
                        if (this.group.length > this.p_y) {
                            var get = this.getItem(this.p_y, this.p_x);
                            this.p_x++;
                        } else {
                            return false;
                        }
                    }
                    if (get.toString() == "[object Object]") {
                        get = this.next()
                    }
                    return get;
                },
                rotate: function() {
                    var temp = [];
                    for (var i = 0; i < hvgObject.group.length; i += 1) {
                        hvgObject.group[i].reverse();
                        for (var j = 0; j < hvgObject.group[i].length; j += 1) {
                            if (!temp[j]) {
                                temp.push([]);
                            }
                            temp[j].push(hvgObject.group[i][j]);
                        }
                    }
                    hvgObject.group = temp;
                    return this;
                },
                reverse: function() {
                    var temp = [];
                    for (var i = 0; i < hvgObject.group.length; i += 1) {
                        hvgObject.group[i].reverse();
                    }
                    return this;
                },
                each: function(fnc, args) {
                    if (typeof fnc == "function") {
                        while (i = this.next()) {
                            fnc.call(i, i, args)
                        }
                    }
                    this.p_x = 0;
                    this.p_y = 0;
                    return this;
                },
                getRows: function() {
                    return doEx(this.group);
                },
                getAll: function() {
                    var o = [];
                    while (i = this.next()) {
                        o.push(i)
                    }
                    return o;
                },
                doEx: function() {
                    return doEx(this.getAll());
                },
                toArray: function() {
                    return this.getAll();
                },
                view: function() {
                    var x = this.group;
                    var p = [];
                    for (var i = 0; i < x.length; i += 1) {
                        var v = [];
                        for (var j = 0; j < x[i].length; j += 1) {
                            v.push(x[i][j].toString() !== "[object Object]" ? "■" : "□");
                        }
                        p.push(v.join(""));
                    }
                    alert("print\n" + p.join("\n"));
                    return this;
                }
            };
            var orderFunc = {
                o: function(a, b) {
                    return a.geometricBounds[0] - b.geometricBounds[0];
                },
                g: function(a, b) {
                    return b.n - a.n;
                },
                h: function(x) {
                    return Math.abs(x.geometricBounds[1] - x.geometricBounds[3]);
                },
                c: function(x) {
                    var z = (x.visibleBounds[3] + x.visibleBounds[1]) / 2;
                    return z;
                }
            };
            var hl = 0;
            this.each(function() {
                try {
                    if (this.hasOwnProperty("geometricBounds")) {
                        hl += orderFunc.h(this);
                    }
                } catch (e) {

                }
            });
            hl = hl / this.length;
            var group = [];
            this.each(function() {
                try {
                    if (!this.hasOwnProperty("geometricBounds")) {
                        return;
                    }
                    var ic = orderFunc.c(this);
                    for (var i = 0; i < group.length; i += 1) {
                        if (Math.abs(ic - group[i].n) < (hl * Sc)) {
                            group[i].item.push(this);
                            return;
                        }
                    }
                    group.push({
                        n: ic,
                        item: [this]
                    });
                    return;
                } catch (e) {
                    return e;
                }
            });
            var temp = [];
            group = group.sort(orderFunc.g);
            for (var i = 0; i < group.length; i += 1) {
                temp.push(group[i].item.sort(orderFunc.o));
            }
            var getCol = function() {
                var temp2 = [];
                var col = [];
                for (var i = 0; i < temp.length; i += 1) {
                    var wl = {};
                    try {
                        wl = temp[i].shift();
                        col.push(wl);
                    } catch (e) {
                        col.push(wl);
                    }
                }
                var cl = 0;
                var ml = undefined;
                for (var i = 0; i < col.length; i += 1) {
                    try {
                        if (!ml) {
                            ml = col[i].visibleBounds[0];
                            cl = Math.abs((col[i].visibleBounds[2] + col[i].visibleBounds[0]) / 2);
                        }
                        if (col[i].visibleBounds[0] < ml) {
                            ml = col[i].visibleBounds[0];
                            cl = Math.abs((col[i].visibleBounds[2] + col[i].visibleBounds[0]) / 2);
                        }
                    } catch (e) {

                    }
                }
                for (var i = 0; i < col.length; i += 1) {
                    try {
                        if (Math.abs(((col[i].visibleBounds[2] + col[i].visibleBounds[0]) / 2) - cl) < Math.abs(col[i].visibleBounds[2] - col[i].visibleBounds[0])) {
                            temp2.push(col[i]);
                        } else {
                            temp2.push({});
                            temp[i].unshift(col[i]);
                        }
                    } catch (e) {
                        temp2.push({});
                    }
                }
                return temp2;
            };
            var getL = function() {
                var l = 0;
                try {
                    for (var i = 0; i < temp.length; i += 1) {
                        l += temp[i].length;
                    }
                } catch (e) {

                }
                return l;
            };
            var temp3 = [];
            while (getL()) {
                temp3.push(getCol());
            }
            var temp4 = [];
            for (var i = 0; i < temp3.length; i += 1) {
                for (var j = 0; j < temp3[i].length; j += 1) {
                    if (!temp4[j]) {
                        temp4.push([])
                    }
                    temp4[j].push(temp3[i][j]);
                }
            }
            hvgObject.group = temp4;
            if (app.name == "Adobe InDesign") {
                hvgObject = hvgObject.rotate().rotate().rotate()
            }
            if (app.name == "Adobe Illustrator") {
                app.activeDocument.rulerOrigin = rulerOrigin_Original
            }
            return hvgObject;
        },
        isPlural: function() {
            var pluralWords = ["ArtStyles", "Artboards", "Brushes", "CharacterStyles", "Characters", "CompoundPathItems", "DataSets", "Documents", "GradientStops", "GraphItems", "GroupItems", "InsertionPoints", "Layers", "LegacyTextItems", "Lines", "MashItems", "NonNativeItems", "PageItems", "ParagraphStyles", "PathItems", "PathPoints", "Patterns", "PlacedItems", "PluginItems", "RasterItems", "Spots", "Stories", "Swatches", "SymbolItems", "Symbols", "Tags", "TextFonts", "TextFrameItems", "TextRanges", "Variables", "Views", "Words"];
        }
    };
    doEx.extend = function(target, source) {
        for (var property in source) {
            try {
                target.prototype[property] = source[property];
            } catch (e) {
                target[property] = source[property];
            }
        }
        return target;
    };
    doEx.prototype.init.prototype = doEx.prototype;
    doEx.view = function() {
        doEx(app.activeDocument).view();
    };
    var config_for_illustrator = {
        filters: {
            ParentisLayer: function(s) {
                this.each(function() {
                    if (this.parent.constructor.name == "Layer") {
                        s.TRUE.push(this)
                    }
                });
                return s;
            },
            layers: function(s) {
                this.each(function() {
                    this.constructor.name.match(/Document|Layer/) && doEx(this.layers).each(function() {
                        s.TRUE.push(this);
                    });
                });
                return s;
            },
            placedItems: function(s) {
                this.each(function(a) {
                    a.constructor.name == "PlacedItem" ? s.TRUE.push(a) : (a.constructor.name == "GroupItem" ? doEx(a.pageItems).each(arguments.callee) : s.FALSE.push(a));
                });
                return s;
            },
            rasterItems: function(s) {
                this.each(function(a) {
                    a.constructor.name == "RasterItem" ? s.TRUE.push(a) : (a.constructor.name == "GroupItem" ? doEx(a.pageItems).each(arguments.callee) : s.FALSE.push(a));
                });
                return s;
            },
            pathItems: function(s) {
                this.each(function(a) {
                    a.constructor.name == "PathItem" ? s.TRUE.push(a) : (a.constructor.name.match(/CompoundPathItem|GroupItem/) ? doEx(a.constructor.name == "GroupItem" ? a.pageItems : a.pathItems).each(arguments.callee) : s.FALSE.push(a));
                });
                return s;
            },
            compoundPathItems: function(s) {
                this.each(function(a) {
                    a.constructor.name == "CompoundPathItem" ? s.TRUE.push(a) : (a.constructor.name == "GroupItem" ? doEx(a.pageItems).each(arguments.callee) : s.FALSE.push(a));
                });
                return s;
            },
            pathPoints: function(s) {
                this.filter("pathItems").each(function() {
                    s.TRUE = s.TRUE.concat(Array.prototype.slice.call(this.pathPoints, 0));
                });
                return s;
            },
            groupItems: function(s) {
                this.each(function() {
                    if (this.constructor.name == "GroupItem") {
                        s.TRUE.push(this);
                    } else {
                        if (this.constructor.name.match(/Document|Layer/)) {
                            doEx(this.groupItems).each(function() {
                                if (this.parent.constructor.name == "Layer") {
                                    s.TRUE.push(this)
                                }
                            });
                        }
                    }
                });
                return s;
            },
            textFrames: function(s) {
                this.each(function(a) {
                    a.constructor.name == "TextFrame" ? s.TRUE.push(a) : (a.constructor.name == "GroupItem" ? doEx(a.pageItems).each(arguments.callee) : s.FALSE.push(a));
                });
                return s;
            },
            textRanges: function(s) {
                this.each(function() {
                    if (this.constructor.name == "TextRange") {
                        s.TRUE.push(this);
                        return;
                    }
                    this.constructor.name.match(/TextFrame|Story/) && doEx(this.textRanges).each(function(tr) {
                        s.TRUE.push(tr);
                    });
                });
                return s;
            },
            selection: function(s) {
                this.each(function() {
                    this.selected == true || this.selected == "PathPointSelection.ANCHORPOINT" ? s.TRUE.push(this) : s.FALSE.push(this);
                });
                return s;
            }
        },
        objectShortName: {
            I: "allPageItems",
            T: "textFrames",
            G: "groupItems",
            P: "pathItems",
            PP: "pathPoints",
            S: "selection",
            PL: "placedItems",
            L: "layers",
            R: "rasterItems"
        }
    };
    var config_for_indesign = {
        filters: {
            selection: function(s) {

            },
            allPageItems: function(s) {
                this.each(function() {
                    doEx(this.allPageItems).each(function() {
                        s.TRUE.push(this);
                    });
                });
                return s;
            },
            groupItems: function(s) {
                this.each(function() {
                    this.constructor.name == "Group" ? s.TRUE.push(this) : s.FALSE.push(this);
                });
                return s;
            },
            textFrames: function(s) {
                if (this.constructor.name !== "DocumentObjectsExtract") {
                    return s;
                }
                this.each(function() {
                    try {
                        this.constructor.name == "TextFrame" ? s.TRUE.push(this) : (this.constructor.name == "Group" ? doEx(this.textFrames).each(function() {
                            s.TRUE.push(this);
                        }) : s.FALSE.push(this));
                    } catch (e) {

                    }
                });
                return s;
            },
            paragraphs: function(s) {
                var textFrames = this.filters.textFrames.call(this, {
                    TRUE: [],
                    FALSE: []
                });
                doEx(textFrames.FALSE).each(function() {
                    this.constructor.name == "Text" || this.constructor.name == "TextColumn" || this.constructor.name == "Paragraph" || this.constructor.name == "Cell" && textFrames.TRUE.push(this);
                    this.constructor.name == "Table" && doEx(this.cells).each(function() {
                        textFrames.TRUE.push(this);
                    });
                });
                doEx(textFrames.TRUE).each(function() {
                    doEx(this.paragraphs).each(function() {
                        s.TRUE.push(this);
                    });
                });
                return s;
            },
            words: function(s) {
                var textFrames = this.filters.textFrames.call(this, {
                    TRUE: [],
                    FALSE: []
                });
                doEx(textFrames.FALSE).each(function() {
                    this.constructor.name == "Text" || this.constructor.name == "TextColumn" || this.constructor.name == "Paragraph" || this.constructor.name == "Cell" && textFrames.TRUE.push(this);
                    this.constructor.name == "Table" && doEx(this.cells).each(function() {
                        textFrames.TRUE.push(this);
                    });
                });
                doEx(textFrames.TRUE).each(function() {
                    doEx(this.words).each(function() {
                        s.TRUE.push(this);
                    });
                });
                return s;
            },
            characters: function(s) {
                var textFrames = this.filters.textFrames.call(this, {
                    TRUE: [],
                    FALSE: []
                });
                doEx(textFrames.FALSE).each(function() {
                    this.constructor.name == "Text" || this.constructor.name == "TextColumn" || this.constructor.name == "Paragraph" || this.constructor.name == "Cell" && textFrames.TRUE.push(this);
                    this.constructor.name == "Table" && doEx(this.cells).each(function() {
                        textFrames.TRUE.push(this);
                    });
                });
                doEx(textFrames.TRUE).each(function() {
                    doEx(this.characters).each(function() {
                        s.TRUE.push(this);
                    });
                });
                return s;
            },
            tables: function(s) {
                var textFrames = doEx(this).filters.textFrames.call(this, {
                    TRUE: [],
                    FALSE: []
                });
                doEx(textFrames.FALSE).each(function() {
                    if (this.constructor.name == "Table" || this.constructor.name == "Cell") {
                        this.constructor.name == "Cell" && this.tables.length > 0 && textFrames.TRUE.push(this);
                        s.TRUE.push(this);
                    }
                });
                doEx(textFrames.TRUE).each(function() {
                    doEx(this.tables).each(function() {
                        var innertable = doEx(this).filters.tables.call(this.cells, {
                            TRUE: [],
                            FALSE: []
                        });
                        doEx(innertable.TRUE).each(function() {
                            this.constructor.name == "Table" && s.TRUE.push(this);
                        });
                        s.TRUE.push(this);
                    });
                });
                return s;
            },
            cells: function(s) {
                var tables = this.filters.tables.call(this, {
                    TRUE: [],
                    FALSE: []
                });
                doEx(tables.TRUE).each(function() {
                    doEx(this.cells).each(function() {
                        s.TRUE.push(this);
                    });
                });
                return s;
            },
            rows: function(s) {
                var tables = this.filters.tables.call(this, {
                    TRUE: [],
                    FALSE: []
                });
                doEx(tables.TRUE).each(function() {
                    doEx(this.rows).each(function() {
                        s.TRUE.push(this);
                    });
                });
                return s;
            }
        },
        objectShortName: {
            I: "pageItems",
            T: "textFrames",
            G: "groupItems",
            P: "pathItems",
            PP: "pathPoints",
            S: "selection",
            PL: "placedItems",
            L: "layers",
            R: "rasterItems"
        }
    };

    function getObjextX(objectType) {
        var X = [];
        for (var x = 0; x < app.activeDocument.layers.length; x += 1) {
            X.concat(Array.prototype.slice.call(app.activeDocument.layers[x][objectType], 0));
        }
        return doEx(X);
    }
    doEx.extend(doEx, {
        concat: Array.prototype.concat
    });
    if (app.name == "Adobe Illustrator") {
        doEx.extend(doEx, config_for_illustrator);
        for (var i in config_for_illustrator.filters) {
            doEx.prototype[i] = doEx.prototype.filter.curry(i);
        }
    }
    if (app.name == "Adobe InDesign") {
        doEx.extend(doEx, config_for_indesign);
        for (var i in config_for_indesign.filters) {
            doEx.prototype[i] = doEx.prototype.filter.curry(i);
        }
    }
}();

function() {
    var setPosition = function(a, b) {
        return [a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]];
    };
    extend = function(target, source) {
        try {
            for (var property in source) {
                try {
                    target.prototype[property] = source[property];
                } catch (e) {
                    target[property] = source[property];
                }
            }
        } catch (e) {

        }
        return target;
    };
    myDialogMaker = function(option) {
        return new myDialogMaker.prototype.init(option);
    };
    myDialogMaker.prototype = {
        init: function(option) {
            if (!option) {
                var F = File.openDialog();
                F.open("r");
                eval("option = " + F.read());
                F.close();
            }
            this.wD = new Window(option.type, option.title, option.pos, option.properties);
            extend(this.wD, option.option);
            option.layout.length > 0 && this.items = this.buildLayout(this.wD, option.layout);
            return this;
        },
        dialog: function(wD, pos, Config) {
            var D = new Window("dialog", Config.label, Config.pos, Config.option);
            try {
                Config.layout.length > 0 && D.items = this.buildLayout(D, Config.layout);
            } catch (e) {

            }
            D.parents = wD;
            return D;
        },
        statictext: function(wD, pos, Config) {
            var sT = wD.add("statictext", pos, Config.label, {});
            return sT;
        },
        edittext: function(wD, pos, Config) {
            if (Config.width && Config.height && Config.size) {
                if (Config.label) {
                    var sT = this.statictext(wD, setPosition([0, 3, Config.width - (Config.size * 10), 20], pos), Config)
                }
                var sET = wD.add(Config.type, setPosition([Config.width - (Config.size * 10), 0, Config.width, Config.height], pos), Config.
                default, {
                    name: Config.name
                });
            } else {
                var Size = 0;
                if (Config.label) {
                    Size = Config.label.length;
                    var sT = this.statictext(wD, [pos[0], pos[1], (pos[0] * 1) + (Size * 10), pos[3]], Config);
                }
                var sET = wD.add(Config.type, [(pos[0] * 1) + (Size * 10), pos[1], pos[2], pos[3]], Config.
                default, Config.option);
            }
            extend(sET, Config.option);
            return sET;
        },
        checkbox: function(wD, pos, Config) {
            var cB = wD.add("checkbox", [pos[0], pos[1], (pos[2] * 1) + (Config.label.length * 10), pos[3]], Config.label);
            extend(cB, Config.option);
            return cB;
        },
        checkboxies: function(wD, pos, Config) {
            var GP = wD.add("group", pos, Config.label);
            switch (typeof Config.list) {
                case "object":
                    for (var x in Config.list) {
                        this.checkbox(GP, checkpos, Config.list[x]);
                    }
                    break;
            }
            return GP;
        },
        radiobutton: function(wD, pos, Config) {
            var RB = wD.add("radiobutton", pos, Config.label);
            extend(RB, Config.option);
            return RB;
        },
        button: function(wD, pos, Config) {
            var bT = wD.add("button", pos, Config.label, {
                name: Config.name
            });
            extend(bT, Config.option);
            return bT;
        },
        iconbutton: function(wD, pos, Config) {
            try {
                var SI = ScriptUI.newImage(Config.label);
                var iB = wD.add("iconbutton", pos, SI);
                extend(iB, Config.option);
                return iB;
            } catch (e) {
                return this.button(wD, pos, Config);
            }
        },
        dropdownlist: function(wD, pos, Config) {
            var ddl = wD.add("dropdownlist", pos);
            Config.list && this.addListItems(ddl, Config.list);
            ddl.selection = ddl.items[0];
            extend(ddl, Config.option);
            return ddl;
        },
        treeview: function(wD, pos, Config) {
            var TB = wD.add("treeview", pos, "");
            Config.list && this.addListItems(TB, Config.list);
            extend(TB, Config.option);
            return TB;
        },
        listbox: function(wD, pos, Config) {
            var TB = wD.add("listbox", pos, "");
            Config.list && this.addListItems(TB, Config.list);
            extend(TB, Config.option);
            return TB;
        },
        flashplayer: function(wD, pos, Config) {
            var FP = wD.add("flashplayer", pos);
            extend(FP, Config.option);
            return FP;
        },
        progressbar: function(wD, pos, Config) {
            var PB = wD.add("progressbar", pos);
            extend(PB, Config.option);
            return PB;
        },
        addListItems: function(TG, LIST) {
            switch (LIST.constructor.name) {
                case "Function":
                    LIST(TG);
                    break;
                case "Array":
                    for (var x in LIST) {
                        TG.add("item", LIST[x]);
                    }
                    break;
                case "Object":
                    for (var x in LIST) {
                        TG.add("item", x);
                    }
                    break;
            }
        },
        slider: function(wD, pos, Config) {
            var SL = wD.add("slider", pos, "");
            extend(SL, Config.option);
            return SL;
        },
        group: function(wD, pos, Config) {
            var GP = wD.add("group", pos, Config.label);
            GP.items = this.buildLayout(GP, Config.items);
            extend(GP, Config.option);
            return GP;
        },
        panel: function(wD, pos, Config) {
            var GP = wD.add("panel", pos, Config.label);
            GP.items = this.buildLayout(GP, Config.items);
            extend(GP, Config.option);
            return GP;
        },
        save: function() {

        },
        items: {},
        getItem: function(name) {
            return this.items.hasOwnProperty(name) ? this.items[name] : false;
        },
        temp: {},
        buildLayout: function(wD, layout) {
            var layout_L = layout.length;
            var myitems = {};
            for (var xi = 0; xi < layout_L; xi += 1) {
                var ret = myDialogMaker.prototype[layout[xi].type](wD, layout[xi].pos, layout[xi]);
                layout[xi].hasOwnProperty("name") && typeof layout[xi].name == "string" && myitems[layout[xi].name] = ret;
            }
            return myitems;
        }
    };
    myDialogMaker.prototype.init.prototype = myDialogMaker.prototype;
}();
var buildDialogLayout = myDialogMaker.prototype.buildLayout;
var addListItems = myDialogMaker.prototype.addListItems;
var MDM = myDialogMaker;

function setFont(o, f, t, s) {
    var g = o.graphics;
    var FontStyle = {
        REGULAR: ScriptUI.FontStyle.REGULAR,
        BOLD: ScriptUI.FontStyle.BOLD,
        ITALIC: ScriptUI.FontStyle.ITALIC,
        BOLDITALIC: ScriptUI.FontStyle.BOLDITALIC
    };
    g.font = ScriptUI.newFont(f, FontStyle[t] || ScriptUI.FontStyle.REGULAR, s);
}

function setFontStyle(o, f, t, s, pt, c, w) {
    setFont(o, f, t, s);
    setFGC(o, pt, c, w);
}

function setFGC(o, t, c, w) {
    try {
        var g = o.graphics;
        g.foregroundColor = g.newPen(g.PenType.SOLID_COLOR, c, w || 1);
    } catch (e) {

    }
}

function setBGC(o, t, c) {
    try {
        var g = o.graphics;
        g.backgroundColor = g.newBrush(g.BrushType.SOLID_COLOR, c);
    } catch (e) {

    }
}

function drawRact(target, rac) {
    var x = 0;
    var y = 0;
    var z = {};
    try {
        target.graphics.newPath();
        target.graphics.rectPath(rac[0], rac[1], rac[2], rac[3]);
        z = target.graphics.newBrush(target.graphics.BrushType.SOLID_COLOR, [rac[4][0], rac[4][1], rac[4][2], 1]);
        target.graphics.fillPath(z);
    } catch (e) {
        $.writeln("drawRact : " + e);
    }
}

function drawRactData(target, racdata) {
    var x = 0;
    var y = 0;
    var z = {};
    var rac = {};
    try {
        for (var i = 0; i < racdata.length; i += 1) {
            rac = racdata[i];
            target.graphics.newPath();
            target.graphics.rectPath(rac[0], rac[1], rac[2], rac[3]);
            z = target.graphics.newBrush(target.graphics.BrushType.SOLID_COLOR, [rac[4][0], rac[4][1], rac[4][2], 1]);
            target.graphics.fillPath(z);
        }
    } catch (e) {
        $.writeln("drawRact : " + e);
    }
}

function drawText(target, t, point) {
    var x = 0;
    var y = 0;
    try {
        x = typeof point[0] == "number" ? point[0] : 0;
        y = typeof point[1] == "number" ? point[1] : 0;
    } catch (e) {

    }
    try {
        target.graphics.drawString(t.text, target.graphics.newPen(target.graphics.PenType.SOLID_COLOR, [t.color[0], t.color[1], t.color[2], t.color[3]], 1), x, y, target.graphics.font);
    } catch (e) {

    }
}

function drawMosaicPict(target, picData, point) {
    var px = 0;
    var py = 0;
    try {
        px = typeof point[0] == "number" ? point[0] : 0;
        py = typeof point[1] == "number" ? point[1] : 0;
    } catch (e) {

    }
    try {
        var r = g = b = 0;
        var z = {};
        for (var i = 0; i < picData.height; i += 1) {
            for (var j = 0; j < picData.width; j += 1) {
                target.graphics.newPath();
                target.graphics.rectPath((j * picData.pictsize) + px, (i * picData.pictsize) + py, picData.pictsize, picData.pictsize);
                if (picData.type == "mono") {
                    r = g = b = picData.data[(i * picData.width) + j];
                } else {
                    r = picData.data.r[(i * picData.width) + j];
                    g = picData.data.g[(i * picData.width) + j];
                    b = picData.data.b[(i * picData.width) + j];
                }
                z = target.graphics.newBrush(target.graphics.BrushType.SOLID_COLOR, [r, g, b, 1]);
                target.graphics.fillPath(z);
            }
        }
    } catch (ex) {
        $.writeln("xxx : " + ex);
    }
}

function MosaicButton(targetButtom, mozpic) {
    targetButtom.onDraw = function() {
        drawMosaicPict(this, mozpic.normal, mozpic.point);
    };
    try {
        if (mozpic.mouseover) {
            targetButtom.addEventListener("mouseover", function(e) {
                e.target.onDraw = function() {
                    drawMosaicPict(this, mozpic.mouseover, mozpic.point);
                };
                e.target.notify("onDraw");
            }, false);
            targetButtom.addEventListener("mouseout", function(e) {
                e.target.onDraw = function() {
                    drawMosaicPict(this, mozpic.normal, mozpic.point);
                };
                e.target.notify("onDraw");
            }, false);
        }
    } catch (e) {

    }
}

2.文本转执行

  使用记事本或者notepad++等文本编辑器,将代码拷贝至文本文件,然后修改后缀名为js或者jsx,最后再illustrator软件菜单文件》脚本》其它脚本,选择脚本文件,点执行即可,也可以将脚本放在预设文件夹内,会显示再脚本下的二级子菜单。如下图所示:


AI脚本执行.png

3.作者答疑


  代码长度过长,如需全部项目或有疑问,请留言。

提示: 作者知了-联系方式1
提示: 作者知了-联系方式2

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,869评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,716评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,223评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,047评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,089评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,839评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,516评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,410评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,920评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,052评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,179评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,868评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,522评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,070评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,186评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,487评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,162评论 2 356