首页 > 解决方案 > Javascript 中的此脚本执行两次操作。我想知道为什么?

问题描述

这段代码是用 JavaScript 编写的,由 Photoshop 运行以执行下面描述的以下操作。

这是开发人员写给我的脚本代码。这段代码的作用:

将 TITLE.txt 内容加载到 > 变量 $workingLayer 1 检查此类图层是否存在 > 如果不存在,则从 Photoshop 中的操作执行操作“NOTEXISTS”。

2 做了一些有趣的事情后的动作最终创建了一个层名为“0”的新层。

3 然后脚本应将“0”更改为从 TITLE.txt 加载的 workingLayer 变量内部的任何内容

如果存在: 1 将图层“$workingLayer”的图层名称改为“0”

2 执行动作“EXISTS”

3 将图层名称从“0”改回“$workingLayer”

所以我在这里有一位好心的绅士帮我修复了一些语法错误后对此进行了测试。

现在的问题是该动作执行了两次“NOTEXISTS”动作

我想知道为什么……嗯

这是整个代码:

function somename(){
    #target photoshop
    
    var fileRef = "C:/Users/Dharmindar/OneDrive/Desktop/NAME.txt"
    var doc = open(fileRef);
    alert(doc);
}
// this function somename is supposably testing if the name.txt can be opened and it gives an alert 

// this is anther test function I guess to see how to get layers name or document name I am not sure what he did here

function testFunction() {
    var testName = app.activeDocument.name;
    var nameWithoutExt = testName.split(".")[0];
    nameWithoutExt = nameWithoutExt + ", " + app.activeDocument.layers[0].name;
    alert(nameWithoutExt);
}






   //here he lists files from folder desktop and then he loops and searches for a file named TITLE.txt. from there he reads the value that he puts into a layer workingLayer. Later he will execute function based on the fact if the layer exists or not.
function SomeOtherFunction() {
    //Choose the desired folder
    var currentFolder = new Folder('~/desktop/');

    //Get files from the selected folder
    var currentFiles = currentFolder.getFiles(); 
    var selectedFile;

    for (i = 0; i < currentFiles.length; i++) {
        if  (currentFiles[i] == "~/desktop/NAME.txt") {
          selectedFile = currentFiles[i];
        }
    }


 // here he reads the file he opens and equates it to working layer
        selectedFile.open("r");
        var workingLayer = selectedFile.read();
    
        alert(workingLayer);

    var layers = app.activeDocument.layers;
    //alert(layers);

    for (l = 0; l < layers.length; l++) {
        //alert(layers[l].name + " - " + str); 
        var currentLayer = layers[l].name;
         // here he conditions and says if the working layer is equal to the current layer name then execute action exists from custom made 1145Actionset and later he says else, if it does not exist the layer with equal name as working layer then execute action 

        if(currentLayer.name == workingLayer) {
            currentLayer.name = "0";
            ExecuteAction("EXISTS","1145ActionSet");
        } else {
            ExecuteAction("NOTEXISTS","1145ActionSet");
        }

        RenameLayer("0", workingLayer, layers);
    }
}


//here he created function rename layer which is supposed to change layer names after the action has been completed and why was this needed? well it was needed because photoshop actions can not work with variables so what he did he changes the layer name to "0" and then executes action always on layer "0" and then after that he changes the layer 0 back to workingLayer variable value that comes from that TITLE.txt and that is pretty much all there is to it and this weird thing gives me error exactly on the line 60 it says it expects ; 
functon RenameLayer(oldName, newName, layers) {
    for (var j=0;j < layers.length; j++) {
        if (layers[j].name == oldName ){
            layers[j].name = newName;
        }
    }
}


function ExecuteAction(actionName, actionSetName) {
    try {
        //TO DO IF THE LAYER DOESNOT EXISTS
        //Action set must be created first and the action should be in that.
        //The first parameter is the name of th action to be performed and the second parameter is the name of th ActionSet
        app.doAction(actionName,actionSetName);
    } catch(e) {
        alert(e);
    }
}


SomeOtherFunction();

标签: javascriptadobephotoshop

解决方案


推荐阅读