13 Replies Latest reply on Aug 1, 2013 11:25 AM by techzol

    Continuing from a script error.

    techzol Level 1

      This has been a very frustrating problem to solve. We create technical documentation for industrial equipment. With each machine, we ship a physical manual and a CD with an inveractive PDF.

       

      For physical manual, we need to strip out the interactivity color formatting so the print version looks correct; i.e. the blacks are truly black and some black pixilated version of dark blue.

       

      I was looking for a script that would search and replace character styles so that I didn't have to do in manually, document by document.

       

      Vandy88 provided me with this nifty scrip which works like a charm:

       

      myBookFile = File.openDialog("Choose the BOOK File", "INDB:*.indb", false);

      var myDoc=[];

      if(myBookFile != null){  

          app.open(File(myBookFile.fullName.toString()));

          var myBook = app.activeBook

          for(var n=0; n < myBook.bookContents.length; n++)

          {

              myDoc[n] = app.open(File(myBook.bookContents[n].fullName.toString()));

              //--- cross referece style 1

              app.findGrepPreferences = app.changeGrepPreferences = null;

              app.findGrepPreferences.appliedCharacterStyle = "crossreference1";

              app.changeGrepPreferences.appliedCharacterStyle = "BoldUnderline";

              app.activeDocument.changeGrep();

              app.findGrepPreferences = app.changeGrepPreferences = null;

              //--- cross referece style 2

              app.findGrepPreferences = app.changeGrepPreferences = null;

              app.findGrepPreferences.appliedCharacterStyle = "crossreference2";

              app.changeGrepPreferences.appliedCharacterStyle = "BoldUnderline";

              app.activeDocument.changeGrep();

              app.findGrepPreferences = app.changeGrepPreferences = null;

          }

      }

      else{

          alert("Please select the book file properly !!!");

      }

       

      Now, here's my problem: Our books contain not only the chapter files, but also the Cover; TOC, Parts Lists, Schematics, and Index. Not all of these documents have the same character styles in use, as some of them, such as the Cover, TOC, Parts List, and Schematics, do not have cross-references. As nice as this script is, as soon as it enounters a file that does not have the character style it is looking for, it stops.

       

      What I need is a why to catch this error, tell it to ignore the document and move on to the next.

       

      I have expermented with Try/Catch but with no success. Cannot figure out what code to put in Catch to make it keep going or re-cycle. Even tried: Catch (e) {n=n+1} to no avail.

       

      Was wondering if there is some Catch code I haven't firgured out, or if I need to do some sort of an if/else statement that makes sure the first character style exists in the document, and then, if is not, tells the script to skip that document and move on the next. Otherwise, I'll have to create a custom book with only the documents I know have the styles. Not a difficult chore, but it would be best if I could have a block of code that would deal with the original book.

       

      Much thanks to Vandy88 for the original code and I would be very greatful to anyone who can help me figure out this problem.

        • 1. Re: Continuing from a script error.
          Kasyan Servetsky Level 5

          You can check if the styles are valid and if they are, run "change" command.

           

          var myFindStyle = myDoc.characterStyles.item("crossreference1");
          var myChangeStyle = myDoc.characterStyles.item("BoldUnderline");
          if (myFindStyle.isValid && myChangeStyle.isValid) {
              app.findGrepPreferences.appliedCharacterStyle = myFindStyle;
              app.changeGrepPreferences.appliedCharacterStyle = myChangeStyle;
              app.activeDocument.changeGrep();
          }
          
          1 person found this helpful
          • 2. Re: Continuing from a script error.
            techzol Level 1

            Interesting suggestion. I'll have to try it. I need to process a total of seven crossreference styles but that is just a matter of coding so should be no big deal.

             

            Question: How do I remove a character style completely? I.e. I want to change something with character style of Procedure and change it to no chanracter style (None). I've tried using both "None" and "[None]" in code, but it doesn't like either.

             

            Thanks for your help.

            • 3. Re: Continuing from a script error.
              Peter Kahrel Adobe Community Professional & MVP

              Try characterStyles[0]

               

              Peter

              • 4. Re: Continuing from a script error.
                Kasyan Servetsky Level 5

                I need to process a total of seven crossreference styles but that is just a matter of coding so should be no big deal.

                You can put a list of styles (find-change pairs) into array (to be more exact array of arrays) and then loop through its elements like so:

                 

                var i, myFindStyle, myChangeStyle,
                arr = [    ["crossreference1", "BoldUnderline1"],
                            ["crossreference2", "BoldUnderline2"],
                            ["crossreference3", "BoldUnderline3"],
                            ["crossreference4", "BoldUnderline4"],
                            ["crossreference5", "BoldUnderline5"],
                            ["crossreference6", "BoldUnderline6"],
                            ["crossreference7", "BoldUnderline7"]
                        ];
                                
                for (i = 0; i < arr.length; i++) {        
                    myFindStyle = myDoc.characterStyles.item(arr[i][0]);
                    myChangeStyle = myDoc.characterStyles.item(arr[i][1]);
                    
                    if (myFindStyle.isValid && myChangeStyle.isValid) {
                        app.findGrepPreferences.appliedCharacterStyle = myFindStyle;
                        app.changeGrepPreferences.appliedCharacterStyle = myChangeStyle;
                        app.activeDocument.changeGrep();
                    }
                }
                

                You  can also put this list into a separate txt-file and read it by script splitting it into array.

                • 5. Re: Continuing from a script error.
                  techzol Level 1

                  Tried it, Peter, and the result was that it deleted all of the text assigned to that character style. So, I'll have to find something else.

                   

                  Also, Kaysan, tried your suggestion but it didn't like the myDoc. When I changed it to app.activeDocument, it was quite happy. Thanks for puting me on the right track.

                   

                  Still need to figure out how to assign something to No Character Style. Tried Peter's suggestion and it turned out as noted. Also tried appliedCharacterStyle = 0; and it didn't work either.

                  • 6. Re: Continuing from a script error.
                    Kasyan Servetsky Level 5

                    doc.characterStyles[0] worked for me before, for example, in this script.

                    • 7. Re: Continuing from a script error.
                      techzol Level 1

                      Sorry, Kasyan, I couldn't read the script. The link took me to a Russian language site that seemed to show the 404 error code.

                       

                      Remember I am trying to change text from an assigned character style to having no character style.

                       

                      Love your array idea. Haven't quite gotten it to work yet but I'm sure I'll find my error soon.

                       

                      Thanks for all of the help.

                      • 9. Re: Continuing from a script error.
                        techzol Level 1

                        Sorry. Still can't get it to work. Here's the code snippet:

                         

                          var myFindStyle = app.activeDocument.characterStyles.item("CrossReferenceProcedure");

                               var myChangeStyle = app.activeDocument.applyCharacterStyle(characterStyles[0]);

                               if (myFindStyle.isValid && myChangeStyle.isValid) {

                        app.findGrepPreferences.appliedCharacterStyle = myFindStyle;

                        app.changeGrepPreferences.appliedCharacterStyle = myChangeStyle;

                        app.activeDocument.changeGrep();

                                }

                         

                        Some variations have resulted in deleted text and this various results in an error.     

                        • 10. Re: Continuing from a script error.
                          Kasyan Servetsky Level 5

                          var myChangeStyle = app.activeDocument.applyCharacterStyle(characterStyles[0]);

                          should be

                          var myChangeStyle = app.activeDocument.characterStyles[0];

                          • 11. Re: Continuing from a script error.
                            techzol Level 1

                            Correct; now, I'm having trouble getting it to work in an array. By the way, I figured out why I was having the earrlier problem with the array you proposed, I missing a closing {.

                             

                            Since assigning no style is a variation from the others, I figured out that the array wouldn't work without modification. This is what I came up with:

                             

                                   var i, myFindStyle, myChangeStyle,

                                   arr = [  ["CrossReferenceProcedure", "app.activeDocument.characterStyles[0]"],

                                              ["CrossReferenceSection", "app.activeDocument.characterStyles[0]"]

                                         ];

                                           

                                   for (i = 0; i < arr.length; i++) {       

                                        myFindStyle = app.activeDocument.characterStyles.item(arr[i][0]);

                                        myChangeStyle = (arr[i][1]);

                               

                                        if (myFindStyle.isValid && myChangeStyle.isValid) {

                                            app.findGrepPreferences.appliedCharacterStyle = myFindStyle;

                                            app.changeGrepPreferences.appliedCharacterStyle = myChangeStyle;

                                            app.activeDocument.changeGrep();

                                        }

                             

                            It looks to me like this should work. The script runs without error, but it does not change the styles. What am I missing?

                            • 12. Re: Continuing from a script error.
                              Kasyan Servetsky Level 5

                              The array should contain only style names, not references to styles:

                               

                              arr = [     ["CrossReferenceProcedure", "[None]"],
                                             ["CrossReferenceSection", "[None]"]
                                        ];
                              

                               

                              If you always change all the styles to "None", you can list only find-styles:

                               

                              var arr = [ "CrossReferenceProcedure", "CrossReferenceSection" ];
                              myChangeStyle = myDoc.characterStyles[0];
                              
                              1 person found this helpful
                              • 13. Re: Continuing from a script error.
                                techzol Level 1

                                Kasyan,

                                 

                                Your second code segment proved to be the correct one. For some reason, it just doesn't like "[None]".

                                 

                                In any case, the script now works beautifully and exactly as i need. Thank you so much for your help. 5 gold stars!