11 Replies Latest reply on Aug 22, 2013 12:53 AM by Asher31

    Script to convert one style to another

    Asher31 Level 1

      Hi all,

      I don't really know much about scripts and indesign but from what i've seen it would be possible i just don't know how to go about it... so please let me know if i've done something wrong or missed somethin

       

      So the problem i'm trying solve is as follows:
      I am importing a bunch of text that is already styled, a lot of those styles just need to be replaced with styles i have already made in inDesign, is there a way to have a script that will automatically convert to the right style...

       

      For example:

      All the headings are imported with the style H2 but i want to change everything with that style to be the style ActiviyH1

       

      The reason i ask for a script is that i will need to do this a lot.

       

      any and all help would be appriciated even if it's just pointing me in the right direction. Cheers

        • 1. Re: Script to convert one style to another
          Mac_rk Level 2

          try below code

           

          var myDoc = app.activeDocument;

          var importedStyle = myDoc.paragraphStyles.item("H1");

          var ApplyStyle = myDoc.paragraphStyles.item("ActiviyH1");

          importedStyle.remove(ApplyStyle);

          • 2. Re: Script to convert one style to another
            Asher31 Level 1

            I recieved this message?

            scripts1.jpg

             

            Also was wondering if i wanted to add more styles to the script would i just duplicate the code and change the style names or would i need to do multiple scripts

             

            EDIT: And i completely forgot to say thank you Mac... sorry, so thank you

            • 3. Re: Script to convert one style to another
              Mac_rk Level 2

              // which style you went to find and replace  add like below metion 1st tow line

              replaceStyle("H1", "ActiviyH1");

              replaceStyle ("H2", "ActiviyH2")

               

               

              function replaceStyle(find, replace){

                  try{

              var myDoc = app.activeDocument;

              var importedStyle = myDoc.paragraphStyles.item(find);

              var ApplyStyle = myDoc.paragraphStyles.item(replace);

              importedStyle.remove(ApplyStyle);

                  }

              catch(e){

                  alert (e)

                  }

              }

              • 4. Re: Script to convert one style to another
                Asher31 Level 1

                I'm getting "referenceError: object invalid" which from some testing seems to be from the 'replaceStyle' but what it could be is that my styles are in folders, do i need to define the folder too? Also can the styles have spaces in them or will that break it.

                 

                Thank you so much for your help so far

                • 5. Re: Script to convert one style to another
                  Mac_rk Level 2

                  // which style you went to find and replace  add like below metion 1st tow line

                  replaceStyle("Old", "H1", "new", "ActiviyH1");

                  replaceStyle ("Old", "H2", "new","ActiviyH2")

                   

                   

                  function replaceStyle(GroupOld , find, GroupNew, replace){

                      try{

                  var myDoc = app.activeDocument;

                  importedStyle = myDoc.paragraphStyleGroups.item(GroupOld).paragraphStyles.item(find);

                  ApplyStyle = myDoc.paragraphStyleGroups.item(GroupNew).paragraphStyles.item(replace);

                  importedStyle.remove(ApplyStyle);

                      }

                  catch(e){

                      //alert (e)

                      }

                  }

                  • 6. Re: Script to convert one style to another
                    Asher31 Level 1

                    Still no luck unfortunately... getting this error now

                     

                    script2.jpg

                    • 7. Re: Script to convert one style to another
                      Mac_rk Level 2

                      sorry mis type

                       

                      replace this line " ApplyStyle = myDoc.paragraphStyleGroups.item(GroupNew).paragraphStyles.item(replac e);"

                       

                      with "ApplyStyle = myDoc.paragraphStyleGroups.item(GroupNew).paragraphStyles.item(replace);"

                       

                      space is there replac e

                      • 8. Re: Script to convert one style to another
                        Asher31 Level 1

                        Still not having any luck, although got rid of the error so i'm thinking that the issue now is the bits i need to change/adjust depending on my local settings.

                         

                        After playing with it i have a couple of questions that might help with the solution...

                         

                        1) In line "replaceStyle("Old", "H1", "new", "ActiviyH1");"
                        Are 'Old' and 'New' meant to be the names of the folders the styles are in? Do we HAVE to include them? If it's possible to do it without saying the folders (even if the styles are in folders/groups) that would be heaps easier as the old style are so deeply nested it would take a while.

                         

                        2) For the style names, can the name have space in it, as my names at the moment have spaces in them.

                         

                        Again thank you so much for taking the time to help me hope we can get this worked out.

                        • 9. Re: Script to convert one style to another
                          Mac_rk Level 2

                          can u send me ur paragarph style pallete snap shot

                          • 10. Re: Script to convert one style to another
                            Asher31 Level 1

                            Here you go

                             

                            styles3.jpg

                            • 11. Re: Script to convert one style to another
                              Asher31 Level 1

                              I played around a bit more and got it worked out, mainly thanks to you.. i think it is all sorted now except i have one question i thought you might be able to help with ...

                               

                              How do i show the top level directory for a style, so one that isn't in a group, but still keeping the dinfing group bit so i can also have ones in directories in the group...

                               

                              thanks in advance and thank you for all the past help, you rock