19 Replies Latest reply on Jun 29, 2014 6:35 AM by salvi_

    Undefined Variables

    salvi_

      I have the following piece of script, used to import xml files from a folder to a template, but I can't get it to work.

      var myTemplate = File("/E/template.indd");
      var MyFolderWithFiles = Folder.selectDialog ("Choose a folder");
      var myXMLDocs = MyFolderWithFiles.getFiles(".xml");
      
      for(var i = 0; i < myXMLDocs.length; i++) {
          //open template
          var newDoc = app.open(myTemplate),
          myDoc = app.activeDocument;
      
          //import xml
          newDoc.importXML(myXMLDocs[i]);
           saveFiles();
      }
      
      function saveFiles() {
          var newFileName = MyFolderWithFiles.absoluteURI + "/Outputs/" + myXMLDocs[i].name.replace(/xml/,"indd");
          newDoc.save(new File(newFileName));

       

      }

      I keep getting the message (in js console) "Error: myTemplate is undefined". Why is this happening? If I alert myTemplate, I get "/E/template.indd".

        • 1. Re: Undefined Variables
          BEGINNER_X Level 3

          Hi Salvi,

           

          May the below code helpful for you.

           

          You need to edit as per your requirement:

           

           

          var myFolderInd = Folder.selectDialog();
           
          var myFile = myFolderInd.getFiles("*indt");
           
          app.open(myFile)
           
          var myDoc = app.activeDocument;
           
          var myDocName = myDoc.name;
           
          var mySaveFile = app.activeDocument.save(myFolderInd.fsName + "/" + myDocName.split(".indt").join(".indd"));
           
          var myFolderDoc = Folder.selectDialog();
          var myFile1 = myFolderDoc.getFiles("*doc");
           
          mySaveFile.pages[0].textFrames[0].place(File(myFile1))
          //~ mySaveFile.place(File(myFile1))
          
          //Please edit the below line
          
          //mySaveFile.pages[0].textFrames[0].importXML(File(myFile1)) 
           
          app.activeDocument.textPreferences.smartTextReflow = true;
              
          alert("Process Completed")
          
          

           

          Thanks

          Siraj

          • 2. Re: Undefined Variables
            Ronald63 Level 4

            Hi,

             

            perhaps comma

            var newDoc = app.open(myTemplate),
            • 3. Re: Undefined Variables
              salvi_ Level 1

              I'm new to InDesign scripting, so it might be that, but your code didn't help me locate and solve my problem

               

              Also, comma is not an issue (I tried it without it as well).

              • 4. Re: Undefined Variables
                Larry G. Schneider Adobe Community Professional & MVP

                That comma needs to be a semi-colon (;).

                • 5. Re: Undefined Variables
                  salvi_ Level 1

                  I did change it to semi-colon and I'm still getting the same error.

                  • 6. Re: Undefined Variables
                    Larry G. Schneider Adobe Community Professional & MVP

                    How about a dot between the * and indt in your secong line?

                    • 7. Re: Undefined Variables
                      Kai Rübsamen Adobe Community Professional

                      salvi, your alert() says nothing about if your file really exists or if your input is correct! You can check this with

                       

                       

                      myTemplate.exists;
                      

                       

                      You see the full Path of your document by trying the following:

                       

                       

                      var myTemplate = File.openDialog("Choose a file");
                      var fPath = myTemplate.fullName;
                      alert(fPath);
                      
                      • 8. Re: Undefined Variables
                        salvi_ Level 1

                        I moved the lines

                        var newDoc = app.open(myTemplate);
                        var myDoc = app.activeDocument;

                        out of the for loop since I want the xml files to be imported in the same instance of myTemplate. It opens myTemplate, but nothing gets imported.

                        • 9. Re: Undefined Variables
                          Mary Posner Level 3

                          I'm just learning JavaScript myself, but my guess would be that you have to move your app.open(myTemplate) line to before your for loop. With it inside the loop, you're asking it to open the template as many times as you have XML files.

                           

                          ETA: Beat me to it!

                          • 10. Re: Undefined Variables
                            Mary Posner Level 3

                            I think some of your newDoc references need to be changed to myDoc, the active document.

                            • 11. Re: Undefined Variables
                              Marc Autret Level 4

                              Hi salvi_

                               

                              Sorry I don't see where the "Error: myTemplate is undefined" message comes from considering your original code.

                               

                              Anyway, the interesting side of your project is in managing copies of the current document during the process. Here is my first approach—including various comments for newbies:

                               

                               

                              // YOUR SETTINGS
                              // ---
                              const MY_TEMPLATE_PATH = "/E/template.indd";
                              const OUTPUT_FOLDER_NAME = "Outputs";
                              
                              // THE WHOLE PROCESS
                              // ---
                              (function()
                              {
                                  // Best is to declare your variables at the beginning of the block
                                  // ---
                                  var myTemplateFile,          // the template File object
                                      myFolderWithFiles,       // folder selected by the user
                                      myXMLDocs,               // array of XML files
                                      outPath,                 // output folder path
                                      myTemplate,              // the template Document
                                      i,                       // loop index
                                      xml,                     // the current XML File
                                      outFile;                 // the current exported doc file
                                      
                                  // Make sure the INDD template file exists
                                  // ---
                                  if( !(myTemplateFile=File(MY_TEMPLATE_PATH)).exists )
                                      {
                                      alert("Template not found!");
                                      return;
                                      }
                              
                                  // Let the user choose a XML source folder
                                  // ---
                                  if( !(myFolderWithFiles=Folder.selectDialog ("Choose a source folder for XMLs")) )
                                      {
                                      // Cancelled by the user
                                      return;
                                      }
                              
                                  // Collect the XML files (non-empty array required to continue)
                                  // ---
                                  myXMLDocs = myFolderWithFiles.getFiles("*.xml");
                                  if( !myXMLDocs || !myXMLDocs.length )
                                      {
                                      alert("No XML file found!");
                                      return;
                                      }
                              
                                  // Format the output folder path, create it if needed
                                  // ---
                                  outPath = myFolderWithFiles.absoluteURI + '/' + OUTPUT_FOLDER_NAME;
                                  if( !Folder(outPath).exists && !(new Folder(outPath)).create() )
                                      {
                                      alert("Unable to create the output folder!");
                                      return;
                                      }
                              
                                  // Open the template once
                                  // Note: working on a copy prevents from modifying the
                                  // original. Just a good practice, not required here.
                                  // ---
                                  myTemplate = app.open(myTemplateFile, true, OpenOptions.OPEN_COPY);
                                  if( !myTemplate.isValid )
                                      {
                                      alert("Unable to open the template!");
                                      return;
                                      }
                                  
                                  // Loop in myXMLDocs
                                  // ---
                                  for( i=myXMLDocs.length ; i--  ; )
                                      {
                                      // Get the XML file
                                      // ---
                                      xml = myXMLDocs[i];
                                      
                                      // Import (or re-import!) xml into myTemplate
                                      // ---
                                      myTemplate.importXML(xml);
                                      
                                      // Make sure that the output file doesn't exist yet
                                      // ---
                                      outFile = File(outPath + '/' + xml.name.replace(/xml$/,'indd'));
                                      if( outFile.exists )
                                          {
                                          alert( "The file " + outFile.name + " already exists!" );
                                          break;
                                          }
                              
                                      // Save a COPY of myTemplate in its current state
                                      // ---
                                      myTemplate.saveACopy(outFile);
                                      }
                                  
                                  // Finally, close the template without saving
                                  // ---
                                  myTemplate.close(SaveOptions.NO);
                              
                              })();
                              

                               

                              Hope that may help.

                               

                              @+

                              Marc

                              • 12. Re: Undefined Variables
                                salvi_ Level 1

                                Thanks for the code Marc! The script you gave me is running without an error, however, it creates 5 indd files (based on my template) and saves them in the Output folder. I need one indd file. Plus, when I open those files that it creates, there is no data in them (my guess is that the xml files are not imported).

                                The output of the script is "Execution finished. Result: undefined" (if that helps).

                                • 13. Re: Undefined Variables
                                  Marc Autret Level 4

                                  Ah, so you mean that XML data are gradually injected into the same document (the template). That is, each XML file only describes a part of the whole data structure (?) Sorry I did not understand your process…

                                   

                                  Well, in that case you probably just have to move the outFilesaveACopy stuff out of the loop. But a question remains: what is the name of the single INDD output file? Your original saveFiles() function seemed to indicate that multiple files were to be created.

                                   

                                  As far as I understand your algorithm is:

                                   

                                  myTemplate->import(xml_1);
                                  myTemplate->import(xml_2);
                                  // etc.
                                  myTemplate->import(xml_N);
                                  
                                  // then
                                  myTemplate->saveACopy(/* What Is The Indd File Name Here */)
                                  

                                   

                                  Also, it might be of some importance to respect the order in which xml data are populated (?)

                                   

                                  > my guess is that the xml files are not imported

                                   

                                  I tested my snippet above with sample files and data were properly imported. Are you sure that your template structure actually matches the XML data you are trying to inject? What if you manually import those data using the GUI?

                                   

                                  @+

                                  Marc

                                  • 14. Re: Undefined Variables
                                    Kai Rübsamen Adobe Community Professional

                                    I tested my snippet above with sample files and data were properly imported.

                                    +1

                                     

                                    @ Marc: Thanks for the template. Really helpful to all newbies .

                                    • 15. Re: Undefined Variables
                                      salvi_ Level 1

                                      > Are you sure that your template structure actually matches the XML data you are trying to inject?

                                       

                                      I followed the instrunctions I found on a video to create myTemplate, but maybe I did something wrong. Can you point me to the right direction to structure my template properly please?

                                       

                                      And yes! My process is what you described. I have some xml files and I want to select the folder I have saved them, create a new indd document based on a template and import ALL the xml files in the same indd file. Does it matter if the data in the xml files are in greek?

                                      • 16. Re: Undefined Variables
                                        Marc Autret Level 4

                                        salvi_ wrote:

                                         

                                        Can you point me to the right direction to structure my template properly please?

                                         

                                        Here are the basics:

                                        http://tv.adobe.com/watch/instant-indesign/creating-a-basic-xmlready-template/

                                         

                                        And don't miss the online help on importing XML:

                                        http://help.adobe.com/en_US/indesign/cs/using/WS96346460-C5B6-42c7-88F5-086287793984a.html #WSB3CF54D6-9843-4486-96CF-A224B6566401a

                                         

                                        salvi_ wrote:

                                         

                                        Does it matter if the data in the xml files are in greek?

                                         

                                        I don't think so, provided that your xml flow is UTF8 encoded.

                                         

                                        [OT]

                                        Don't forget to click the "helpful" button for those contributors who helped you. It's a highly recommanded strategy to have future questions noticed and answered ;-)

                                        [/OT]

                                        • 17. Re: Undefined Variables
                                          salvi_ Level 1

                                          I will study the resources you gave me! I'll be back with more questions if I have any!

                                           

                                          Again, thanks for your help!

                                          • 18. Re: Undefined Variables
                                            Trevorׅ Adobe Community Professional

                                            In the meantime it won't hurt you if you mark Marc's answer as correct

                                            Greenshot_2014-04-03_01-48-49.png

                                            • 19. Re: Undefined Variables
                                              salvi_ Level 1

                                              The import part seems to be working fine now, although only the 1st xml is imported (I am testing with 2 xml files, with the same structure). And I did move the outFilesaveACopy stuff out of the loop.