8 Replies Latest reply on Sep 28, 2015 2:57 AM by Liphou

    [CS4 JavaScript] Adding a CrossReferenceFormat

    frameexpert Level 4
      I am using a function to return a cross-reference format. If it doesn't exist, then I want to create it. Here is the code:

      var doc = app.activeDocument;
      
      var xrefFmt = getXRefFmt(doc,'Hoffman Accessory');
      alert(xrefFmt.name);

      function getXRefFmt(doc,formatName) {
        var xrefFmt = null;
        try {
          xrefFmt = doc.crossReferenceFormats.item(formatName);
          $.writeln(xrefFmt.name);
        }
        catch (e) {
          xrefFmt = doc.crossReferenceFormats.add({name:formatName});
          xrefFmt.buildingBlocks.add({blockType:BuildingBlockTypes.FULL_PARAGRAPH_BUILDING_BLOCK});
          xrefFmt.buildingBlocks.add({blockType:BuildingBlockTypes.CUSTOM_STRING_BUILDING_BLOCK,customText:', page '});
          xrefFmt.buildingBlocks.add({blockType:BuildingBlockTypes.PAGE_NUMBER_BUILDING_BLOCK});
        }
        return xrefFmt;
      }


      The function works as long as I have the $.writeln statement in the try block; otherwise the format is not created and I get an error on the alert statement on line 3. Why does the function fail without the $.writeln statement? Thanks.

      Rick Quatro
      585-659-8267
        • 1. Re: [CS4 JavaScript] Adding a CrossReferenceFormat
          Level 1
          Hi Rick,

          It's most likely that "$.writeln(xrefFmt.name)" throws an error that is then handled by the catch statement. JavaScript will always think that "xrefFmt = doc.crossReferenceFormats.item(formatName)" returns a valid object--it's only when you try to use some property of that object that it'll realize that the object is invalid.

          You could just change the line to: "xrefFmt.name;"--that will generate an error if the cross reference format does not exist.

          Thanks,

          Ole
          • 2. Re: [CS4 JavaScript] Adding a CrossReferenceFormat
            frameexpert Level 4
            Hi Ole,

            OK, I see now. I modeled my code after an example in the "Adobe InDesign CS4 Scripting Guide: JavaScript", but I missed this part:

            ...
            
            //If the style does not exist, trying to get its name will generate an error.
            myName = myCharacterStyle.name;
            ...


            Thank you very much for your help.

            Rick
            • 3. Re: [CS4 JavaScript] Adding a CrossReferenceFormat
              Level 1
              Rick,

              You can also test for null or undefined rather than trigger an error. I usually do it like this:
              var doc = app.activeDocument;
              
              var xrefFmt = getXRefFmt(doc,'Hoffman Accessory');
              alert(xrefFmt.name);

              function getXRefFmt(doc,formatName) {
                var xrefFmt = null;
                xrefFmt = doc.crossReferenceFormats.item(formatName);
                if (xrefFmt == null) {
                  xrefFmt = doc.crossReferenceFormats.add({name:formatName});
                  xrefFmt.buildingBlocks.add({blockType:BuildingBlockTypes.FULL_PARAGRAPH_BUILDING_BLOCK});
                  xrefFmt.buildingBlocks.add({blockType:BuildingBlockTypes.CUSTOM_STRING_BUILDING_BLOCK,customText:', page '});
                  xrefFmt.buildingBlocks.add({blockType:BuildingBlockTypes.PAGE_NUMBER_BUILDING_BLOCK});
                }
                return xrefFmt;
              }
              Dave
              • 4. Re: [CS4 JavaScript] Adding a CrossReferenceFormat
                frameexpert Level 4
                Hi Dave,

                This does seem cleaner; any idea why the documentation suggests the try/catch approach? Are there any circumstances where you would have to use try/catch instead of testing for null or undefined? Thanks.

                Rick
                • 5. Re: [CS4 JavaScript] Adding a CrossReferenceFormat
                  Level 1
                  Hi Rick,

                  I used try/catch in the scripting documentation because I think that it's easier for new scripters to understand. The idea being that null and undefined are somewhat esoteric concepts.

                  The point of the scripting documentation is clarity--you'll note that it's often more verbose than is really necessary for a production script. My thinking is that the hard part is learning InDesign object model, and that people can always optimize their scripts once they have the model in mind.

                  There are various other constraints, as well--the scripts need to use the same overall structures in all languages to make maintaining them easier. Sometimes, I have to do without a feature in one language or another to accomplish this.

                  Thanks,

                  Ole
                  • 6. Re: [CS4 JavaScript] Adding a CrossReferenceFormat
                    frameexpert Level 4
                    Hi Ole,

                    Understood; thanks for the explanation. The InDesign object model has been quite a challenge for me. Fortunately, some things are starting to "click". My background is scripting FrameMaker using FrameScript, a proprietary, third-party language. I think the FrameMaker SDK was way ahead of its time as far as performance and accessing FrameMaker objects, but I digress :-).

                    Rick
                    • 7. Re: [CS4 JavaScript] Adding a CrossReferenceFormat
                      Liphou Level 1

                      Hello, Thanks for this post I find it quite useful.
                      I am putting a tab


                      xrefFmt.buildingBlocks.add({blockType:BuildingBlockTypes.CUSTOM_STRING_BUILDING_BLOCK,cust omText:',^t page '});



                      But it does not work, can you help me?

                      Thank you

                       

                      -----------------

                      Bonjour, Merci pour ce poste il m'est bien utile.

                      Je voudrai mettre une tabulation

                       

                      ex:

                       

                          xrefFmt.buildingBlocks.add({blockType:BuildingBlockTypes.CUSTOM_STRING_BUILDING_BLOCK,cus tomText:',^t page '});

                       

                      Mais cela ne marche pas, pouvez-vous m'aidé ?

                       

                      Merci

                      • 8. Re: [CS4 JavaScript] Adding a CrossReferenceFormat
                        Liphou Level 1

                        OK

                         

                        xrefFmt.buildingBlocks.add({blockType:BuildingBlockTypes.CUSTOM_STRING_BUILDING_BLOCK,cus tomText:',\t page '});