gpt4 book ai didi

javascript - 遍历图层并将它们设置为可见

转载 作者:行者123 更新时间:2023-11-30 19:41:19 25 4
gpt4 key购买 nike

我正在尝试遍历 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.");
}

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

最佳答案

在您的 if 语句中为 strict equality 使用三等号.例如:

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.

// ...

关于javascript - 遍历图层并将它们设置为可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55365740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com