9 Replies Latest reply on Jul 21, 2009 2:02 AM by Peter Kahrel

    CS4/JS: Create cross reference from XML-Data

    franzi_ska Level 1

      Hi Scripters.

       

      My problem is the following:

      I am importing and scripting a XML-file into InDesign.

      Now I'd like to insert a crossreference (endnotes) into my text.

      The insertion point is after element <endnotesign>, the endnotetext itself stands at the end of the document (and is also numbered).

      When manually editing the document I choose --> "insert crossreference" and then link it with "paragraph" afterwards I choose a special paragraphstyle "endnotestyle", there all paragraphs are listed, I now link the first of them and insert "paragraph number" (which comes from numbering the "endnotestyle"). I have to do this after every element <endnotesign>

      Is there a way to automate this?

      I am a little bit confused with the IndesignObjects in ESTK as there are crossreferences and hyperlinks mixed with each other in this content.

       

      Thanks in advance for your help!!!

       

      this is my first fragmentary try:

       

      function processEndnotes(){
          this.name = "processEndnotes";
          this.xpath = "//endnotesign"
          this.apply = function (myElement, myRuleProcessor) {
              var myInsertionPoint = myElement.insertionPoints[0];
              var mySource = myDocument.hyperlinkTextSources.add(myInsertionPoint);

                // how to define the destination in this case?

              var xRefForm = doc.crossReferenceFormats.item("page nr.");

              var source = myDocument.crossReferenceSources.add(myInsertionPoint, xRefForm);
              var myLink = myDocument.hyperlinks.add(source, destination);
              myLink.visible = false;
          return true;
          }
      }

        • 1. Re: CS4/JS: Create cross reference from XML-Data
          Peter Kahrel Adobe Community Professional & MVP

          Hi franziska,

           

          Here is an example of how to script endnotes using cross-references:

          http://www.kahrel.plus.com/indesign/dynamic_endnotes.html

           

          Peter

          • 2. Re: CS4/JS: Create cross reference from XML-Data
            franzi_ska Level 1

            Hi Peter,

             

            thank you very much for the very helpful answer.

            Now I only have to understand the script.

             

            Thanks again!!!

            • 3. Re: CS4/JS: Create cross reference from XML-Data
              franzi_ska Level 1

              Hi Peter,

               

              I just tried to edit your skript according to my requirements. They are the following:

              I have my endnotesigns in the text with a special character style ("Endnotenzeichen").

              The endnotestyles itself are already in the mentioned numbered paragraph style.

              It is nothing more to do than inserting a crossreference at the position of the endnotesign and link it with the according endnotetext.

              I am not sure with some functions, could you help me with the script?


              function dynamic_endnotes(){
                  // add a cross-reference format
                  var cr_format = add_crossref_format (myDocument, 'endnote');
                  // link notes and references
                  crossref_notes (myDocument, cr_format);
              }

               

              function add_crossref_format (myDocument, s, c_style){
                  try{ // the only reason why this could fail is if the format already exists

                        // which meaning does the expression "{name: s}" have?    
                      var cr = myDocument.crossReferenceFormats.add ({name: s});
                      var c_style = myDocument.characterStyles.itemByName("Endnotenzeichen");
                      cr.appliedCharacterStyle = app.activeDocument.characterStyles.item (c_style);
                      cr.buildingBlocks.add (BuildingBlockTypes.paragraphNumberBuildingBlock);
                      }
                  catch(_){}
                  return cr
                  }

               

              function crossref_notes (myDocument, reference_style, note_style, cr_format){
                  var references = find_references (myDocument, reference_style);
                  var notes = find_notes (myDocument, note_style);
                  for (var i = references.length-1; i > -1; i--){

                        // at this point InDesign tells me that "paragraphDestinations" is not a correct method         
                      var endnote_link = myDocument.paragraphDestinations.add (notes[i].insertionPoints[0]);
                      var reference = myDocument.crossReferenceSources.add (references[i].insertionPoints[0], cr_format);

                        // what is the need for saying "contents = "";" ?                        
                      references[i].contents = "";
                      myDocument.hyperlinks.add (reference, endnote_link, {visible: false});
                      }
                  myDocument.crossReferenceSources.everyItem().update();
                  }

               

              function find_references (mydocument)
                  {
                  app.findGrepPreferences = null;
                  app.findGrepPreferences.appliedCharacterStyle =myDocument.characterStyles.itemByName("Endnotenzeichen");
                  return myDocument.findGrep();
                  }

               


              function find_notes (myDocument)    {
                  app.findGrepPreferences = null;
                  app.findGrepPreferences.findWhat = '^\\d+';
                  app.findGrepPreferences.appliedParagraphStyle = myDocument.paragraphStyles.itemByName("anm");
                  return myDocument.findGrep();
                  }

               

               

              Thanks again!!!

              • 4. Re: CS4/JS: Create cross reference from XML-Data
                Peter Kahrel Adobe Community Professional & MVP

                // which meaning does the expression "{name: s}" have?

                 

                The line

                 

                var cr = myDocument.crossReferenceFormats.add ({name: s});

                 

                creates a cross-reference format whose name is the contents of the variable "s". In the script, that's "endnote".

                 

                // at this point InDesign tells me that "paragraphDestinations" is not a correct method

                 

                You said that your endnotes are formatted with numbered paragraph styles. In that case they don't have a hard-coded number, but an automatic number. The GREP search "^\\d+" therefore fails so that find_notes returns an empty array. You'd need a different way to find the endnote paragraphs. You could try this GREP in find_notes: "^." There's no need to match the whole paragraph, just the first character will do.

                 

                There is a potential problem here, namely, if you have multi-paragraph notes. I used "^\\d+" so that I matched just paragraphs formatted with (what you call) "anm" and that start with a number, which is a pretty safe way of avoiding multi-paragraph notes. Looking for "^." is safe only when all your notes consist of just one paragraph.

                 

                If you do have multi-paragraph notes you'd have to turn them into single-paragraph notes somehow, e.g. by temporarily replacing note-internal paragraph marks with some string. After the script has finished you then replace those placeholders with paragraph marks.

                 

                // what is the need for saying "contents = "";" ?

                 

                references[i] are the static footnote references, i.e. hard-coded numbers (that's how the note references are after placing a Word/RTF file). These need to be deleted after the script adds the cross-reference (i.e. the dynamic number). In your case, that would be the item tagged with the XML element <endnotesign>.

                 

                Peter

                1 person found this helpful
                • 5. Re: CS4/JS: Create cross reference from XML-Data
                  franzi_ska Level 1

                  Thank you very much for your answer.

                  Unfortunately I still receive the error message:

                  at this point InDesign tells me that "paragraphDestinations" is not a correct method

                  Do you have an idea what else could be wrong?

                   

                  Thanks in advance!

                  • 6. Re: CS4/JS: Create cross reference from XML-Data
                    Peter Kahrel Adobe Community Professional & MVP

                    What exactly does the error message say?

                     

                    You mentioned that you want to insert the reference after the element <endnotesign>. Maybe you pick an insertion point inside a tag (or inside tagged text), and maybe InDesign doesn't like that. You can send me one of your files if you want and I can have a look, that may be handier than guessing in the dark.

                     

                    Peter

                    • 7. Re: CS4/JS: Create cross reference from XML-Data
                      franzi_ska Level 1

                      Hi Peter,


                      I attached the Script at the current state.

                      Conditions are the following:

                      • InDesign document with text and graphic
                      • endnotesigns within the text, formatted with characterstyle "Endnotenzeichen"
                      • endnotes at the end of the text, formatted with paragraphstyle "anm" - which is already numbered paragraph style

                      I inserted the endnotesign within the xml element, but at the end I think that doesn't matter as the script does not use of the xml elements in indesign but of the formatted endnotes and endnotesigns.

                       

                      I would be very glad if you could help me.

                       

                      Franziska.

                      • 8. Re: CS4/JS: Create cross reference from XML-Data
                        franzi_ska Level 1

                        I think it didn't attach the script, so I will post it here:


                        // Convert Static Endnotes to Dynamic Endnotes
                        // EndnoteToEndnote.jsx
                        
                        // Script works only in CS4 and later
                        if (parseInt (app.version) > 5 && app.documents.length > 0)
                            dynamic_endnotes ();
                            
                        var myDocument = Application.activeDocument;
                        
                        function dynamic_endnotes(){
                            // add a cross-reference format
                            var cr_format = add_crossref_format (myDocument, 'endnote');
                            // link notes and references
                            crossref_notes (myDocument, cr_format);
                        }
                        
                        function add_crossref_format (myDocument, s, c_style){
                            try{ // the only reason why this could fail is if the format already exists
                                var cr = myDocument.crossReferenceFormats.add ({name: s});
                                var c_style = myDocument.characterStyles.itemByName("Endnotenzeichen");
                                cr.appliedCharacterStyle = app.activeDocument.characterStyles.item (c_style);
                                cr.buildingBlocks.add (BuildingBlockTypes.paragraphNumberBuildingBlock);
                                }
                            catch(_){}
                            return cr
                            }
                        
                        function crossref_notes (myDocument, reference_style, note_style, cr_format){
                            var references = find_references (myDocument, reference_style);
                            var notes = find_notes (myDocument, note_style);
                            for (var i = references.length-1; i > -1; i--){
                                var endnote_link = Applcation.activeDocument.paragraphDestinations.add (notes[i].insertionPoints[0]);
                                var reference = Applcation.activeDocument.crossReferenceSources.add (references[i].insertionPoints[0], cr_format);
                                references[i].contents = "";
                                myDocument.hyperlinks.add (reference, endnote_link, {visible: false});
                                }
                            myDocument.crossReferenceSources.everyItem().update();
                            }
                        
                        function find_references (mydocument)
                            {
                            app.findGrepPreferences = null;
                            app.findGrepPreferences.appliedCharacterStyle = myDocument.characterStyles.itemByName("Endnotenzeichen");
                            return myDocument.findGrep();
                            }
                        
                        
                        function find_notes (myDocument)    {
                            app.findGrepPreferences = null;
                            app.findGrepPreferences.findWhat = '^.';
                            app.findGrepPreferences.appliedParagraphStyle = myDocument.paragraphStyles.itemByName("anm");
                            return myDocument.findGrep();
                            }
                        
                        • 9. Re: CS4/JS: Create cross reference from XML-Data
                          Peter Kahrel Adobe Community Professional & MVP

                          Franziska,

                           

                          Could you send me one of your problematic documents? Never mind the graphics.

                          My address is here: http://www.kahrel.plus.com/info.html

                           

                          Peter