2 Replies Latest reply on May 22, 2012 8:18 AM by mvtimes

    Process rules after importXML()

    mvtimes

      Hello all,

       

      I have a script which starts out by importing an XML file into a document. Then I have a few rules to run on the content.

       

      The trouble I'm having is that some of the rules don't seem to see the content as it should. They work if I use the menu item "Import XML..." to bring in the content and then run the desired rules via script, but I'm trying to get it to work in one fell swoop.

       

      In a script, is there a way to wait for importXML() to finsh before running the rules processor? I've tried adding a if statement around the importXML() command, but that doesn't seem to work. It seems that there would be a way to wait until importXML() has finished and all the content is on the pages before running the XML rules in a single script, but I can't seem to put my finger on it.

       

      Any help would be greatly appreciated.

       

      Thank you.

       

       

       

      var myDocument = app.documents.item(0);

                 

      if (getXML()) {

          main();   

      }

       

      function getXML() {

          if (app.documents.length != 0) {

                  // First import the XML

                  var myFolder= Folder.selectDialog ("Choose the folder with the XML data");

                  if (myFolder != null){

                      myDocument.importXML(myFolder.fsName + '/data.xml');               

                      return true;

                  }

                  else {

                      return false;

                  }

          }

          else {

              return false;

          }

      }

       

      function main() {

      // Rules

      }

        • 1. Re: Process rules after importXML()
          absqua Level 4

          I do this—import xml then process the document with an xmlRuleProcessor—all the time and have never had the problem you're describing. Does your xml import add pages to the document with Smart Text Reflow? Maybe that's the culprit? What about adding in a myDocument.stories.everyItem().recompose() in between your import and rules? I've rarely encountered these kinds of timing problems scripting InDesign though. Is it possible you don't have the same xmlImportPreferences set in your scripted import as you do when you import via the UI, and that that explains the differences you're seeing?

           

          Jeff

          • 2. Re: Process rules after importXML()
            mvtimes Level 1

            That seems to have done the trick:

             

            if (getXML()) {

                myDocument.stories.everyItem().recompose();

                main();  

            }

             

            I'm even more perplexed now that you've not experienced the issue. I've been wrestling with this one rule for a while now. All the others seemed OK before even without the recompose. All the rule does is move an XML Element from an inline frame to it's own unanchored frame on the same page. I had some trouble  getting the page index of the parentPage of the element. I think the problems I'm having has something to do with this, in that the element isn't ON the page until the import is finished. I don't know.

             

            Hopefully with the line you've supplied, I will be able to proceed.

             

            Thank you, again.