6 Replies Latest reply on Oct 8, 2018 10:26 AM by Laubender

    Move pstyle to specific location in group

    GusSoares Level 1

      Hi guys,

       

      I'm struggling with something that should be straightforward.
      I want to move a paragraph style to a position BEFORE or AFTER another paragraph style.

      The target location could be in a nested group or not.

       

      The documentation explicitly says that I can use a paragraph style as a reference, yet I keep getting "Invalid value for parameter 'reference' of method 'move'. Expected..., but received ParagraphStyle."

       

      So, to test, I want to move P3 to AFTER P2:

      movePStyle.PNG

       

      The code

      //IDs got from a loop via app.activeDocument.allParagraphStyles
      var P2 = app.activeDocument.paragraphStyles.itemByID(222);
      var P3 = app.activeDocument.paragraphStyles.itemByID(223);
      
      P3.move(LocationOptions.AFTER, P2);
      

       

       

      Quite stuck here : /

      Thanks in advance!

      Gus

        • 1. Re: Move pstyle to specific location in group
          Manan Joshi Level 4

          The reference of the P2 object that you obtain is not valid. The paragraphstyles present in a group are not present in the paragraphStyles collection. You can verify it by checking the value of P3.isValid, it will be false. The paragraphStyles collection contains just the paragraph styles that are without a group, so in your case it would be [No Paragraph Style], [Basic ParagraphStyle] and P3

           

          What you should do is get the valid reference for your paragraph style P2 by either iterating allParagraphStyle and matching the name or traversing the ParagraphStyleGroups which in turn can contain ParagraphStyleGroups or ParagraphStyles.

           

          -Manan

          • 2. Re: Move pstyle to specific location in group
            Laubender Adobe Community Professional & MVP

            Hi Manan,

            in the script's comment Gus is saying that the paragraph style's ID is from a loop on the array of allParagraphStyles.

            So basically this should work.

             

            Just tested this where I used the index of the allParagraphStyles array to identify the styles. Did not work, if the two styles are in different style groups. If the styles are in the same group, there's no problem.

             

            So the solution would be to move P3 to the same style group first and then to move after P2 if necessary.

             

            Regards,
            Uwe

            • 3. Re: Move pstyle to specific location in group
              Laubender Adobe Community Professional & MVP

              Something like that should work in this special case:

               

              var doc = app.documents[0];
              var pStyles = doc.allParagraphStyles;
              var p2, p3;
              
              for(var n=0;n<pStyles.length;n++)
              {
                  if( pStyles[n].name == "P2" ){ p2 = pStyles[n] };
                  if( pStyles[n].name == "P3" ){ p3 = pStyles[n] };
              };
              
              p2.move( LocationOptions.AT_BEGINNING, p3.parent );
              p2.move( LocationOptions.AFTER, p3 );
              

               

              One should test first if parent of p2 and parent of p3 is the same, then a simple move() like in line 12 of my snippet is possible right from the start.

               

              Regards,
              Uwe

              • 4. Re: Move pstyle to specific location in group
                Manan Joshi Level 4

                Hi Uwe,

                 

                Yes moving the paragraphStyles across groups using move results in a error and as you suggested. We would need to move it into the group first and then place it in the destination group as per the desired order.

                 

                However what i had suggested was based on the fact the the following code does not return the name of any paragraphStyle which is inside a group. But when i tried the itemByID api on paragraphStyles using a valid id of a style i get a valid pstyle, this is interesting. Do you also see this on your end? Any ideas why this is happening?

                app.activeDocument.paragraphStyles.everyItem().name
                

                 

                -Manan

                • 5. Re: Move pstyle to specific location in group
                  GusSoares Level 1

                  Hi Manan, Uwe,

                   

                  Thank so much for your reply.

                   

                  The 2-step process suggested by Uwe works perfectly, but it is confusing why we can't complete the whole task in single line – even more considering that ID's documentation explicitly tells us that it is possible...

                   

                  The collection from allParagraphStyles returns all styles regardless of their groups, while paragraphStyles.everyItem().getElements() returns only the styles on the base level. Now, the styles IDs are unique and should work as a target location. The same reasoning works for textFrames

                   

                  Many thanks!

                  Best,

                  • 6. Re: Move pstyle to specific location in group
                    Laubender Adobe Community Professional & MVP

                    Hi Manan,

                    with itemByID() you simply drill through all nested structures in your document.
                    You can reach everything. Even items in not active states of a multistate object.

                    That's the beauty of itemByID() :-)

                     

                    That the move() command is not working as we expect for nested paragraph styles is a different thing.

                    An idiosyncratic part of the DOM which is not documented and commented. We have to live with that…

                     

                    Best,
                    Uwe