InDesign CS5 Script: Why is `#targetengine` not working correctly?


I understand that the declaration #targetengine "myEngineName" would be used for InDesign to remember global variables (found information on this here:  <a href="http://incom.org/post/89818" rel="nofollow">http://incom.org/post/89818</a>). <br />

<hr /> However, this was still not enough for it to remember global variables as it still throws an error regarding the global variable imgs: <br /><blockquote>

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


...or something like that anyway. It doesn't like that particular line in my code, and seems to be forgetting what the global variable imgs was instantiated as. <br />

<hr /> So I implemented a try-catch statement, and reinstatiated the variable imgs and decremented the iterator in the catch... Though this <strong>did</strong> solve the problem, why doesn't #targetengine "myEngineName" solve the problem like it is supposed to? Here is my code: <br />#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

<br /> This is the only information out there I could find regarding this error:  <a href="http://forums.adobe.com/thread/748419" rel="nofollow">http://forums.adobe.com/thread/748419</a> <strong>EDIT</strong> -- I'm pretty sure the problem has something to do with the function replaceWithNewImage, because this error did not occur without this function, and there was then no need for the try-catch statement...


Reading over your code, I see something that may be really problematic. You set the reference to the document to the active document. But this reference remains all over the session. Fact is if you switch to another document or close the document, then the reference is lost. This may explain why imgs can be undefined at some point although I think it should raise an error instead. Do wrap your variables in a function scope and I warranty you everything will be fine ;)


First of all, just avoid global variables as much as you can and especially while using session engines.

If the aim of your script is a simple export links/modify links/replace links, why do you want to have persistent variables ?

In my humble opinion, I will get rid of the engine and just do a regular funbction call

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

It should be all you need ;)



  • Couldn`t join two files with one key via Cascading
  • when I use git clone to get a git from the remote, the git_index_entrycount return 0?
  • C#: Insert and indent bullet points at bookmark in word document using Office Interop libraries
  • Nested Bulleted lists in Novacode docx
  • in r combine a list of lists into one list
  • Appending strings from json object on a condition together efficiently?
  • DOMXML, PHP4 => PHP5
  • textIndicatorPrecision in dojox.dgauges using dojo
  • Always require certain dependencies in RequireJS
  • Generate a unique string based on a pair of strings
  • Why is django manage.py syncdb failing to create new columns on my development server?
  • What is the reason that Policy.getPolicy() is considered as it will retain a static reference to the
  • Want to understand iframe breakout code
  • AlertDialog style when using setView()
  • ImportError: cannot import name Pubnub
  • HTML5 video only works in IE. The other browsers shows the black screen
  • How to split circle in to the sectors in google maps?
  • Django model inheritance, filtering models
  • AndEngine Applying Transparancy to AndEngine View
  • How to attach a node.js readable stream to a Sendgrid email?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Android fill_parent issue
  • JFileChooser in front of fullscreen Swing application
  • How reduce the height of an mschart by breaking up the y-axis
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Running a C# exe file
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • JSON with duplicate key names losing information when parsed
  • PHP: When would you need the self:: keyword?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Acquiring multiple attributes from .xml file in c#
  • Free memory of cv::Mat loaded using FileStorage API
  • How do I configure my settings file to work with unit tests?
  • Android Google Maps API OnLocationChanged only called once
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • reshape alternating columns in less time and using less memory
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML
  • How can I use threading to 'tick' a timer to be accessed by other threads?