遍历图层并将其设置为可见

时间:2019-03-26 20:30:43

标签: javascript adobe-indesign

我试图遍历InDesign文档中的各个层,并将所有层设置为可见。这是为了确保文件收集正确进行。

我整理了以下内容

var myDocument = app.activeDocument;

//make all layers visable
for (i = 0; i < myDocument.layers.length; i++) {    
    if(myDocument.layers[i].visible = false) {  
        myDocument.layers[i].visible = true;
    };  
}; 

这摘自一个自动执行文件收集的较大脚本,这只是图层的例行程序。

对于上下文,这是实际的脚本。

function Left(str, n){
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

if (app.documents.length != 0){
    var myDocument = app.activeDocument;
    var docName = myDocument.name;
    var docName = Left(docName, String(docName).length-5)
    //alert(docName);
    var myFolder = new Folder ("~/Desktop/"+docName+"/");
    //myFolder.create("Bob");s

    /*new Folder ("~/Desktop/Collected/Hi-Res PDF/");
    new Folder ("~/Desktop/Collected/RELEASE INFO/");*/ 

//make all layers visable
for (i = 0; i < myDocument.layers.length; i++) { 

    if(myDocument.layers[i].visible = false) {  

        myDocument.layers[i].visible = true;
    };  
};  

myDocument.packageForPrint (myFolder,1,1,0,1,0,0,0);

var newFolder = new Folder ("~/Desktop/"+docName+"/RELEASE INFO/");
newFolder.create();

var inddFolder = new Folder ("~/Desktop/"+docName+"/Indesign Files/");

inddFolder.create();

var newFolder = new Folder ("~/Desktop/"+docName+"/IDML Files/");
newFolder.create();

//Export IMDL File

myDocument.exportFile(ExportFormat.INDESIGN_MARKUP, File("~/Desktop/"+docName+"/IDML Files/"+docName+".idml"), false);

//Move INDD File
//var myInddfile = File("~/Desktop/"+docName+"/"+docName+".indd"); 
//myDocument.changePath(File(inddFolder),false);

//Rip Low Res PDFs

var myPDFExportPreset = app.pdfExportPresets.item("CP3 Low Rez"); 
app.activeDocument.exportFile(ExportFormat.pdfType, 
    File("~/Desktop/"+docName+"/RELEASE INFO/"+docName+"_LR.pdf"), false, myPDFExportPreset);

//Now export the document. You'll have to fill in your own file path.
//app.activeDocument.exportFile(ExportFormat.pdfType, File("~/Desktop/"+docName+"_FILM/RELEASE INFO/"+docName+"_LR.pdf"), false);

var newFolder = new Folder ("~/Desktop/"+docName+"/Hi-Res PDF/");
newFolder.create();

//Rip Hi-Res PDF

var myPDFExportPreset = app.pdfExportPresets.item("Kern Hi Rez Print"); 
app.activeDocument.exportFile(ExportFormat.pdfType, 
    File("~/Desktop/"+docName+"/Hi-Res PDF/"+docName+"_HiRes.pdf"), false, myPDFExportPreset);

//Now export the document. You'll have to fill in your own file path.
//app.activeDocument.exportFile(ExportFormat.pdfType, File("~/Desktop/"+docName+"_FILM/Hi-Res PDF/"+docName+"_HiRes.pdf"), false);

myFolder.execute();

}
else{
    alert("Please open a document and try again.");
}

希望当脚本执行时,所有层都将设置为可见,然后将进行文件收集。

1 个答案:

答案 0 :(得分:1)

strict equalityif语句中使用三重等于。例如:

for (i = 0; i < myDocument.layers.length; i++) {    
    if(myDocument.layers[i].visible === false) {  // <-- Note the `===` instead of `=`
        myDocument.layers[i].visible = true;
    };  
}; 

甚至更好的是,您可以对其进行更改以利用Logical NOT ! operator

for (i = 0; i < myDocument.layers.length; i++) {    
    if (!myDocument.layers[i].visible) {  // <-- Change to this.
        myDocument.layers[i].visible = true;
    };  
}; 

注意:在您的示例中,实际上没有必要使用条件if语句。您可以简单地这样做:

for (i = 0; i < myDocument.layers.length; i++) {    
    myDocument.layers[i].visible = true;
};

将所有内容设置为可见

如果您实际上想使所有内容可见-包括; InDesign文档层以及子层上的所有页面项,那么您需要执行以下示例:

var myDocument = app.activeDocument;

// ...

function makeAllVisible() {
    for (i = 0, max = myDocument.layers.length; i < max; i++) {
        var  currentLayer = myDocument.layers[i];        
        currentLayer.visible = true; // Make the top level layer visible.

        // Make all sub layers visible,
        // i.e. make all page items on the current layer visible.
        var currentLayerPageItems = currentLayer.allPageItems;
        for (x = 0, len = currentLayerPageItems.length; x < len; x++) {
            currentLayerPageItems[x].visible = true
        }
    }
}

makeAllVisible(); // Invoke the function.

// ...