2012-07-10 62 views
0

据我所知,声明#targetengine "myEngineName"将用于InDesign记住全局变量(在这里找到的信息:    http://incom.org/post/89818)。
InDesign CS5脚本:为什么`#targetengine`工作不正常?


然而,这仍然是不够的,重要的是记住全局变量,因为它仍然抛出关于全局变量 imgs错误:

Error Number:  30476
Error String:  "if(imgs[i].itemLink != null)" could not be completed because the object no longer exists.

...或者类似的东西反正。它不喜欢我的代码中的特定行,似乎忘记了全局变量imgs被实例化为。


所以,我实现了一个try-catch语句,并reinstatiated变量 imgs和减少在catch迭代器......虽然这 解决的问题,为什么不 #targetengine "myEngineName"解决问题就像它应该?

这里是我的代码:

#target "InDesign" // this solves the "Error Number: 29446" problem 
#targetengine "session" // this solves the "Error Number: 30476" problem 

var imgs; // global variable for the #targetengine "session" to keep track of 
var document = app.activeDocument; 
var newFolder = createFolder(document); // if subdirectory images DNE, create this folder with the function below 

saveAllImages(document, newFolder); // with the function below 

alert("The file conversion is complete!\n\nAll black & white images have been copied to:\n" + newFolder + 
     "\.\n\nAll color images have been replaced with the new black & white images in the current InDesign document."); 

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

function createFolder(doc) 
{ 
    try 
    { 
     /* 
     * type-casting the filePath property (of type object) into a String type; 
     * must be a String type to concatenate with the subdirectory variable (also of type String) 
     */ 
     var docPath = String(doc.filePath); 
     var subdirectory = "/BLACK AND WHITE IMAGES"; 
    } 
    catch(e) 
    { 
     alert(e.message + "\n - reload the script, and it should work."); 
     exit(); 
    } 

    var imagesFolder = docPath + subdirectory; // concatenating the two variables 
    if(!Folder(imagesFolder).exists) 
    { 
     Folder(imagesFolder).create(); 
    } 

    return imagesFolder; // for instantiation outside of this function 

} // end of function createFolder 

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

function saveAllImages(doc, folder) 
{ 
    imgs = document.allGraphics; // this is a global variable, for the #targetengine "session" to keep track of 
    var fileName = ""; 
    var img = ""; 
    var imgType = ""; 

    for(var i = 0; i < imgs.length; i++) 
    { 
     try 
     { 
      if(imgs[i].itemLink != null) 
      { 
       fileName = imgs[i].itemLink.name; 

       img = new File(folder + "/" + fileName); // each image instantiated here 
       imgType = imgs[i].imageTypeName; // image type is determined here (.tif, .jpg, .png, etc..) 

       //alert("The file \"" + fileName + "\"\n\tis a " + imgType + " file."); // Note: escape characters 

       /* 
        * array for image options, instantiated from the function below; 
        * options[0] is the "MAXIMUM" image quality property, & 
        * options[1] is the "GRAY" image conversion property; 
        */ 
       var options = convertToBlackAndWhite(imgType); 

       // each image exported to the new folder here, by file type 
       switch(imgType) 
       { 
        case "GIF": 
         alert("This script cannot convert and export the GIF file:\n\t" + fileName + " !"); // Note: escape characters 
         break; 

        case "Adobe PDF": 
         break;        
        case "EPS": 
         break; 
        case "Windows Bitmap": 
         break; 
        case "JPEG": 
         break; 
        case "PNG": 
         break; 
        case "TIFF": 
         options[0]; // maximum image quality 
         options[1]; // black & white conversion 

         imgs[i].exportFile(ExportFormat.JPG, img, false); 
         replaceWithNewImage(doc, fileName, img); // with the function below 
         break; 

        default: 
         alert("\tUnlisted image type: " + imgType + "!\nAdd this type to the switch statement."); 
         break; 
       } // end of switch statement 

      } // end of if statement 
     } // end of try statement 
     catch(e) 
     { 
      /* 
       * in case the #targetengine is overloaded, this solves the "Error Number: 30476" problem: 
       *   - "The requested action could not be completed because the object no longer exists." 
       *    (the second statement #targetengine "session" is also in place to solve this error) 
       */ 
      imgs = document.allGraphics; // global variable reinstantiated in case of error 
      i--; // retry the same iteration again, in case of error (the variable " i " is the iterator in the for loop) 
     } 
    } // end of for loop 

} // end of function saveAllImages 

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

     function convertToBlackAndWhite(fileType) 
     { 
      // array for image-quality and color-conversion values 
      var settings = []; 

      // each image exported to the new folder here, by file type 
      switch(fileType) 
      {      
        case "Windows Bitmap": 
         break; 
        case "JPEG": 
         break; 
        case "PNG": 
         break; 
        case "TIFF": 
        settings[0] = "app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM"; // maximum image quality 
        settings[1] = "app.jpegExportPreferences.jpegColorSpace = JpegColorSpaceEnum.GRAY"; // black & white conversion 
        break; 

       default: 
        break; 
      } // end of switch statement 

      return settings; // for instantiation outside of this function 

     } // end of function convertToBlackAndWhite 

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

function replaceWithNewImage(doc, imageName, newImage) 
{ 
    var links = doc.links; 
    var link = ""; 
    for(var i = 0; i < links.length; i++) 
    { 
     link = links[i]; 
     if ((link.status == LinkStatus.NORMAL) && (link.name == imageName)) 
     { 
      try 
      { 
       link.relink(newImage); 
       link.update(); 
      } 
      catch(e) 
      { 
      } 
     } // end of if statement 
    } // end of for loop 

} // end of function replaceWithNewImage 


这是唯一的信息,在那里我能找到关于此错误:    http://forums.adobe.com/thread/748419

编辑 -

我很确定这个问题有些事情与功能replaceWithNewImage有关,因为没有这个功能就没有发生这个错误,那么就没有必要使用try-catch语句...

回答

4

阅读过你的代码,我看到一些可能确实存在问题的东西。您将对文档的引用设置为活动文档。但是这个参考文献仍然在整个会议中。事实是,如果您切换到其他文档或关闭文档,则引用将丢失。这也许可以解释为什么在某些时候imgs可能是未定义的,尽管我认为它应该引发一个错误。 做你的变量包装在一个功能范围,我保证你一切都会好起来的)

+0

有趣...所以使用'app.activeDocument'我尝试了'app.documents [0]',现在这个错误不会发生。所以它看起来似乎变得未定义,因为'app.activeDocument';声明,并且文档将焦点更改为图像文件夹可能导致此问题。感谢@Loic的帮助! – 2012-07-13 16:11:22

+1

这就是为什么如果你可以避免使用全局变量是好的;) – Loic 2012-07-13 21:29:33

0

首先,尽量避免使用全局变量特别是在使用会话引擎时。

如果你的脚本的目标是简单的导出链接/修改链接/替换链接,为什么你想拥有持久变量?

在我的愚见,我将摆脱发动机,只是做一个普通funbction呼叫

//myscript.jsx 
mySuperFunction(); 
function mySuperFunction() 
{ 
    var doc; 
    if (app.documents.length == 0){ return; } 
    doc = app.activeDocument; 
    //Do all your stuff 
} 

它应该是你所需要的;)

卢瓦克

+0

感谢您的帮助@Loic!但是,我第一次使用正常的函数调用,因为你建议,但变量'imgs'变得不确定,或者由于某种原因“不再存在”... – 2012-07-12 01:42:34

+0

我假设ESTK的主引擎被超载,并且需要'#targetengine'来防止变量变得不确定。然而,同样的问题发生了,所以我认为'#targetengine'会跟踪全局变量......所以**然后**之后,不工作,我只是在try-catch语句的捕获中重新实例化了变量'imgs',它工作正常!我同意 - 我尽量避免全局变量,但http://incom.org/post/89818上的信息似乎表明这是'#targetengine'用于... – 2012-07-12 01:42:51