Silicon Publishing Invents the desperationCounter

Silicon Publishing Invents the desperationCounter

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…


Colleagues,

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.
try{
      SuiteTalk.log("Setting file property for placedItem to EPS file for layer " + lName + ".", "DEBUG");
      placedEPS.file = epsFile;
      SuiteTalk.log("Setting file succeeded.", "DEBUG");
}
catch(e:Error){
      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{
                        //Try again from the beginning.
                        newDoc.activate();
                        placedEPS = newDoc.placedItems.add();
                        placedEPS.file = epsFile;
                  }
                  catch(e:Error){
                        //"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;
                              outErrors.push(se);
                              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.

Thanks,

Ole

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

Leave Your Comment

Your Comment*

Your Name*
Your Webpage