Silicon Publishing Invents the desperationCounter

by | May 9, 2012 | Adobe, Creative Suite, Illustrator, scripting

Anybody who works with automating Adobe Illustrator long enough will find some level of flakiness in the behavior of this application. It is very old, and doesn’t enjoy the same level of support for automation as does InDesign, a much more modern application. I got this email this morning, and it seems we are one step closer to taming the Illustrator beast…


To place a graphic in Illustrator, you create a new placedItem using placedItems.add(), then you set the file property of the placedItem to a file reference for the graphic you want to import. That’s okay. But when you’re creating/exporting/closing lots of files, Illustrator sometimes loses track of what’s going on. In times like these, Illustrator will create a placedItem that doesn’t really exist. When that happens, trying to set the file property will fail, but it won’t throw an error until later—because the placedItem.file property is null. In fact, the whole placedItem is garbage—but Illustrator doesn’t *know* that it’s junk until it tries to do something with it. If you set the placedItem.file property to an existing file and the property remains null, you know you’ve got a bad object.

So…be encouraging. I find that asking Illustrator to do something a few times usually produces the result I want. Something like this (I can probably go through and remove a few of the extra steps here, but, however inelegant, this does work):

//Place the EPS file by setting the file property of the placedItem to the EPS.
 SuiteTalk.log("Setting file property for placedItem to EPS file for layer " + lName + ".", "DEBUG");
 placedEPS.file = epsFile;
 SuiteTalk.log("Setting file succeeded.", "DEBUG");
 SuiteTalk.log("Error creating placed EPS for layer " + lName + ".", "DEBUG");
 SuiteTalk.log("File exists? " + epsFile.exists + ".", "DEBUG");
 SuiteTalk.log("File URL: " + epsFile.url + ".", "DEBUG");
 //Try again.
 if(epsFile.exists == true){
 var epsFilePlaced:Boolean = false;
 var desperationCounter:int = 0;
 while(epsFilePlaced == false){
 //Try again from the beginning.
 placedEPS = newDoc.placedItems.add();
 placedEPS.file = epsFile;
 //"SuiteTalk" is for writing to a log file
 SuiteTalk.log("Retry failed for setting file of placedItem for layer " + lName + ".", "DEBUG");
 if(desperationCounter >= 100){
 se = new SuiteTalkError( "Error creating placedItem for EPS: " + e.message, "103");
 se.other = lName;
 SuiteTalk.log("Could not create placedItem for EPS for layer " + lName + ".", "DEBUG");
 epsFilePlaced = true;
 desperationCounter ++;
 if(placedEPS.file != null){
 epsFilePlaced = true;

So far, it’s never run through the desperationCounter, and it’s always–eventually—imported the file.

And now, a round of applause for Jonathan Brown’s “isValid” property on InDesign objects.



LOL, we can’t always be elegant, especially with older applications.

Let's discuss your product!

Have a project in mind? Contact us today to explore how we can make it a reality.

Let's get started.

Our Products

Silicon Designer:
Edit InDesign Documents Online.

Silicon Paginator:
Data-Driven Document Publishing.

Silicon Connector:
Digital Asset Management Meets Creative Cloud.

Adobe InDesign Server:
Adobe InDesign Server Solutions.

Print Reddy:
Bringing High-Quality Print to Adobe Express.

Box Express Connector:
Access the Box Content Cloud from Adobe Express.

Share This