5 Replies Latest reply on Oct 29, 2009 1:44 PM by [Jongware]

    [JS InDesign CS3] Style groups, begone! (or: How do I take paragraph and character styles out of style groups?)

    JuanJMacias

      Sorry for this question (and my terrible English, by the way), I'm a javascript noob and I know when I've reached my limits.

      Well, I'm trying to take a set of paragraph and character styles out of the style groups they are placed in, in hundreds of InDesign documents (that cannot be treated as a book). As far as I've tried (thanks to the invaluable help of previous posts in this forum) I've been able to move a style into a group and change it's position inside the group, inside the root level or even between groups. But it doesn't matter how I try, I don't know which move reference should I try in case I want to take every style out of their style group and place them after their original group folder, at the [Root] style level .

      I have tried:

       

      var doc=app.activeDocument;
      var pGroups=doc.paragraphStyleGroups;
      
      for (i=pGroups.length-1; i>=0; i--){
           var pStylesInGroup=pGroups[i].paragraphStyles;
           for (j=pStylesInGroup.length-1; j>=0; j--){
      // Here I am, trying to move a style outside the folder that contains it, and failing miserably.
                pStylesInGroup[j].move (LocationOptions.after, pGroups[i]);
           }
      }
      

       

      It didn't work, the script sent an invalid parameter value in the reference field, so I cant use the group folder itself as reference.

      Tried other (obviously wrong) solutions, like use the first available style as reference. Therefore, my second script was

       

       

       

      var doc=app.activeDocument;
      var pStyles=doc.allParagraphStyles;
      var pGroups=doc.paragraphStyleGroups;
      
      for (i=pGroups.length-1; i>=0; i--){
           var pStylesInGroup=pGroups[i].paragraphStyles;
           for (j=pStylesInGroup.length-1; j>=0; j--){
      // Now I try placing the styles after the [Basic Paragraph], that is, the second paragraph style in the document style list 
                pStylesInGroup[j].move (LocationOptions.after, pStyles[1]);
           }
      }
      
      

      It didn't work either, another invalid parameter in the reference field. Similar results with my other attempts (I even tried "[Root]" as literal with similar luck).

       

      So, my question is: Which command (or script, in case my whole approach is utterly wrong) should I use to achieve my goal?

       

      Thanks in advance to whoever decides to spend more than a minute thinking about my humble worries, and my apologies for shamelessly ripping some of your lines of code for my purposes.

        • 1. Re: [JS InDesign CS3] Style groups, begone! (or: How do I take paragraph and character styles out of style groups?)
          [Jongware] Most Valuable Participant
          .. my terrible English, by the way ..

           

          What, you're kidding me? Read some of the other messages

           

          You seem to have had the right ideas; well, at least, you mention you tried both ways that popped up in my mind. Perhaps it's just failing because you try to move items around inside doc.allParagraphStyles -- I've had a few nasty collisions with this, as it seems not to be quite the same as "doc.paragraphStyles". This latter one only returns you the "root" styles as a regular array; allParagraphStyles is (I think!) a cheat/kludge kind of thing.

           

          Try your second piece of code with doc.paragraphStyles[0] (or [1]) as reference.

          • 2. Re: [JS InDesign CS3] Style groups, begone! (or: How do I take paragraph and character styles out of style groups?)
            JuanJMacias Level 1

            Thanks for your really fast response. When I posted this message, I believed that it would get lost among the huge mass of similar help requests, but seeing that someone is taking interest is truly a joy.

             

            Well, following your advice I changed my code to this:

             

            var doc=app.activeDocument;
            // As you may see, now I'm using 'paragraphStyles' instead of 'allParagraphStyles' due to the fact that the latter is a little tricky
            var pStyles=doc.paragraphStyles;
            var pGroups=doc.paragraphStyleGroups;
            
            for (i=pGroups.length-1; i>=0; i--){
                 var pStylesInGroup=pGroups[i].paragraphStyles;
                 for (j=pStylesInGroup.length-1; j>=0; j--){
            // Now I try placing the styles after the [Basic Paragraph], that is, the second paragraph style in the document style list 
                     pStylesInGroup[j].move (LocationOptions.after, pStyles[1]);
                 }
            }
            

             

            Unfortunately, I got the same error after executing this script. This invalid parameter value is really driving me nuts, I don't understand what I'm doing wrong; InDesign CS3 object model says that you may place a paragraphStyle or paragraphStyleGroup as reference (in this particular case), but the script keeps on telling me that it got an 'Invalid parameter value as reference for the move event. Expected ParagraphStyle, ParagraphStyleGroup, CharacterStyle, yaddayaddayadda... , but only received ParagraphStyle' (sorry if the message isn't exactly the same, but my Extendscript editor is in Spanish). I suppose that trying to move a paragraph style to the character style or object style lists should generate this kind of warning, but in this particular case, I'm really lost.

            • 3. Re: [JS InDesign CS3] Style groups, begone! (or: How do I take paragraph and character styles out of style groups?)
              [Jongware] Most Valuable Participant

              Okay, tried a few things and got really weird results!

               

              1. You can move a style around inside its group.

              2. You can move a style out of a group "to" another, but it will appear 'inside' that style. I got my test style as a sub-item of [Basic Paragraph], using index #1. With index #0 ([No Paragraph Style]) InDesign crashed.

              3. You can duplicate the style, but then you get a copy in the same group. Still no luck.

              4. Finally! What is the parent of a paragraph style?

               

              Document | Application | ParagraphStyleGroup

               

              "Application" is easy -- that's when you make a style global. So what's the difference between 'Document' and 'paragraphStyleGroup'? Simple -- well, when you finally get it...

               

              pStylesInGroup[j].move (LocationOptions.AT_END, doc);

               

              moves the style out of the group and to the end of the list in the document. I don't think it's possible to move it directly to a specific position into the main style list -- you first have to move it out of a group, then move it around in its own list.

              Fortunately, it returns its new position as a ParagraphStyle again, so if needed, you can use

               

              newStyle = pStylesInGroup[j].move (LocationOptions.AT_END, doc);
              newStyle.move (LocationOptions.AFTER, pStyles[1]);
              

               

              -- I didn't really try that out, but it should work.

              • 4. Re: [JS InDesign CS3] Style groups, begone! (or: How do I take paragraph and character styles out of style groups?)
                JuanJMacias Level 1

                Wow! That really DID work, at last! Mr. Jongware, you're really a lifesaver.

                 

                It's weird that you have to aim to the document, instead of a more logical reference in the paragraph style list. But it works, so I wont complain.

                 

                Thanks a lot, and kudos to you, sir.

                • 5. Re: [JS InDesign CS3] Style groups, begone! (or: How do I take paragraph and character styles out of style groups?)
                  [Jongware] Most Valuable Participant

                  I learned something as well: ID is not kidding if it tells you that the parent of a style is a document.

                   

                  In retrospect, it there is some logic behind it; app.activeDocument.paragraphStyles point to the paragraph styles "in" the document, while app.activeDocument.paragraphStyleGroups[0].paragraphStyles point to the styles in that group. Each one's parent is immediately to the left.

                   

                  As for the moving around in and out groups, I think it has been designed deliberately that way. You can move all kinds of stuff around, but only with one single reference point -- the list itself (moving up and down) or straight into another hierarchical object. I've seen this behaviour before, trying to move frames around from one layer to another on another page.

                   

                  Time well spent, after all.