SchattenJager SchattenJager - 2 months ago 30x
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');


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

#targetengine "session"

app.addEventListener('afterOpen', function(myEvent) {
if ( !== 'Document') {

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


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
event callback, and that's the reason why it works manually using the same method, but not in the startup script.


I was able to get around the issue by avoiding the event listener altogether.


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) {  

    // open the file  
    var curDoc =;  

    // import the xml  

    // create a new file object  
    var pdfFile = new File(curFile.parent + '/' + + '.pdf');  

    // export to pdf  
    curDoc.exportFile(ExportFormat.PDF_TYPE, pdfFile);  

    // close app