SchattenJager SchattenJager - 4 months ago 34
Javascript Question

Issue with InDesign startup script not loading images from XML import

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.

Answer

I was able to get around the issue 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);  

}