3 Replies Latest reply on Mar 4, 2008 2:15 AM by Peter Kahrel

    Need to compare styles between two documents

    Tom Tomasko Level 1
      Hello,

      I need to compare the object, paragraph and character styles in an active document to a list of those styles that includes only the styles that should be in the document. This need arises because I often have old PageMaker files that are converted to InDesign. In the process of conversion, PageMaker styles that I no longer want sneak into the ID document. I'd like a script that will show me those bad styles.

      Below is a script that works just fine and dandy if I substitute two lists of simple words in the arrays (see bottom of script). It finds the words that are unique to each array and returns only them, deleting all words common to both arrays.

      But when I put into the first array "document.objectStyles.everyItem().name" what happens is thus: The first array returns all the object files just fine. The second array returns just fine a list of the only legal object styles I want in the document. But the string "illegalStyles"--which is supposed to be the difference between them--is in fact a combination of the two arrays.

      The problem must lay in the fact that "document.objectStyles.everyItem().name" returns something not comparable to the list in the second array. It is as if the words are spelled differently. (In fact, if I misspell every word in the simple list, the string is a combination of both arrays).

      So, how do I fix this?

      Thanks,

      Tom

      //Call up active document and create array of Object Styles in the active document. Second array is a list of the only Object Styles that should be in the document.
      var myDoc = app.activeDocument;
      var docObjectStyles = new Array(document.objectStyles.everyItem().name);
      var legalObjectStyles = new Array("Object Style 1", "Object Style 2");
      //Turn the arrays into strings with '~' separators
      var arrstring1 = '~' + docObjectStyles.join('~');
      var arrstring2 = '~' + legalObjectStyles.join('~');
      //Compares the words in both strings. If any are the same, deletes them from both strings. Creates final string with only the unique words.
      for (var i = 0; i < docObjectStyles.length; i++)
      {
      var replacestring = '~' + docObjectStyles[i];
      if (arrstring2.indexOf(replacestring) > -1)
      {
      arrstring1 = arrstring1.replace(replacestring, '');
      arrstring2 = arrstring2.replace(replacestring, '');
      }
      }
      var arrstring = (arrstring1 +arrstring2).substr(1);
      var illegalStyles = arrstring.split('~');
      //First alert shows Object Styles in active document.
      //Second alert is a list of the only Object Styles that sould be in the document.
      //Third alert should show the extra Object Styles that should not be in the document.
      Window.alert(docObjectStyles);
      Window.alert(legalObjectStyles);
      Window.alert(illegalStyles);

      // By substituting the arrays below for the objectStyles, you can see that the javascript does work.
      //var docObjectStyles = new Array("tanks","Banks","Thanks","men","women","gals");
      //var legalObjectStyles = new Array("men","tanks","guys","Banks","women","gals");
        • 1. Re: Need to compare styles between two documents
          Peter Kahrel Adobe Community Professional & MVP
          Tom,

          The problem is in this line:
          >var docObjectStyles = new Array(document.objectStyles.everyItem().name);

          The statement document.objectStyles.everyItem().name creates an array, and you create an array out of that. Check what's in arrstring1 by adding after the line that begins with "var arrstring1 = '~' + doc..." this line:
          >$.writeln (arrstring1);

          You'd expect something like ~style1~style2~style3, but instead you get ~style1,style2,style3. Try your script with this line to create your docObjectStyles array.

          >var docObjectStyles = document.objectStyles.everyItem().name;

          Peter
          • 2. Re: Need to compare styles between two documents
            Tom Tomasko Level 1
            Peter,
            Thanks very much. I see the problem now. I was making an array out of an array. I did not realize the line "document.objectStyles.everyItem().name" is an array itself.

            I am very new at this, so I have yet another question. You said I should use "$.writeln (arrstring1);" to check the variable "arrstring1". I did so, but nothing happened. This is a method for debugging, right? But I did not see any result.

            I am using ExtendScript Toolkit 2.

            Thanks again,

            Tom
            • 3. Re: Need to compare styles between two documents
              Peter Kahrel Adobe Community Professional & MVP
              Tom,

              That probably means that you don't have the console displayed. To see that invaluable window, enable it in Window > Javascript Console. It'll probably appear on the top right of your screen.

              The console is an output/input window. It always displays the last result of a script after it has finished, but you can output intermediate results by using $.writeln(). Another way to check the value of a variable is to set a breakpoint somewhere, then type the variable's name in the console. For example, set a breakpoint at the line that begins with "var arrstring2 = '~' +" (click on the line number to set a breakpoint there; a red bullet appears to mark the breakpoint) and run the script. It stops at the breakpoint. Now click in the console, type arrstring1 and press Enter/Return. The current contents of the variale is printed in the console. You could do this for all variables that have been assigned a value so far (myDoc, myObjectStyles, etc.) Stepping through a script using F10 and/or F11 you can check intermediate results in the same way.

              The console can also be used to type commands. For example, select a text frame in InDesign and type app.selection[0] in the console: it prints [object TextFrame].

              ESTK's finer details are described in the documentation (Help > SDK > Javascript tools guide).

              Happy debugging!

              Peter