Photoshop脚本(javascript)。将tiff文件复制到模板,更改文本和保存。希望改进代码

时间:2017-09-20 12:56:13

标签: javascript photoshop

我对脚本编写起来非常陌生(这是我的第一个脚本之一,所以请不要过于嘲笑它是多么低效或奇怪)。 我试图让我的脚本将tiff图像复制到模板文件中,更改模板文件中的文本,然后使用tiff文件中的名称保存。此时它会在下一个打开的tiff图像上移动并执行相同的操作。

它有效,但我想知道我是否可以改进我的代码。

特别是我的if语句将文件编号链接到文本名称是非常冗长的,并且感觉有更好的方法可以做到这一点,但我不太确定要采取哪种方向。

任何建议都将不胜感激。谢谢!

var dlg=  
"dialog{text:'Script Interface',bounds:[100,100,500,220],"+   
"txt_static:StaticText{bounds:[15,20,390,79] ,text:'Dont forget to change the PN and the SN of the template file first!! Click ok to continue, cancel to stop the script.', properties:{multiline:true}},"+
"btn_ok:Button{bounds:[10,80,190,101] , text:'ok' },"+ 
"btn_closedlg:Button{bounds:[210,80,390,101] , text:'Cancel' }};"  

var win = new Window(dlg,'PSD Creator');  

win.center();  

win.btn_ok.onClick = function() {   

    win.close(1);

    var doc = app.activeDocument;
    var openDoc = app.documents;
    var docNum = openDoc.length;

    //select tiff, select template, paste change text
    for (var i = 1; i < docNum; i++){   

        var layerNum = doc.layers.length

        app.activeDocument = app.documents[i]; // Select that document.

        //Tiff document name and its path after the new document is selected
        var setName = app.activeDocument.name;
        var setLenth = setName.length;
        var viewNumber = setName.slice (setLenth-8,setLenth-4);
        var tiffPath = app.activeDocument.path;
        //also document path name in order to use it as a file name
        var psdName = setName.substring (0,setLenth-19);

        app.activeDocument.selection.selectAll(); //select all
        app.activeDocument.selection.copy(); //copy;

        app.activeDocument = app.documents[0]; //select template
        app.activeDocument.activeLayer = app.activeDocument.layers["Layer 0"]; //select layer [0]
        app.activeDocument.paste();//paste


        //////////////////////////////////////
        //Changing the text inside the view //
        //////////////////////////////////////

        var textLayer = app.activeDocument.layers["VIEW TEXT"]//select layerset "VIEW TEXT"
        app.activeDocument.layers.getByName("VIEW TEXT").visible = true;                
        var x = viewNumber.toString();

            if (x == "0001") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("GL");
            }
            if (x == "0002") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("GL");
            }
            if (x == "0003") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("Z2 LF");
            }                   
            if (x == "0004") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("Z1 LA");
            }           
            if (x == "0005") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("Z3 LA");
            }
            if (x == "0006") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("Z4 RLF");
            }
            if (x == "0007") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("AL");
            }
            if (x == "0008") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("Z3 LF");
            }
            if (x == "0009") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("Z2 LF");
            }
            if (x == "0010") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("Z2 LA");
            }
            if (x == "0011") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("Z3 RLF");
            }
            if (x == "0012") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("CA");
            }
            if (x == "0013") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("Z4 LF");
            }
            if (x == "0014") {
                app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents = ("Z4 RLA");

            }

            ////////
            //END //
            ////////


        var viewName = app.activeDocument.layers.getByName("VIEW TEXT").textItem.contents;

        function saveTemplate (name) {

            var file = new File(tiffPath + "/" + name + '.psd');
            var saveOptions = new PhotoshopSaveOptions();

            saveOptions.embedColorProfile = true;
            saveOptions.layers = true;

            doc.saveAs(file, saveOptions, true);
        }       

        saveTemplate(psdName + " " + viewName.toLowerCase());

        app.activeDocument.activeLayer.remove();

    }



}

win.show();

0 个答案:

没有答案