Skip to main content
Participant
July 21, 2016
Answered

Issue with InDesign startup script not loading images from XML import

  • July 21, 2016
  • 3 replies
  • 418 views

I am trying to do an XML import automatically from a startup script when a document is loaded. I am successful in getting most of the content to populate, but images are being ignored. Everything works, including images, when I do a manual 'Import XML' through the UI, or through a manual script.

Below is my manual script:

var myDocument = app.activeDocument;
var xmlFile = File('/c/Full/Path/To/data.xml');

myDocument.importXML(xmlFile);

But the goal is to do it on startup. Below is my startup script:

#targetengine "session"

app.addEventListener('afterOpen', function(myEvent) {
   if (myEvent.target.constructor.name !== 'Document') {
   return;
   }

   var myDocument = myEvent.target;
   var xmlFile = File('/c/Full/Path/To/data.xml');

  myDocument.importXML(xmlFile);
});

Below is the XML tag for the image:

<Image href="file:///C:/Full/Path/To/Image/02.png" />

I'm wondering if there is an issue with the 'afterOpen' event callback, and that's the reason why it works manually using the same method, but not in the startup script.

This topic has been closed for replies.
Correct answer schattenjager

I solved it by avoiding the event listener altogether:

main();

function main () {

    // create a path for a file object

    var curFile = File('/c/Path/To/file.indd);

    var xmlFile = File('/c/Path/To/data.xml');

    // close app if files don't exist

    if (!curFile.exists || !xmlFile.exists) {

        app.quit(SaveOptions.NO);

    }

    // open the file

    var curDoc = app.open(curFile);

    // import the xml

    curDoc.importXML(xmlFile);

    // create a new file object

    var pdfFile = new File(curFile.parent + '/' + curFile.name + '.pdf');

    // export to pdf

    curDoc.exportFile(ExportFormat.PDF_TYPE, pdfFile);

    // close app

    app.quit(SaveOptions.NO);

}

3 replies

schattenjagerAuthorCorrect answer
Participant
July 21, 2016

I solved it by avoiding the event listener altogether:

main();

function main () {

    // create a path for a file object

    var curFile = File('/c/Path/To/file.indd);

    var xmlFile = File('/c/Path/To/data.xml');

    // close app if files don't exist

    if (!curFile.exists || !xmlFile.exists) {

        app.quit(SaveOptions.NO);

    }

    // open the file

    var curDoc = app.open(curFile);

    // import the xml

    curDoc.importXML(xmlFile);

    // create a new file object

    var pdfFile = new File(curFile.parent + '/' + curFile.name + '.pdf');

    // export to pdf

    curDoc.exportFile(ExportFormat.PDF_TYPE, pdfFile);

    // close app

    app.quit(SaveOptions.NO);

}

Legend
July 21, 2016

Sounds like something inside InDesign is not yet up and running when the open is processed.

What happens if you defer the import to an idle task?

Dirk

Community Expert
July 21, 2016

Maybe you could first add something and remove that ( a rectangle perhaps ) to the document, then save it and after saving it import the XML?

Regards,
Uwe