0

#targetengine "myEngineName" 선언은 InDesign에서 전역 변수를 기억하는 데 사용된다는 것을 알고 있습니다. 여기에 대한 정보는     http://incom.org/post/89818입니다. 그런
InDesign CS5 Script : '# targetengine'이 (가) 제대로 작동하지 않는 이유는 무엇입니까?

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

...이나 뭐 어쨌든 : 여전히 글로벌 변수 imgs에 대한 오류를 발생시킵니다로 전역 변수를 기억하기


그러나이 여전히 충분하지 않았다. 그것은 내 코드의 특정 라인을 좋아하지 않으며, 글로벌 변수 imgs이 어떻게 인스턴스화되었는지 잊고있는 것처럼 보입니다.

그래서 나는 try-catch 문을 구현, 변수 imgs을 reinstatiated이 했다이 문제를 해결할 수 있지만이 가정처럼, 왜 #targetengine "myEngineName" 문제가 해결되지 않습니다 ... 캐치의 반복자를 감소 ? -

    http://forums.adobe.com/thread/748419

편집 :

#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 


이 내가이 오류에 대한 찾을 수 밖에있는 유일한 정보는 : 여기

내 코드입니다 나는 그 문제가 확실히 있다고 확신한다. 함수 replaceWithNewImage과 관련이 없으므로이 오류가이 함수없이 발생하지 않았으므로 try-catch 문이 필요하지 않습니다.

답변

4

읽기 당신의 코드, 나는 정말로 문제가 될 수있는 것을 본다. 문서에 대한 참조를 활성 문서로 설정합니다. 그러나이 참조는 세션 전체에 남아 있습니다. 사실은 다른 문서로 전환하거나 문서를 닫으면 참조가 손실됩니다. 이것은 img가 언젠가는 정의되지 않은 이유를 설명해 줄 수 있지만, 오류가 발생해야한다고 생각합니다. 변수를 함수 범위로 묶으십시오. 모든 것이 괜찮을 것이라고 보증합니다.)

+0

흥미 롭다. 그래서 대신 'app.activeDocument'를 사용하여'app.documents [0]'을 시도했는데, 이제이 오류는 *** 발생하지 않습니다. 그래서 변수가'app.activeDocument' 때문에 정의되지 않은 것처럼 보입니다. 그리고 이미지 폴더에 초점을 변경하는 문서가이 문제의 원인 일 수 있다고 가정합니다. @Loic 도움에 감사드립니다! –

+1

글로벌 변수를 사용하지 않는 것이 좋습니다. – Loic

0

우선 글로벌 변수는 가능한 한 많이 사용하지 마십시오. 특히 세션 엔진을 사용하는 동안.

스크립트의 목적이 간단한 내보내기 링크/링크 수정/링크 교체 인 경우 왜 영구 변수를 사용 하시겠습니까? 내 소견에서

, 나는 엔진을 제거하고

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

그것은 당신이 필요로하는 모든해야 일반 funbction 호출을 바로 얻을 것이다 이상)

루이

+0

감사합니다. @Loic! 그러나, 나는 당신이 제안하는 것처럼 처음으로 정규 함수 호출을 사용했다. 그러나 변수'imgs'는 어떤 이유로 정의되지 않거나 "더 이상 존재하지 않는다"... –

+0

ESTK의 주 엔진이 오버로드되고 있다고 가정하고 있었다. 변수가 정의되지 않게되는 것을 막기 위해'# targetengine '이 필요했습니다. 그러나 동일한 문제가 발생했기 때문에'# targetengine '이 전역 변수를 추적 할 것이라고 생각했습니다 ...따라서 ** 작동하지 않는 ** 후에 try-catch 구문의 catch에서 변수'imgs'를 다시 인스턴스화하고 작동합니다! 그리고 나는 동의한다. 나는 전역 변수를 피하려고 노력한다. 그러나 http://incom.org/post/89818의 정보는 그것이'# targetengine'이 사용되는 것을 제안하는 것으로 보인다. –