16 Replies Latest reply on Jul 28, 2009 9:46 AM by slaart0

    Script is crashing ID3 with no error given???

    slaart0

      When trying to evaluate the "appliedNestedStyle" object of a Character item ID3 will crash.  Cannot trap it or debug it.  Even when the debugger tries to access the object ID3 will crash.  Below is partial script. If I comment out the appliedNestedStyle line then no problems.

       

       

      for(var iiii = 0; iiii < myCell.characters.length; iiii++){

      var myChar = myCell.characters.item(iiii);

      var pStyle = myChar.appliedParagraphStyle.name

      var cStyle = myChar.appliedNestedStyle ? myChar.appliedNestedStyle.name : "none";

      alert(myChar.contents + " \nParagraph Style = " + pStyle + "\nCharacter Style = " + cStyle);

      }

        • 1. Re: Script is crashing ID3 with no error given???
          Thomas B. Nielsen Level 3

          I dont have CS on this comp, but shouldn't you be able to loop in textStyleRanges?

          The line  var pStyle = myChar.appliedParagraphStyle.name miss a ; at the end.

          • 2. Re: Script is crashing ID3 with no error given???
            [Jongware] Most Valuable Participant

            I agree with Thomas, using TextStyleRanges might speed it up a bit :-)

             

            It just might be the construction

             

            cStyle = myChar.appliedNestedStyle ? myChar.appliedNestedStyle.name : "none";

             

            I'm not aware of any special JS operator precedences, but if the JS engine tries to evaluate the entire expression at once, it might get into trouble accessing the name of an undefined object (if the nested style is not there). Now usually this should throw an 'undefined' error, but this case might be the proverbial overlooked edge-case.

            Try replacing the line with a more conventional if..then.

            • 3. Re: Script is crashing ID3 with no error given???
              slaart0 Level 1

              Thanks for the responses.  Let me explain what I'm trying to do and maybe there is a different angle that can be presented.  I'm attempting to create index topic entry based on applied cell style.  The cell style is based on paragraph style which contains nested character style. Consider the entries below:

               

              123456(1)

              789012

               

              I don't want the superscript characters contained in the index topic, only the part number itself.  The part number characters have nested character style applied while the superscript characters are defined in the paragraph style.

               

              Using the code below I'm able to get the entire character string added to the index topics.  Was just looking for a way to index only the part number.

               

              // IF INDEX DOESN'T EXIST THEN ADD
              if (app.activeDocument.indexes.length == 0){
                  var index = app.activeDocument.indexes.add();
              }
              else{
                  // DELETE EXISTING INDEX TOPICS AND PAGE REFERENCES
                  var index = app.activeDocument.indexes[0];
                  while (index.topics.length > 0){
                      var topic = index.topics[0];
                      topic.remove();
                     
                  }
              }

               

              if(app.documents.length != 0){
                  var myDoc = app.activeDocument;
                  if(myDoc.textFrames.length != 0){  

               

                      // LOOP THROUGH TEXTFRAMES
                      for(var i = 0; i < myDoc.textFrames.length; i++){
                          var myTextFrame = myDoc.textFrames.item (i);
                          if(myTextFrame.tables.length != 0){
                             
                              // LOOP THROUGH TABLES IN TEXTFRAME
                              for(var ii = 0; ii < myTextFrame.tables.length; ii++){
                                  var myTable = myTextFrame.tables.item (ii);
                                  if(myTable.cells.length != 0){
                                     
                                      //LOOP THROUGH CELLS IN TABLE
                                      for(var iii = 0; iii < myTable.cells.length; iii++){
                                          var myCell = myTable.cells.item(iii);
                                          if(myCell.appliedCellStyle.name == "Catalog Number"){
                                              var text = myCell.texts[0];
                                             
                                              // CREATE INDEX TOPIC FOR CATALOG/PART NUMBER
                                              topic = app.activeDocument.indexes[0].topics.add( myCell.contents );
                                             
                                              // ADD PAGE NUMBER REFERENCE TO CATALOG/PART NUMBER
                                              topic.pageReferences.add(text, PageReferenceType.currentPage);
                                          }
                                      }
                                  }
                              }
                          }
                      }
                  }
                  alert("Index entries have been updated!");
              }
              else{
                  alert("Please open a document and try again!");
              }

              • 4. Re: Script is crashing ID3 with no error given???
                Peter Kahrel Adobe Community Professional & MVP

                To check if a character or (or any text) has a nested style applied, do this:

                 

                if (myChar.appliedNestedStyles.length > 0)

                 

                But maybe in your case you could take still another approach. If the part numbers consist of just digits and if what you want to exclude is either a number in parentheses or some symbol like the dagger, then you can capture the part number like this:

                 

                myTopic = myCell.contents.match (/^\d+/);

                if (myTopic != null)

                   {

                   myTopic = myIndex.topics.add (myTopic[0]);

                   // add a page reference

                   // etc.

                   }

                 

                 

                Peter

                • 5. Re: Script is crashing ID3 with no error given???
                  slaart0 Level 1

                  Hi Peter,

                   

                  Unfortunately evaluating the appliedNestedStyle of a character seems to be a flaw or bug in ID3 as it crashes each time this is attempted.  Even from the Data Browser in the Extend Script tool.  Thus the reason for this thread (see original post).

                   

                  The regular expression would no doubt probably work if I could pattern all possible cases.  However, I don't know regular expressions which is why I did not attempt this route.

                   

                  The part numbers are not always numeric. For example:

                   

                  1234

                  M1234

                  M1234-5

                   

                  Any of these could be succeeded with a symbol or alphanumeric contained in parenthesis.

                  • 6. Re: Script is crashing ID3 with no error given???
                    Peter Kahrel Adobe Community Professional & MVP

                    It's not a bug: you don't use it correctly. Check the documentation (Jongware's, since you work in CS3) and you'll see that nested styles don't have names (or anything much else for that matter). They do have appliedCharacterStyles, so your syntax should be:

                     

                    var cStyle = myChar.nestedStyles[0].appliedCharacterStyle;

                     

                    in fact, text can have several nested styles applied to it, so you should cycle through the text's applied nested styles:

                     

                    for (i = 0; i < myChar.nestedStyles.length; i++)

                       if (myChar.nestedStyles[i].appliedCharacterStyle.name == ??)

                          // do something

                     

                    What you try to achieve in this line:

                     

                    var cStyle = myChar.appliedNestedStyle ? myChar.appliedNestedStyle.name : "none";

                     

                    isn't clear to me, but that may be because I always have great difficulty deciphering if-then-else statements of the form "x == y ? this : that". It seems to me, by the way that your "var cStyle = myChar.appl..." should be "var cStyle == myChar.appl...". Is that so?

                     

                    Peter

                    • 7. Re: Script is crashing ID3 with no error given???
                      slaart0 Level 1

                      Peter - sorry that I'm not too advanced yet.  I'm trying to find my way.  Using your suggestion I have attemped the following code with the given scenario below:

                       

                      Scenario:

                      ID table cell with applied cell style called "Catalog Number".  Cell style has paragraph style called "Catalog/Part Number" which contains nested character style called "Index Character" through the 1st "(".  The contents of the cell is "100621(1)".

                       

                      if(myCell.appliedCellStyle.name == "Catalog Number"){

                           var myTopic;
                           for(var i= 0; i < myCell.characters.length; i++){
                                var myChar = myCell.characters.item(i);
                                if (myChar.nestedStyles.length > 0){
                                     for(var ii = 0; j < myChar.nestedStyles.length; ii++){
                                          if(myChar.nestedStyles[ii].appliedCharacterStyle.name == "Index Character"){
                                               myTopic = myTopic + myChar.contents;

                                          }

                                     }

                                }

                           }

                      }

                       

                      After excuting this scripting myTopic contains "undefined100621(1)".  I'm attempting to get myTopic to contain "100621".

                      • 8. Re: Script is crashing ID3 with no error given???
                        Peter Kahrel Adobe Community Professional & MVP

                        If you're finding your way then you've picked a tough topic to start on! Nested styles are complicated, but in this case there's an easy way out. First the bad news.

                         

                        Unfortunately, your approach isn't going to work because it turns out that in your cells, every character will tell you that it has the character style "Index Character" applied to it (via the nested style), even the characters outside the scope of the nested style (if I had known that I'd have told you earlier!). You can see this as follows: select a character in a cell with a catalogue number, which has the paragraph style "Catalog/Part Number" assigned to it and run this script:

                         

                        app.selection[0].nestedStyles[0].appliedCharacterStyle.name

                         

                        ESTK will print "Index Character" in the console. Now select a character that's outside the scope of the nested style and run that one-line script again. You'd expect an error (because you thing that the selected character has no nested style applied to it), or null, or nothing -- but you wouldn't expect "Index Character". But that's what you get. (By the way, you describe the nested style as 'through the 1st "("' but that should be "up to 1st (".)

                         

                        Now the good news. There's a much easier way to go about this: just look for all instances of character style "Index Character". You'd get something like this:

                         

                        var myIndex = app.documents[0].indexes.add();
                        app.findGrepPreferences = null;
                        app.findGrepPreferences.appliedCharacterStyle = app.documents[0].characterStyles.item ("Index Character");
                        myFound = app.activeDocument.findGrep ();
                        for (var i = myFound.length-1; i > -1; i--)
                          {
                          var myTopic = myIndex.topics.add (myFound[i].contents);
                          myTopic.pageReferences.add (myFound[i], PageReferenceType.currentPage)
                          }

                        This should also be a lot quicker. Does this give you what you were after?

                         

                        Peter

                        • 9. Re: Script is crashing ID3 with no error given???
                          slaart0 Level 1

                          Ahhh...was not aware of this method.  However, the script works only if I apply directly the character style "Index Character" to selected text in document.  It does not find any text as result of cell style or paragraph style with nested character style.  I even tried using the "Find/Replace" dialog in ID3.  Is the document construction wrong?  Does the script work as intended for you?

                          • 10. Re: Script is crashing ID3 with no error given???
                            Peter Kahrel Adobe Community Professional & MVP

                            > It does not find any text as result of cell style or paragraph style with nested character style.

                             

                            Indeed. I didn't get the style assignments right. On to yet another approach!

                             

                            var myIndex = app.documents[0].indexes.add();
                            app.findGrepPreferences = null;
                            app.findGrepPreferences.findWhat = "^[^(]+";
                            app.findGrepPreferences.appliedParagraphStyle = app.documents[0].paragraphStyles.item ("Catalog/Part Number");
                            myFound = app.activeDocument.findGrep ();
                            for (var i = myFound.length-1; i > -1; i--)
                               {
                               var myTopic = myIndex.topics.add (myFound[i].contents);
                               myTopic.pageReferences.add (myFound[i], PageReferenceType.currentPage)
                               }

                             

                            The nested style is applied from the beginning of the string up to the first ( (if any). This is easily captured by a GREP expression the says "match characters from the beginning of a striong up to the first (. The script now looks for anything to which the paragraph style "Catalog/Part Number" has been applied and takes the contents of tose paragraphs up to the first (.

                             

                            You said you didn't know GREP -- you could do yourself no bigger favour than learning some basics on the topic. Up to a still useful level it's not that hard.

                             

                            Peter

                            • 11. Re: Script is crashing ID3 with no error given???
                              slaart0 Level 1

                              Peter - this does indeed work and I will mark the question answered.  Is it possible to modify it to include the scenarios I posted in reply 3 above?  The part numbers can be succeeded by numbers or letters contained in parenthesis (which is handled already) or sometimes symbols are used (dagger, double dagger, and others) and the symbols are not contained in parenthesis.

                               

                              Many thanks for your patience and guiding.

                              • 12. Re: Script is crashing ID3 with no error given???
                                Peter Kahrel Adobe Community Professional & MVP

                                In the case of M1234-5, should the topic be 1234 or 1234-5?

                                • 13. Re: Script is crashing ID3 with no error given???
                                  slaart0 Level 1

                                  Actually it should be M1234-5

                                  • 14. Re: Script is crashing ID3 with no error given???
                                    Peter Kahrel Adobe Community Professional & MVP

                                    So if I understand it correctly, the part numbers consist of letters, digits, and/or hyphens, maybe followed either by a superscripted number in parentheses or by a symbol. If that's the case, then the following should work. Replace this line:

                                     

                                    app.findGrepPreferences.findWhat = "^[^(]+";

                                     

                                    with these two:

                                     

                                    app.findGrepPreferences.findWhat = "^[-\\w]+";
                                    app.findGrepPreferences.position = Position.normal;

                                     

                                    and see what happens.

                                     

                                    Peter

                                    • 15. Re: Script is crashing ID3 with no error given???
                                      slaart0 Level 1

                                      Excellent this works.  Although I can't quite figure out what it is doing.  Do you care to explain this line?

                                       

                                      app.findGrepPreferences.findWhat = "^[-
                                      w]+";

                                       

                                      ^ = Beginning of paragraph

                                      = Character Set

                                      \w = Any word character (which is exactly what?)

                                      + = one or more times

                                       

                                      Not sure about the -\

                                      • 16. Re: Script is crashing ID3 with no error given???
                                        Peter Kahrel Adobe Community Professional & MVP

                                        "^[-\\w]+" corresponds with ^[-\w]+ in the interface. In a script you need to escape the backslash, therefore \w is written as \\w.

                                         

                                        \w is the GREP class "word character", which covers letters, digits, and the underscore, but not the hyphen.

                                         

                                        Peter