4 Replies Latest reply on Jun 5, 2009 12:21 PM by Ken Krugh

    Create paragraph style based on selection

    Ken Krugh Level 1

      Is there a way to create a paragraph style based on the selection in the same way the "New Paragraph Style" dialog box does without having to go through ALL of the elements of the style individually?

       

      Thanks

        • 1. Re: Create paragraph style based on selection
          Dave Saunders Level 4

          I was hoping (even expecting) that this would work:

           

          if (app.documents.length > 0 &&
              app.selection.length === 1 &&
                  app.selection[0].hasOwnProperty("baseline")) {
              var myDoc = app.documents[0];   
              var myPstyle = myDoc.paragraphStyles.add({
                  properties : app.selection[0].properties,
                  name : "myNewStyle"});
          }

           

          but the result is very disappointing, particularly with respect to the font details. Maybe you just can't use the properties property as part of an add() method call, because this works pretty well:

           

          if (app.documents.length > 0 &&
              app.selection.length === 1 &&
                  app.selection[0].hasOwnProperty("baseline")) {
              var myDoc = app.documents[0];   
              var myPstyle = myDoc.paragraphStyles.add({name : "myNewStyle"});
              myPstyle.properties = app.selection[0].properties;
          }

           

          Dave

          • 2. Re: Create paragraph style based on selection
            Ken Krugh Level 1

            I noticed the Properties somewhere else but, apparently, it isn't supported in VBA as I'm getting an error of "Object doesn't support this property or method."

             

            I THINK there is a way to run java from VBA. I'll have to check that out.

             

            Thanks for taking the time.

             

            Ken

            • 3. Re: Create paragraph style based on selection
              Olav Martin Kvern Level 3

              Hi Ken,

               

              Take Dave's code (thanks Dave) and put it into a string:

               

              Rem Note the "escaped" quotation marks.

              myString = "if (app.documents.length > 0 && app.selection.length === 1 && app.selection[0].hasOwnProperty(""baseline"")) {" & vbCr

              myString = myString & "var myDoc = app.documents[0];" & vbCR
              myString = myString & "var myPstyle = myDoc.paragraphStyles.add({name : ""myNewStyle""});" & vbCr
              myString = myString & "myPstyle.properties = app.selection[0].properties;" & vbCr

              myString = myString & "}" & vbCr

              Rem Now execute the string using "DoScript" (assumes that you have an

              Rem instance of InDesign "myInDesign"...

              myInDesign.DoScript myString, idScriptLanguage.idJavaScript

               

              That should do it.

               

              Thanks,

               

              Ole

              • 4. Re: Create paragraph style based on selection
                Ken Krugh Level 1

                I knew there was SOMEthing I saw a bit ago. Thanks for saving me the trouble of finding it again and giving me specifics on how it works!

                 

                This is now doing just what I need.

                 

                Thanks again, also, to Dave.

                 

                Ken