aijs 添加物件

1.配置文件

{
  "window": {
    "title": "AddShape",
    "author": "jialan75",
    "date": "2022年5月14日08:57:43",
    "buttons": [
      "添加文字",
      "添加多个文字",
      "添加图片",
      "添加多个图片",
      "添加PDF",
      "添加多个PDF",
      "添加线条",
      "添加多个线条",
      "添加矩形",
      "添加多个矩形",
      "添加椭圆",
      "添加多个椭圆"
    ]
  },
  "添加文字": [
    {
      "type": "text",
      "content": "你在就好了",
      "fontSize": 20,
      "fontName": "微软雅黑",
      "outline": true,
      "alignment": "left",
      "rotate": 0,
      "fillColor": "regcolor",
      "x": 0,
      "y": 0
    }
  ],
  "添加多个文字": [
    {
      "type": "text",
      "content": "你在就好了",
      "fontSize": 20,
      "fontName": "微软雅黑",
      "outline": true,
      "alignment": "left",
      "rotate": 0,
      "fillColor": "regcolor",
      "x": 0,
      "y": 0
    },{
      "type": "text",
      "content": "俺就觉得\n记得",
      "fontSize": 20,
      "fontName": "微软雅黑",
      "outline": true,
      "alignment": "left",
      "rotate": 0,
      "fillColor": "regcolor",
      "x": 0,
      "y": -30
    }
  ],
  "添加图片": [
    {
      "type": "picture",
      "path": "C:/Users/Administrator/Desktop/未标题-5_画板 1.jpg",
      "x": 100,
      "y": 100,
      "w": 100,
      "h": 100,
      "embed": true
    }
  ],
  "添加多个图片": [
    {
      "type": "picture",
      "path": "C:/Users/Administrator/Desktop/未标题-5_画板 1.jpg",
      "x": 100,
      "y": 100,
      "w": 100,
      "h": 100,
      "embed": true
    },{
      "type": "picture",
      "path": "C:/Users/Administrator/Desktop/QQ截图20220513094318.png",
      "x": 100,
      "y": 100,
      "w": 100,
      "h": 100,
      "embed": true
    }
  ],
  "添加PDF": [
    {
      "type": "pdf",
      "path": "\\\\tom\\d\\2022\\2 Pend\\202205141032 813832-长梁无芯@W 600g 白 吸塑 21600.pdf",
      "x": 100,
      "y": 100,
      "w": 0,
      "h": 0,
      "index": 1,
      "embed": true
    }
  ],
  "添加多个PDF": [
    {
      "type": "pdf",
      "path": "\\\\tom\\d\\2022\\2 Pend\\202205141032 813832-长梁无芯@W 600g 白 吸塑 21600.pdf",
      "x": 100,
      "y": 100,
      "w": 0,
      "h": 0,
      "index": 1,
      "embed": true
    },{
      "type": "pdf",
      "path": "\\\\tom\\d\\2022\\2 Pend\\202205141032 813831-30mm短梁无芯@W 600g 白 吸塑 16200.pdf",
      "x": 100,
      "y": 350,
      "w": 100,
      "h": 100,
      "index": 1,
      "embed": true
    }
  ],
  "添加矩形": [
    {
      "type": "rect",
      "x": 0,
      "y": 50,
      "w": 10,
      "h": 10,
      "expand": 0,
      "strokeWidth": 0.2,
      "fillColor": "nocolor",
      "strokeColor": "regcolor"
    }
  ],
  "添加多个矩形": [
    {
      "type": "rect",
      "x": 200,
      "y": -150,
      "w": 10,
      "h": 10,
      "expand": 0,
      "strokeWidth": 0.2,
      "fillColor": "nocolor",
      "strokeColor": "regcolor"
    },{
      "type": "rect",
      "x": 210,
      "y": -150,
      "w": 10,
      "h": 10,
      "expand": 0,
      "strokeWidth": 0.2,
      "fillColor": "nocolor",
      "strokeColor": "100-100-0-0"
    },{
      "type": "rect",
      "x": 220,
      "y": -150,
      "w": 10,
      "h": 10,
      "expand": 0,
      "strokeWidth": 0.2,
      "fillColor": "PANTONE 485 C-0-100-100-0-100",
      "strokeColor": "regcolor"
    }
  ],
  "添加线条": [
    {
      "type": "line",
      "x1": 51,
      "y1": 52,
      "x2": 10,
      "y2": 10,
      "strokeWidth": 0.5,
      "strokeColor": "PANTONE 485 C-0-100-100-0-100"
    }
  ],
  "添加多个线条": [
    {
      "type": "line",
      "x1": 200,
      "y1": -200,
      "x2": 220,
      "y2": -200,
      "strokeWidth": 0.5,
      "strokeColor": "PANTONE 485 C-0-100-100-0-100"
    },
    {
      "type": "line",
      "x1": 200,
      "y1": -220,
      "x2": 220,
      "y2": -220,
      "strokeWidth": 0.5,
      "strokeColor": "100-0-100-0"
    }
  ],
  "添加椭圆": [
    {
      "type": "ellipse",
      "x": 0,
      "y": 50,
      "w": 10,
      "h": 10,
      "expand": 0,
      "strokeWidth": 0.2,
      "fillColor": "PANTONE 485 C-0-100-100-0-100",
      "strokeColor": "regcolor"
    }
  ],
  "添加多个椭圆": [
    {
      "type": "ellipse",
      "x": 200,
      "y": -300,
      "w": 10,
      "h": 10,
      "expand": 0,
      "strokeWidth": 0.2,
      "fillColor": "PANTONE 485 C-0-100-100-0-100",
      "strokeColor": "regcolor"
    },
    {
      "type": "ellipse",
      "x": 200,
      "y": -320,
      "w": 10,
      "h": 10,
      "expand": 0,
      "strokeWidth": 0,
      "fillColor": "nocolor",
      "strokeColor": "0-0-0-100"
    }
  ]
}

2.代码

#targetengine main

var SET = new Setting();

eval('var map = '+new Txt("C:\\配置.json").read());
main(map);

function main(data) {
    new MyWindow(data.window).create_window();
}
function MyWindow(data) {
    this.data = data;
    var that = this;
    this.create_window = function () {
        var win = new Window('palette', this.data.title);
        win.add("statictext { text: '"+this.data.author+"'}");
        win.add("statictext { text: '"+this.data.date+"'}");

        for (var i = 0; i < this.data.buttons.length; i++) {
            that.add_button_by_title_and_action_text(win,this.data.buttons[i])
        }
        win.titleLayout = {characters: 8, truncate: 'middle'};
        win.center();
        win.show();
    }
    this.add_button_by_title_and_action_text = function (win,title) {
        bt = win.add("iconbutton { title: '"+title+"'}")
        bt.onClick=function(){that.action_bridge_talk_text(title)}
    }
    this.action_bridge_talk_text = function (title) {
        var bt = new BridgeTalk();
        bt.target = "illustrator";
        bt.body = "work('info')".replace('info',title);
        bt.send();
    }
}


function work(title) {
    for (var i = 0; i < map[title].length; i++) {
        var obj = map[title][i];
        if (obj.type === SET.types.text) {
            new Text(obj).work();
        } else if (obj.type === SET.types.picture) {
            new Picture(obj).work();
        } else if (obj.type === SET.types.pdf) {
            new Pdf(obj).work();
        } else if (obj.type === SET.types.rect) {
            new Rect(obj).work();
        }else if (obj.type === SET.types.line) {
            new Line(obj).work();
        }else if (obj.type === SET.types.ellipse) {
            new Ellipse(obj).work();
        }
    }
}
function Txt(path) {
    this.path = path;
    this.read = function () {
        try{
            var f =new File (this.path);//文件位置
            f.open('r');
            var txt=f.read();
            f.close();
            return txt;
        }catch (e){
            return '';
        }
    }

}
function Setting() {
    this.alignment = {
        "center": Justification.CENTER,
        "left": Justification.LEFT,
        "right": Justification.RIGHT,
    }
    this.getColor =function (color) {
        var colors = {
            "white": create_color_plus("0-0-0-0"),
            "black": create_color_plus("0-0-0-100"),
            "cyan": create_color_plus("100-0-0-0"),
            "yellow": create_color_plus("0-0-100-0"),
            "magenta": create_color_plus("0-100-0-0"),
            "nocolor": create_color_plus('nocolor'),
            "regcolor": create_color_plus('regcolor')
        }
        if(colors[color] != undefined){
           return colors[color];
        }else{
            return create_color_plus(color);
        }
    }

    this.fontNames = {
        "微软雅黑": "MicrosoftYaHei"
    }
    this.types = {
        "text": "text",
        "picture": "picture",
        "pdf": "pdf",
        "rect": "rect",
        "line": "line",
        "ellipse": "ellipse",
    }
    return this;
}
function Text(data) {
    this.content = data.content;
    this.fontSize = data.fontSize;
    this.fontName = SET.fontNames[data.fontName];
    this.outline = data.outline;
    this.rotate = data.rotate;
    this.fillColor = SET.getColor(data.fillColor);
    this.x = data.x;
    this.y = data.y;
    this.alignment = SET.alignment[data.alignment];
    this.work = function () {
        var doc = app.activeDocument;
        var textRef = doc.textFrames.add();
        textRef.contents = this.content;
        textRef.textRange.paragraphAttributes.justification = this.alignment;
        textRef.textRange.characterAttributes.fillColor = this.fillColor;
        var text_font = null;
        try {
            text_font = textFonts.getByName(this.fontName)
        } catch (e) {
            try {
                text_font = textFonts[font_name]
            } catch (e) {
                text_font = textFonts.getByName('MicrosoftYaHei')
            }
        }
        textRef.textRange.characterAttributes.textFont = text_font;
        textRef.textRange.characterAttributes.size = this.fontSize;
        textRef.rotate = this.rotate;
        if (this.outline) textRef = textRef.createOutline();
        textRef.left = this.x;
        textRef.top = this.y;
        return textRef;
    }
}
function Picture(data) {
    this.path = data.path;
    this.x = data.x;
    this.y = data.y;
    this.w = data.w;
    this.h = data.h;
    this.embed = data.embed;
    this.work = function () {
        var aFile = File(this.path);
        var doc = app.activeDocument;
        if (aFile.exists) {
            var pt = 72.0 / 25.4;
            var myPlacedItem = doc.placedItems.add();
            myPlacedItem.file = aFile;
            myPlacedItem.position = Array(this.x * pt, this.y * pt);
            if (this.embed) myPlacedItem.embed();
            if (this.w != 0 && this.h != 0) {
                doc.selection[0].width = this.w * pt;
                doc.selection[0].height = this.h * pt;
            }
        } else {
            alert("路径不存在");
        }
    }
}
function Pdf(data) {
    this.path = data.path;
    this.index = data.index;
    this.x = data.x;
    this.y = data.y;
    this.w = data.w;
    this.h = data.h;
    this.embed = data.embed;
    this.work = function () {
        var aFile = File(this.path);
        if (!aFile.exists) {
            alert("路径不存在");
        } else {
            var pt = 72.0 / 25.4;
            var doc = app.activeDocument;
            var pdfOptions = app.preferences.PDFFileOptions;
            pdfOptions.pageToOpen = this.index;//此处选择置入页码 貌似低版本不支持这个选项
            var myPlacedItem = doc.placedItems.add();
            myPlacedItem.file = aFile;
            myPlacedItem.position = Array(this.x * pt, this.y * pt);
            if (this.embed) myPlacedItem.embed();
            if (this.w != 0 && this.h != 0) {
                doc.selection[0].width = this.w * pt;
                doc.selection[0].height = this.h * pt;
            }
        }
    }
}
function Rect(data) {
    this.x = data.x;
    this.y = data.y;
    this.w = data.w;
    this.h = data.h;
    this.strokeColor = SET.getColor(data.strokeColor);
    this.strokeWidth = data.strokeWidth;
    this.fillColor = SET.getColor(data.fillColor);
    this.expand = data.expand;
    this.work = function () {
        var pt = 72.0 / 25.4;
        var doc = app.activeDocument;
        var rect = doc.pathItems.rectangle(this.y * pt + this.expand * pt, this.x * pt - this.expand * pt, this.w * pt + this.expand * pt * 2, this.h * pt + this.expand * pt * 2);
        if(this.strokeColor != undefined){
            rect.stroked = true;
            rect.strokeColor = this.strokeColor;
            rect.strokeWidth = this.strokeWidth*pt;
        }
        if(this.fillColor != undefined){
            rect.fillColor = this.fillColor;
        }
        return rect;
    }
}
function Line(data) {
    this.x1 = data.x1;
    this.y1 = data.y1;
    this.x2 = data.x2;
    this.y2 = data.y2;
    this.strokeColor = SET.getColor(data.strokeColor);
    this.strokeWidth = data.strokeWidth;
    this.work = function () {
        var pt = 72.0 / 25.4;
        var doc = app.activeDocument;
        var line = doc.pathItems.add();
        line.setEntirePath([[this.x1 * pt, this.y1 * pt], [this.x2 * pt, this.y2 * pt]]);
        line.stroked = true;
        line.strokeWidth = this.strokeWidth*pt;
        line.strokeColor = this.strokeColor;
        line.fillColor = SET.getColor("nocolor");
        return line;
    }
}
function Ellipse(data) {
    this.x = data.x;
    this.y = data.y;
    this.w = data.w;
    this.h = data.h;
    this.strokeColor = SET.getColor(data.strokeColor);
    this.strokeWidth = data.strokeWidth;
    this.fillColor = SET.getColor(data.fillColor);
    this.expand = data.expand;
    this.work = function () {
        var pt = 72.0 / 25.4;
        var doc = app.activeDocument;
        var ellipse = doc.pathItems.ellipse(this.y * pt + this.expand * pt, this.x * pt - this.expand * pt, this.w * pt + this.expand * pt * 2, this.h * pt + this.expand * pt * 2);
        if(this.strokeColor != undefined){
            ellipse.stroked = true;
            ellipse.strokeColor = this.strokeColor;
            ellipse.strokeWidth = this.strokeWidth*pt;
        }
        if(this.fillColor != undefined){
            ellipse.fillColor = this.fillColor;
        }
        return ellipse;
    }
}
function create_color_plus(str) {
    var doc = app.activeDocument;
    /**
     * 支持4种格式
     * 意思依次是:获取无色
     * var color = create_color("nocolor");
     * 意思依次是:获取注册色
     * var color = create_color_plus("regcolor");
     * 意思依次是:C值,M值,Y值,K值
     * var color = create_color("0-0-0-100");
     * 意思依次是:专色名称,C值,M值,Y值,K值,专色百分比
     * var color = create_color("PANTONE 485 C-0-100-100-0-100");
     * 意思依次是:专色名称,C值,M值,Y值,K值,专色百分比
     * var color = create_color("cut-100-0-0-0-100");
     */
    if (str.indexOf("-") == -1) {
        if (str == 'nocolor') return new NoColor();
        if (str == 'regcolor') {
            var newSpot = doc.swatches; // 寻找注册色代码段
            for (var i = 0; i < newSpot.length; i++) {
                var swatch = doc.swatches[i];
                if (swatch.color == '[SpotColor]') {
                    var spot = swatch.color.spot;
                    var colorType = spot.colorType;
                    if (colorType.toString() == "ColorModel.REGISTRATION") {
                        index = i;
                    }
                } else {
                    continue;
                }
            }
            return doc.swatches[index].color;
        }

    } else {
        var arr = str.split("-");
        if (arr.length == 4) {
            var cmykColor = new CMYKColor();//创建颜色变量
            cmykColor.cyan = Number(arr[0]);//设置颜色的值
            cmykColor.magenta = Number(arr[1]);//设置颜色的值
            cmykColor.yellow = Number(arr[2]);//设置颜色的值
            cmykColor.black = Number(arr[3]);//设置颜色的值
            return cmykColor;
        } else if (arr.length == 6) {
            var newSpot = undefined;
            try {
                newSpot = doc.spots.getByName(arr[0]);
            } catch (e) {
                newSpot = doc.spots.add();
            }
            newSpot.name = arr[0];
            newSpot.colorType = ColorModel.SPOT;
            var cmykColor = new CMYKColor();//创建颜色变量
            cmykColor.cyan = Number(arr[1]);//设置颜色的值
            cmykColor.magenta = Number(arr[2]);//设置颜色的值
            cmykColor.yellow = Number(arr[3]);//设置颜色的值
            cmykColor.black = Number(arr[4]);//设置颜色的值
            newSpot.color = cmykColor;
            var newSpotColor = new SpotColor();
            newSpotColor.spot = newSpot;
            newSpotColor.tint = Number(arr[5]);
            return newSpotColor;
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值