10 Replies Latest reply on Aug 22, 2013 8:06 AM by techzol

    Scripting Error

    techzol

      I'm having difficulty figuring this one out.

       

      This script works:

         var myFormat = app.documents[0].crossReferenceFormats.item("Paragraph Text");

         var myXref = app.documents[0].hyperlinks[0];

         myXref.source.appliedFormat = myFormat;

       

      This script gives me an error message:

         var myFormat = app.documents[0].crossReferenceFormats.item("Paragraph Text");

         var myXref = app.documents[0].crossReferenceFormats[0];

         myXref.source.appliedFormat = myFormat;

       

      Now, according to the Document model, crossReferenceFormats is a property and [] is a method for that property. So, it seems to me that as hyperlinks is referring to the index of a specific hyperlink, then crossReferenceFormats should be referring to the index of a specific CrossReferenceFormat.

       

      What am i missing?

        • 1. Re: Scripting Error
          DaveSofTypefi Level 2

          Have you looked at the description of a crossReferenceFormat object in the ESTK Object Model Viewer?

           

          It is very easy to confuse yourself in a script if you give a variable a bad name -- calling your selected crossReferenceFormat "myXref" is something that will be very confusing later in the script. A Hyperlink has a source property, a crossReferenceFormat doesn't, so attempting to set the appliedFormat of a non-existent source is going to cause an error.

           

          Dave

          • 2. Re: Scripting Error
            techzol Level 1

            I have, but not being a very sophisticated scripter, it's a bit difficult to follow.

             

            It does appear to me that crossReferenceFormats[0] should refer to the cross reference format with an index of 0, so I am having a little difficulty understanding why hyperlinks[0] is a source property, but crossReferenceFormats[0] is not.

             

            I also did see something about crossreference id but I am not sure how to get it.

             

            My problem is connected to the other discussion we were having a couple of days ago.

             

            I need to figure out a scrip that will let me change the assignment of cross reference formats. The script you gave me will work only if I know the index number for every cross reference. I have discovered, however, then when you add a cross reference, InDesign re-alphabetizes the crossreferences so that their index numbers change.

             

            What I am seeking is a way to search a document, find each instance of a cross reference format of "Paragraph Text & Page Number" and change it to just "Paragraph Text".

            • 3. Re: Scripting Error
              techzol Level 1

              Dave,

               

              As a second follow up. You gave me this bit of code:

               

              var myFormats = app.activeDocument.crossReferenceFormats;

              $.writeln(myFormats.everyItem().name.join("\n"));

              ;

               

              and it works.

               

              You also gave me this bit of code:

               

              var myFormat = app.activeDocument.crossReferenceFormats.item("Paragraph Text");

              var myXref = app.activeDocument.hyperlinks[0];

              myXref.source.appliedFormat = myFormat;

               

              and it also works.

               

              Just for the fun of it, I changed your first bit of code to:

               

              var myFormats = app.activeDocument.hyperlinks;

              $.writeln(myFormats.everyItem().name.join("\n"));

              ;

               

              and it works.

               

              So for the life of me, I cannot understand why the following does't work:

               

              var myFormat = app.activeDocument.crossReferenceFormats.item("Paragraph Text");

              var myXref = app.activeDocument.crossReferenceFormats[0];

              myXref.source.appliedFormat = myFormat;

               

              since according to the DOM, crossReferenceFormats and hyperlinks are both properties of the Document, and both have identical [ ] (index) methods.

              • 4. Re: Scripting Error
                Dave Saunders Level 4

                Ice cream trucks have names and so do ice creams, but ice creams do not have front-left tires.

                 

                The reason your script fails is because a crossReferenceFormat does not have a source property and so you cannot set the appliedFormat of that source because it doesn't exist. You're trying to set the pressure of the front-left tire of an ice cream.

                 

                That's all I have time for this morning -- have to do my own work.

                 

                Dave

                • 5. Re: Scripting Error
                  techzol Level 1

                  Dave,

                   

                  I sincerely appreciate all the time you've given me even though I haven't quite caught on as to why cross reference formats don't have a source property. I also tried this script, and it worked:

                   

                  var myFormats = app.activeDocument.crossReferenceFormats;

                  $.writeln(myFormats.everyItem().index.join("\n"));

                  ;

                   

                  so I'm afraid I'm at sea about a cross reference not having a source property when it is, at its heart, as best as I can understand, a hyperlink. But, I'll keep digging.

                   

                  Thanks again for all your time.

                  • 6. Re: Scripting Error
                    Dave Saunders Level 4

                    cross references do have sources.

                     

                    cross reference formats don't.

                     

                    Dave

                    • 7. Re: Scripting Error
                      Dave Saunders Level 4

                      Ah, I finally see the cause of your confusion:

                       

                      All cross references are hyperlinks.

                       

                      But: all hyperlinks are not cross references.

                       

                      What you need to do is look at each hyperlink, determine if it is a cross reference and if it is of the kind you're interested in, and if so, change it in the way you want.

                       

                      That's an outline of how your script should operate.

                       

                      Dave

                      • 8. Re: Scripting Error
                        techzol Level 1

                        Dave,

                         

                        Great minds think alike. I finally realized that I was coming at this from the wrong angle.

                         

                        What I need to do is determine the number of hyperlinks in the document, and, as you said, examine each one and and determine its cross-reference format and then change it accordingly.

                         

                        I'm hacking at the script now. There are some things I'm not sure of yet.

                         

                        I know I need to start by determining the number of hyperlinks in the document and then do a for loop to sort through each one.

                         

                        Then, I think I need to determine the index of its cross reference format and re-assign it accordingly.

                         

                        Here's the snippet I've come up with so far:

                         

                        var NumHyper = app.activeDocument.hyperlinks.count();

                         

                        for (i=0; i<NumHyper; i++){

                            if (app.activeDocument.crossReferenceFormats[0].isValid){

                                var Link = ???

                                Link.source.appliedFormat = "Paragraph Text"}

                            }

                         

                        What I'm not sure of is how to get the hyperlink contents using the hyperlink index as the starting point. Am I on the right track, finally?

                        • 9. Re: Scripting Error
                          Dave Saunders Level 4

                          Your words are good, your code doesn't come close to doing what your words say. What is your obsession with crossReferenceFormats[0]?

                           

                          A document has cross reference formats so you can use them to format your cross references.

                           

                          You need something like this:

                           

                          myLinks = app.activeDocument.hyperlinks;
                          myFormat = app.activeDocument.crossReferenceFormats.item("Paragraph Text");
                          for (j = myLinks.length - 1; j >= 0; j--) {
                                    if (myLinks[j].source instanceof CrossReferenceSource) {
                                              if (myLinks[j].source.appliedFormat.name == "Full Paragraph & Page Number") {
                                                        myLinks[j].source.appliedFormat = myFormat;
                                              }
                                    }
                          }
                          
                          • 10. Re: Scripting Error
                            techzol Level 1

                            Don't have obsession with formats[0]; just thought I'd do this using indexes. You way works, I'll just have to add some "else" coding to handle the various possibilities. I was just focusing on one to get the basic approach right. Now, I'll need to extend it to cover my various cases.

                             

                            I cannot tell you how much I appreciate your help!!!!