繁体   English   中英

为什么即使满足条件,我的if语句也不能正确执行?

[英]Why isn't my if statement executing correctly even though condition has been met?

这是一个InDesign脚本,用于将indd文件转换为jpg,然后导出并将其重命名为我的桌面上的文件夹。 一切正常,但是我要尝试做的一部分是仅导出未应用母版页“ H-广告”的页面。 我写了一条if语句,检查哪个母版页已应用于当前文档的当前页,并且表面上仅应在未将“ H-广告”用作母版页的情况下导出该页。 我知道如果添加其他条件(例如if(3!= 4)),该循环会起作用,并且它还能够提醒每个页面的母版,但是似乎可以继续并将该页面添加到无论如何,我都想导出的页面数组。

   Main();

    function Main() {
   // Check to see whether any InDesign documents are open.
  // If no documents are open, display an error message.
  if (app.documents.length > 0) {
    app.jpegExportPreferences.exportingSpread = false;

    //makes sure there is a book open
    if (app.books.length != 1)
      alert("This only works when you have one (1) book open and the     first file in that book open");
    else

    //loop through the book's stories
      for (b = 0; b < app.books[0].bookContents.length; b++) {
      // initialize pages variable
      var pages = [];
      // loop through the pages in the active document
      for (i = 0; i < app.activeDocument.pages.length; i++) {

        // initialize variable holding document name, and then rename as follows
        var myDocumentName = app.books[0].bookContents[b].fullName;
        c = app.open(app.books[0].bookContents[b].fullName);
        myDocumentName = myDocumentName.name.replace("indd", "jpg");
        myDocumentName = myDocumentName.replace("WN16", "WN16_");

        // get value of the current page's applied master
        if (app.activeDocument.pages[i].appliedMaster != null) {
        var appliedMaster = app.activeDocument.pages[i].appliedMaster.name;
        }


        // if it's not an advertising page, get the page number and add it to an array containing page numbers to export
    if (appliedMaster !== "H-ADVERTISING" && appliedMaster!= "[None]" && appliedMaster!= null) {
      alert(appliedMaster);
      pages.push(app.activeDocument.pages[i].name);
      printpages = pages.join(",");
      // set the pageString of pages to export as jpegs 
      app.jpegExportPreferences.pageString = printpages;
      // export all the pages using the export page range page string
      c.exportFile(ExportFormat.JPG, File(Folder.desktop + "/EDIT_Jpgs/" + myDocumentName));

    }

      }

    };

注意:属性“ pageString”在JPEG导出范围不全时有效,因此-请确保-您可能需要将app.jpegExportPreferences.jpegExportRange设置为'ExportRangeOrAllPages.EXPORT_RANGE'

注意2:考虑一本书可以在没有打开文档的情况下打开,或者activeDocument可能来自app.books [0] ==>之外,在这种情况下,您的(i = 0; i <app.activeDocument.pages.length; i ++)可能导致错误的值,导致目标文档在此循环中打开。

哈雷克

好吧,在我更新到InDesign 2017之后,该脚本现在可以正常工作。因此,除了InDesign中一定有错误之外,这并不是真正的答案。 我在代码中做的唯一调整就是添加了这个

        if (app.activeDocument.pages[i].appliedMaster !== null && app.activeDocument.pages[i].appliedMaster!== "H-ADVERTISING" && app.activeDocument.pages[i].appliedMaster!==null) {
           appliedMaster = app.activeDocument.pages[i].appliedMaster.name;
        }

在检查母版页实际值的代码中的前面部分,而不是检查分配了母版页值的变量。 这似乎是检查以筛选出未应用母版页的所有页的技巧(空或“ [无]”)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM