21 Replies Latest reply on Oct 13, 2009 3:22 AM by Loic.Aigon

    How to edit xml link?

    poxd Level 1

      First a few words about my context:

       

      I received a layout made on a CS3 version of InDesign. Now I am trying to create a procedure to automate the production of several pdf based on this layout. Manually the process would be to open the layout; edit the xml link with each xml source and create a corresponding pdf...

       

      First issue:

      In CS4 when I want to manually edit the xml link, the opened dialog prevent me (xml files are in gray) to select xml files, is it a well-known issue? How can I avoid it?

       

      Second question:

      How to edit the xml link from script?

       

      Last question - applescript question - :

      How to skip application dialogs? Like "This document miss some links, would you continue?"

       

      If you'd be able to help me, thanks in advance!

        • 1. Re: How to edit xml link?
          Loic.Aigon Adobe Community Professional

          Hi,

           

          Depending on your needs, you may choose to

           

          Manually :

          1. Label an element of the layout (ex: a graphic frame labeled "Image");

          2. Install xmljs library which is very cumfy to deal with XML without loading xml into Indesign.

           

          Via scripting :

          3. Process through the xml with the library and get back the datas your need (a picture file path for example)

          4. Open the indesign document and proceed like this :

          - Duplicates page one

          - Proceed to changes on this duplicated page (graphic changes)

          - Set the pdfExportPreferences.pageRange to the page name (as String);

          - Export the page your document as PDF

          - Continue

          5. Either save or not the document, close it.

           

          xmljs : http://xmljs.sourceforge.net/

           

           

          But that's not applescript...

           

          A+ Loic

          • 2. Re: How to edit xml link?
            poxd Level 1

            Hi and thanks for your answer.

             

            My issue is not about xml but moreover InDesign and its integration (or my misuse) with it. The layout I received is intended to be used against several xml files I generate on my side. What I have to do is to take the layout and run the following process for every xml file I generated:

             

            1. Import the xml file into the layout (I'd like to edit xml link indeed instead of an import xml command as the import does'nt give me the same results sometime)

            2. Export as pdf

            • 3. Re: How to edit xml link?
              Loic.Aigon Adobe Community Professional

              Hi I was thinking to something like this :

              //You need to download and place the xmljs library some place
              //http://xmljs.sourceforge.net/index.html
              
              //You need to adapt paths to your own system and folders
              
              #include "/m/scripting/xml/xml_for_script-3.1/jsXMLParser/xmldom.js"
              
              //Where your xml file is located
              var myFile=File("/m/scripting/xml/test/datas.xml");
              myFile.open('r');
              var myXMLString = String(myFile.read());
              
              //We make an XML object through xmljs library
              var objDom = new XMLDoc(myXMLString, undefined) ;
              
              var domTree = objDom.docNode;
              var myImage = domTree.getElements("image");
              
              //with false as a property, we ask Indesign to open the doc without showing us.
              var doc= app.open(File('/m/scripting/xml/test/testXmlJs.indd'), false);
              
              for(i=0; i<myImage.length; i++)
              {
                  //For each nodes "image" in the xml, we duplicate the page and change the content of the labeled frame
                  var myPage = doc.pages[0].duplicate(LocationOptions.AT_END);
                  var myFrame = myPage.pageItems.item("image");
                  myFrame.place(File(myImage[i].getAttribute("href").replace('file:///','')));
                  
                  //we set options for the pdf export.
                  app.pdfExportPreferences.pageRange = myPage.name;
                  app.pdfExportPreferences.viewPDF=false; //set this to true if you want to open the PDF when created
                  //PDF are created in this folder : "/m/scripting/xml/test/pdfXml"+i+".pdf"
                  doc.exportFile(ExportFormat.pdfType, File("/m/scripting/xml/test/pdfXml"+(i+1)+".pdf"), undefined, undefined, undefined, undefined);
              }
              
              //We close the doc without saving
              doc.close(SaveOptions.NO);
              

              It's very basic and doesn't prevent misuse but functional if you respect the rules. Maybe it helps.

              I join the indesign and xml I used with the script. And the generated PDF.

              • 4. Re: How to edit xml link?
                poxd Level 1

                I may ,isunderstand what you said but as far as I understand, in your example you take a data in the xml and then update a layout and export. I indirectly want to do the same thing but using the builtin functionality of InDesign. For the matching between the xml and the layout I mean (using Links). I don't want to manage the mapping between the xml and the indd...

                 

                I don't know if I am clear...

                • 5. Re: How to edit xml link?
                  Loic.Aigon Adobe Community Professional

                  If you are confident about the xml files structure, you can set a folder as source, getFiles('*.xml') from this folder.

                  Then for each files, import it in the document, place the root in the main frame, export every page, close the doc and continue;

                  Is that more like you envision things ?

                  Loic

                  • 6. Re: How to edit xml link?
                    poxd Level 1

                    Exactly that's more that! Now the only issue is that when I use the import feature I end sometime with results I don't want... Although when I use the "edit xml link" functionality (possible only in InDesign CS3, see the 1st post) I always get the result I want.

                     

                    How can I use scripting to do that?

                     

                    Thanks for your patience

                    • 7. Re: How to edit xml link?
                      Adobe-InDesign CS4 Level 1

                      Dear Loic_aigon

                       

                          I have read and tried your Scripts....

                       

                      Many thaks for put in this forum.

                       

                      But I need one help and doubt also.

                       

                      I have attached the XML File,

                      Here I can't worked with "Image" tag.

                       

                      For example :

                       

                        <Chapter>

                         <Section>

                         <para>

                           <Figure>

                              <Image>

                             </Image>

                          </Figure>

                      </para>

                      </Section>

                      </Chapter>

                       

                      I can't find the "Image" Tag in the above method. your script is return "Chapter" - Root Element only.

                       

                      Please give me the solutions [this is my humble request].

                       

                      So Please kindly help me...

                       

                      Thanks & Regards

                      T.R.Harihara SudhaN

                      • 8. Re: How to edit xml link?
                        Loic.Aigon Adobe Community Professional

                        Hi,

                         

                        You may have a problem as your image tag is named "Image" and the js command looks for "image". You va a capitalization error.

                        Either switch your tags names to "image" or simplier, try this

                        var myImage = domTree.getElements("Image");

                        instead of

                        var myImage = domTree.getElements("image");

                        Hope it helps

                        Loic

                        • 9. Re: How to edit xml link?
                          Adobe-InDesign CS4 Level 1

                          Dear Loic_aigon

                           

                             Many thanks!

                           

                          But I used what you said!.

                           

                          But Still I'm facing the same problem [ not find : "image"]

                           

                          Here the below tag I used..

                           

                           

                          <Figure id="f0020"><label>Figure 2 </label><caption>Dummy Figure </caption>
                          <image href="C:/Program %20Files/VUG/clouds.jpg"></image></Figure>

                           

                          So please kindly see the attached xml file, and please send me the solutions.

                           

                          Please this is my humble request.

                           

                           

                           

                          Thanks & Regards

                          T.R.Harihara SudhaN

                          • 10. Re: How to edit xml link?
                            Adobe-InDesign CS4 Level 1

                            Dear Loic_aigon

                             

                               Many thanks!

                             

                            But I used what you said!.

                             

                            But Still I'm facing the same problem [ not find : "image"]

                             

                            Here the below tag I used..

                             

                             

                            <Figure id="f0020"><label>Figure 2 </label><caption>Dummy Figure </caption>
                            <image href="C:/Program %20Files/VUG/clouds.jpg"></image></Figure>

                             

                             

                            Program :

                             

                            //============== Start =====================//

                            #include "E:/HariharaSudhan/XMDOM_TEST/xml_for_script-3.1/jsXMLParser/xmldom.js";

                            var myFile = File("E:/HariharaSudhan/XMDOM_TEST/xml/test.xml");
                            myFile.open("r");

                            var myXMLString = String(myFile.read());

                            var objDom = new XMLDoc(myXMLString, undefined);

                            var domTree = objDom.docNode;

                            $.writeln(domTree);
                            var myImage = domTree.getElements("image");


                            var myDoc = app.open(File("E:/HariharaSudhan/XMDOM_TEST/Indd/test.indd"), true);

                            $.writeln(myImage);

                            for(var i=0; i<myImage.length; i++)
                            {
                            $.writeln( myImage[i].name);
                            myImage[i].getAttribute("href").replace('file:///','');
                              var myPage = doc.pages[0].duplicate(LocationOptions.AT_END);
                              var myFrame = myPage.pageItems.item("image");
                              myFrame.place(File(myImage[i].getAttribute("href").replace('file:///','')));
                              app.pdfExportPreferences.pageRange = myPage.name;
                              app.pdfExportPreferences.viewPDF  = false;
                              myDoc.exportFile(ExportFormat.pdfType, File("E:/HariharaSudhan/XMDOM_TEST/PDF/" +(i+ 1)+".pdf"), undefined, undefined, undefined, undefined);
                              }
                             
                            myDoc.close(SaveOptions.NO);

                             

                            //===================== End ============================//

                             

                            So please kindly see the attached xml file, and please send me the solutions.

                             

                            Please this is my humble request.

                             

                             

                             

                            Thanks & Regards

                            T.R.Harihara SudhaN

                            • 12. Re: How to edit xml link?
                              Loic.Aigon Adobe Community Professional

                              Hi,

                               

                              Are you sure about your datas, you tell me your xml structure looks like :

                              <Chapter>
                                   <Section>
                                        <para>
                                             <Figure>
                                                  <Image></image>
                                             </Figure>
                                        </para>
                                   </Section>
                              </Chapter>
                              

                              But in the xml you gave me your datas are like this...

                              <Chapter>
                                   <Section>
                                        <para></para>
                                        <Figure>
                                             ...
                                             <Image></image>
                                        </Figure>
                                   </Section>
                              </Chapter>
                              

                               

                              anyway trying this

                              var myFile = File("E:/HariharaSudhan/XMDOM_TEST/xml/test.xml");
                              myFile.open("r");
                              var myXMLString = String(myFile.read());
                              var objDom = new XMLDoc(myXMLString, undefined);
                              var domTree = objDom.docNode;
                              var myImage = domTree.getElements("image");
                              

                              will cause an error as you faced it.


                              it will works if image was an immediate tag of Chapter.

                               

                               

                              So accordingly to the xml you gave, what you have to do is to proceed through each nodes that can count image tags in it.

                               

                              var myFile = File("E:/HariharaSudhan/XMDOM_TEST/xml/test.xml");
                              myFile.open("r");
                              var myXMLString = String(myFile.read());
                              var objDom = new XMLDoc(myXMLString, undefined);
                              var domTree = objDom.docNode;
                              var section = domTree.getElements("section")[0];
                              var myParas = section.getElements("Figure");
                              for(i=0, i<myParas.length; i++)
                              {
                                   var myImage = myParas[i].getElements("image");
                                   //Etc.
                              }
                              
                              

                               


                              Here you are

                              Loic

                              • 13. Re: How to edit xml link?
                                Loic.Aigon Adobe Community Professional

                                Ok, sorry for digression,

                                 

                                I can't get what you mean by being prevented from manually editing xml links with Indesign. I tried and I can do it whatever I use CS3 or CS4.

                                But maybe, i didn't seize something, don't hesitate to tell more about your process.

                                 

                                Regarding scripting xml links replacing,

                                 

                                You may use something like :

                                //my_old_xml will be the name of the link that appears in the links palette

                                //Replace '/m/scripting/xml/test/my_new.xml' by the path to you rnew xml file;

                                app.activeDocument.links.item("my_old_xml").relink(File('/m/scripting/xml/test/my_new.xml' ));
                                app.activeDocument.links.item("datas4.xml").update();

                                 

                                you can of course process through a bunch of xml files with Folder.getFiles('*.xml');

                                 

                                After that you have to place the root xmlElement in the main textFrame to get your pages generated.

                                Then loop for pages length and set pdfExportPreferences to pages[i] and export your file.

                                 

                                Hope it helps.

                                 

                                Loic

                                1 person found this helpful
                                • 14. Re: How to edit xml link?
                                  poxd Level 1

                                  Thank you Loic for your answer.

                                   

                                  > I can't get what you mean by being prevented from manually editing xml links with Indesign. I tried and I can do it whatever I use CS3 or CS4.

                                   

                                  To me with CS4 the dialog makes xml files gray and I cannot change the link to an another xml file... Do you have any idea why? Having a layout done on CS3 doesn't matter?

                                   

                                  I will try your snippet this weekend but are you aware of a similar code under applescript instead?

                                   

                                  Thanks a lot for your help,

                                   

                                  Valentin

                                  • 15. Re: How to edit xml link?
                                    Loic.Aigon Adobe Community Professional

                                    Ok, I get it.

                                     

                                    If you asked for a xml file to be imported as a link, the file will appear int your links palette. You know that.

                                    If you want to switch that link for another xml file in the UI, you cannot do it via the usual place command even if the link is selected in the links palette.

                                    As you have noticed, xml files are grayed. But if you use the relink button of the links palette, you can have access to any xml file you want.

                                    The fact is that an xml link is not a standard link as an image. You don't place (Ctrl/command D) an xml file like an image, you import it onto the document.

                                    Making it a link is just helpful for modifying your file externally and let Indesign reloads it if changed.

                                     

                                    So How you do xml links replacement via scripting ? You need to use a relink method like I offered to do.

                                     

                                    I am not skilled at all with applescript. There is a lot of very good applescripters around here. You may refer to ADobe scripts examples, it's quite sure there is a relink example in it.

                                     

                                    Hope it helps.

                                     

                                    Loic

                                    1 person found this helpful
                                    • 16. Re: How to edit xml link?
                                      poxd Level 1

                                      > I am not skilled at all with applescript. There is a lot of very good applescripters around here. You may refer to ADobe scripts examples, it's quite sure there is a relink example in it.

                                       

                                      So I will try your javascript I think it's possible to call javascripts from applescript. I hope actually because there is no relink example with InDesign CS4.

                                       

                                      Thanks again

                                      • 17. Re: How to edit xml link?
                                        Loic.Aigon Adobe Community Professional

                                        There is a piece of code found on the net :

                                         

                                        tell document 1 of application "Adobe InDesign CS3"
                                        relink thisItemLink to someFileReference
                                        update thisItemLink
                                        end
                                        • 18. Re: How to edit xml link?
                                          poxd Level 1

                                          Thanks a lot!

                                           

                                          Where have you found this snippet?

                                           

                                          Valentin

                                          • 19. Re: How to edit xml link?
                                            Loic.Aigon Adobe Community Professional

                                            Just googled ! ;-)

                                            • 20. Re: How to edit xml link?
                                              Adobe-InDesign CS4 Level 1

                                              Dear Loic_aigon

                                               

                                                  Many thanks, for given the source code. I really appreciate you!.

                                              I tried your code, but still i'm facing the problem. I don't what i made a mistake.

                                              Here I pasted the below coding was I done.

                                               

                                              //====================== Start ============================//

                                              #include "E:/HariharaSudhan/XMDOM_TEST/xml_for_script-3.1/jsXMLParser/xmldom.js";


                                              var myFile = File("E:/HariharaSudhan/XMDOM_TEST/xml/test.xml");
                                              myFile.open("r");
                                              var myXMLString = String(myFile.read());
                                              var objDom = new XMLDoc(myXMLString, undefined);
                                              var domTree = objDom.docNode;
                                              var section = domTree.getElements("section")[0];
                                              var myParas = section.getElements("Figure");

                                              var doc = app.open(File("E:/HariharaSudhan/XMDOM_TEST/Indd/test.indd"), true);

                                              for(i=0; i<myParas.length; i++)
                                              {
                                                   var myImage = myParas[i].getElements("image");
                                                    var myPage = doc.pages[0].duplicate(LocationOptions.AT_END);
                                                var myFrame = myPage.pageItems.item("image");
                                                myFrame.place(File(myImage.getAttribute("href").replace('file:///','')));
                                                app.pdfExportPreferences.pageRange = myPage.name;
                                                app.pdfExportPreferences.viewPDF  = false;
                                                myDoc.exportFile(ExportFormat.pdfType, File("E:/HariharaSudhan/XMDOM_TEST/PDF/" +(i+ 1)+".pdf"), undefined, undefined, undefined, undefined);
                                              }

                                              //========================End of the Coding ================================//

                                               

                                              Error Message showing in : myFrame.place(File(myImage.getAttribute("href").replace('file:///',''))); [myImage.getAttribute is not a Object]

                                               

                                              I used to run different way, but Still I'm getting the Error.

                                               

                                              Please kindly help me and give me the corrections, I'm waiting your responce.

                                               

                                              Thanks & Regards

                                              T.R.Harihara SudhaN

                                              • 21. Re: How to edit xml link?
                                                Loic.Aigon Adobe Community Professional

                                                Hi,

                                                 

                                                I would guess some of your paragraph nodes don't include an image tag.We have to check if the image tag element exists.

                                                 

                                                Try this :

                                                 

                                                 

                                                for(i=0; i<myParas.length; i++)
                                                {
                                                     if(!myParas[i].getElements("image"))
                                                     {
                                                            var myImage = myParas[i].getElements("image");
                                                            var myPage = doc.pages[0].duplicate(LocationOptions.AT_END);
                                                            var myFrame = myPage.pageItems.item("image");
                                                            myFrame.place(File(myImage.getAttribute("href").replace('file:///','')));
                                                            app.pdfExportPreferences.pageRange = myPage.name;
                                                            app.pdfExportPreferences.viewPDF  = false;
                                                 
                                                            myDoc.exportFile(ExportFormat.pdfType,
                                                            File("E:/HariharaSudhan/XMDOM_TEST/PDF/" +(i+ 1)+".pdf"), undefined,
                                                undefined, undefined, undefined);
                                                     }
                                                }
                                                
                                                
                                                

                                                Loic