11 Replies Latest reply on Dec 4, 2013 5:40 AM by Colin Flashman

    import grep styles to more than one paragraph style

    SilvioGabriel

      Hello

       

      First, I have to say that I don't have any experience in scripting. I don't create scripts, just use them.

       

      I have a script that imports grep styles from one paragraph style to another (in the same document). Everytime I use it, i have to change the script, inputting the source and target paragraph styles in this piece of the code:

       

      source = 'origem';

      target = 'alvo';

       

      What happens is that sometimes i need to do this lots of times (mostly when updating old projects). Is there any way to import the grep styles to more than one paragraph at the same time, listing multiple targets?

       

      thanks!

        • 1. Re: import grep styles to more than one paragraph style
          -hans- Level 4

          Hello SilvioGabriel,

           

          would have been fine to post your script to do the changes on the source ... ;-)

           

          Well, the following should show you a dialog to choose the paragraphstyles you want from active Document. It'll then start your own script using this input as argument.

          Please change in your script 'target = 'alvo';' to 'target = arguments[0]' and fill in the path to your script in first line of script below ...

           

           

          var pathToYourJavaScript = File('~/Desktop/SometestScript.jsx');//please fill correct ...
          
          var theDoc = app.activeDocument;  
          var pStyles = theDoc.allParagraphStyles;  
          var pStyleStringList = [];// listbox  
          
          fillPstyleStringList();  
          var getPstylesIndexinpStyles = selPStyle(pStyleStringList);//[1,2,3,4,  5]  
          var selPStylesByName = getSelPStyleNames(getPstylesIndexinpStyles);
          
          l =     selPStylesByName.length;
          while(l--){callDoScript([selPStylesByName[l]])}
          
              function fillPstyleStringList(){  
          for(var i = 0 ; i < pStyles.length; i++)  
          {  
          
            if(pStyles[i].parent.toString() === '[object ParagraphStyleGroup]')  
            {  
                pStyleStringList.push('Group: ' + pStyles[i].parent.name + ', Name: ' + pStyles[i].name);  
          }else{  
                    pStyleStringList.push('Name: ' + pStyles[i].name);  
          }  
                  }  
              }  
          
          
          
          function selPStyle (array)   
           {   
           var myWindow = new Window ("dialog", "Please select your targetPargraphstyles");   
            var myInputGroup = myWindow.add ("group");   
             var sel = myInputGroup.add ("listbox", [0, 0, 300, 300], array, {scrolling: true, multiselect: true});   
          
               var myButtonGroup = myWindow.add ("group");   
             myButtonGroup.add ("button", undefined, "OK");   
            myButtonGroup.add ("button", undefined, "Cancel");   
          
          if (myWindow.show() == 1){  
          var theSel =     sel.selection;  
          var tmpList = [];  
          for(var g = 0; g < theSel.length; g++)  
          {  
              tmpList.push(theSel[g].index)  
              }  
          return tmpList;  
              myWindow.close();  
                  }else{   
                  exit ();   
              }   
                }  
          
           function getSelPStyleNames(getPstylesIndexinpStyles){
               tmpArray = [];
          for(var j = 0; j < getPstylesIndexinpStyles.length; j++){
          var currTargetPStyleName =    pStyles[getPstylesIndexinpStyles[j]].name;
          tmpArray.push(currTargetPStyleName);
              }    
          return tmpArray;
                    }
          
           function callDoScript(pNameAsArray){
               app.doScript(pathToYourJavaScript, ScriptLanguage.JAVASCRIPT, pNameAsArray);
               }
          
          
          

           

          With NO GUARANTY ;-)

           

          Hans-Gerd Claßen

          • 2. Re: import grep styles to more than one paragraph style
            SilvioGabriel Level 1

            Thanks!

             

            It almost worked

             

            First I was having problems with the path. I was getting the 'file not found' error. When I spotted the error, then, it showed correctly the list of paragraph styles, I selected them, clicked ok and nothing happened. I clicked ok again, and this message showed:

             

            error21.jpg

             

            By the way, this was the script i mentioned before:

             

            source = 'origem';

            target = 'alvo';

             

            error = "";

            source = app.documents[0].paragraphStyles.item(source);

            if (!source.isValid) error = 'Source style does not exist';

            target = app.documents[0].paragraphStyles.item(target);

            if (!target.isValid) error += '\rTarget style does not exist';

            if (error != ""){alert (error); exit()}

             

            gs = source.nestedGrepStyles;

            for (i = 0; i < gs.length; i++)

                    target.nestedGrepStyles.add (gs[i].properties);

            • 3. Re: import grep styles to more than one paragraph style
              SilvioGabriel Level 1

              Hi Hans

               

              Reading again your post, I think I misunderstood what I had to do. I thought you had provided a new script that would do the job by itself, but it actually calls the script I already had (it's my old script's path I shoul fill in the script – I wasn't getting that), that will perform the task using the styles selected with your new script… is it right?

               

              Fixing this, that error message does'n show anymore, but it still doesn't work: this time I get a message that "target style does no exist", that appears 3 times (I think that's because I selected 3 target styles in the dialog box).

              • 4. Re: import grep styles to more than one paragraph style
                -hans- Level 4

                Hi,

                 

                have to say there was a mistake in my advice, as the argument has to be coerced to string in the targetscript ...

                 

                Pasted your code to to the end of my script:

                 

                var source = 'YourSourcepStyle';
                
                var theDoc = app.activeDocument;  
                var pStyles = theDoc.allParagraphStyles;  
                var pStyleStringList = [];// listbox  
                
                fillPstyleStringList();  
                var getPstylesIndexinpStyles = selPStyle(pStyleStringList);//[1,2,3,4,  5]  
                var selPStylesByName = getSelPStyleNames(getPstylesIndexinpStyles);
                
                l =     selPStylesByName.length;
                while(l--){callDoScript([selPStylesByName[l]])}
                
                    function fillPstyleStringList(){  
                for(var i = 0 ; i < pStyles.length; i++)  
                {  
                
                  if(pStyles[i].parent.toString() === '[object ParagraphStyleGroup]')  
                  {  
                      pStyleStringList.push('Group: ' + pStyles[i].parent.name + ', Name: ' + pStyles[i].name);  
                }else{  
                          pStyleStringList.push('Name: ' + pStyles[i].name);  
                }  
                        }  
                    }  
                
                
                
                function selPStyle (array)   
                 {   
                 var myWindow = new Window ("dialog", "Please select your targetPargraphstyles");   
                  var myInputGroup = myWindow.add ("group");   
                   var sel = myInputGroup.add ("listbox", [0, 0, 300, 300], array, {scrolling: true, multiselect: true});   
                
                     var myButtonGroup = myWindow.add ("group");   
                   myButtonGroup.add ("button", undefined, "OK");   
                  myButtonGroup.add ("button", undefined, "Cancel");   
                
                if (myWindow.show() == 1){  
                var theSel =     sel.selection;  
                var tmpList = [];  
                for(var g = 0; g < theSel.length; g++)  
                {  
                    tmpList.push(theSel[g].index)  
                    }  
                return tmpList;  
                    myWindow.close();  
                        }else{   
                        exit ();   
                    }   
                      }  
                
                 function getSelPStyleNames(getPstylesIndexinpStyles){
                     tmpArray = [];
                for(var j = 0; j < getPstylesIndexinpStyles.length; j++){
                var currTargetPStyleName =    pStyles[getPstylesIndexinpStyles[j]].name;
                tmpArray.push(currTargetPStyleName);
                    }    
                return tmpArray;
                          }
                
                 function callDoScript(pNameAsArray){
                     app.doScript(setGrepStyle, ScriptLanguage.JAVASCRIPT, pNameAsArray);
                     }
                
                function setGrepStyle(){
                var target = arguments[0].toString();
                error = "";
                
                source = theDoc.paragraphStyles.item(source);
                if (!source.isValid) error = 'Source style does not exist';
                
                var target = theDoc.paragraphStyles.itemByName(target);
                
                if (!target.isValid) error += '\rTarget style does not exist';
                
                if (error != ""){alert (error); exit()}
                
                
                
                gs = source.nestedGrepStyles;
                
                for (i = 0; i < gs.length; i++)
                        target.nestedGrepStyles.add (gs[i].properties);    
                        } 
                
                

                 

                Note:

                Your old scriptpart won't work on styles in groups

                and if styles are based on others it may run into errors too.

                 

                But as it worked for you until now, this should work.

                 

                Much place for improvements so ;-)

                 

                Hans-Gerd Claßen

                • 5. Re: import grep styles to more than one paragraph style
                  SilvioGabriel Level 1

                  Hi,

                   

                  Almost worked again. With this new script, all I had to do was change 'YourSourcepStyle' to my source paragraph style, right? Or am I missing something?

                   

                  What happens now is that it is copying the grep styles just to the last paragraph selected in the list (instead of copying to all selected), after showing this warning:

                   

                  Error Number: 30477

                  Error String: Invalid parameter.

                   

                  Engine: main

                  File: C:\Arquivos de programas\Adobe\Adobe InDesign CS5.5\Scripts\Scripts Panel\Samples\JavaScript\ImportGrepsPlus.jsx

                  Line: 63

                  Source:   app.doScript(setGrepStyle, ScriptLanguage.JAVASCRIPT, pNameAsArray);

                  • 6. Re: import grep styles to more than one paragraph style
                    -hans- Level 4

                    Sorry,

                     

                    the variable 'source' changed during execution in th old script. Didn't have a close enough look ...

                    Please replace:

                     

                    function setGrepStyle(){
                    var target = arguments[0].toString();
                    error = "";
                     basePStyle = theDoc.paragraphStyles.item(source);
                    if (!basePStyle.isValid) error = 'Source style does not exist';
                     var target = theDoc.paragraphStyles.itemByName(target);
                     if (!target.isValid) error += '\rTarget style does not exist';
                     if (error != ""){alert (error); exit()}
                      gs = basePStyle.nestedGrepStyles;
                     for (i = 0; i < gs.length; i++)
                            target.nestedGrepStyles.add (gs[i].properties);    
                            }
                    

                     

                    Works here, If this doesn't work on your machine i'll write a new part for adding the nestedGrepStyles ans ship off the doScript as it is not really needed ...

                    • 7. Re: import grep styles to more than one paragraph style
                      SilvioGabriel Level 1

                      worked perfectly!

                       

                      thanks!

                      • 8. Re: import grep styles to more than one paragraph style
                        jkorchok2

                        I have improved on this script to make it copy to styles in up to 2 levels of style groups:

                         

                        //This utility will copy GREP styles into styles that are nested in up to two levels of groups i.e. Main Style Group>Headings Group>Heading 1
                        //Copy the text below to a text editor like NotePad or TextEdit, then save as a file with a “.js” ending.
                        //Copy it into the Scripts Panel folder inside the Scripts folder inside the InDesign application folder. 
                        //Create a style at the lowest level of the Paragraph Styles i.e. NOT in a style group.
                        //Put all the GREP styles you wich to copy into this style.
                        //Type the style name between quotes in the line below. Then run the srcipt and select the styles to which you want to copy the GREP styles.
                        var source = 'GREPSourceStyle';
                        var theDoc = app.activeDocument;  
                        var pStyles = theDoc.allParagraphStyles;
                        var pStyleStringList = [];// listbox  
                        fillpStyleStringList();  
                        var getpStyleIndexinpStyles = selectpStyle(pStyleStringList);
                        var selectedpStylesByName = getSelectedpStyleNames(getpStyleIndexinpStyles);
                        l = selectedpStylesByName.length;
                        
                        while(l--){setGrepStyle([selectedpStylesByName[l][0]],[selectedpStylesByName[l][1]],[selectedpStylesByName[l][2]])}
                        
                        function fillpStyleStringList(){  
                            for(i = 0 ; i < pStyles.length; i++){  
                                if(pStyles[i].parent.parent.toString() === '[object ParagraphStyleGroup]'){
                                    pStyleStringList.push('Group: ' + pStyles[i].parent.parent.name + ', Subgroup: ' + pStyles[i].parent.name + ', Name: ' + pStyles[i].name);
                                }else if(pStyles[i].parent.toString() === '[object ParagraphStyleGroup]'){
                                    pStyleStringList.push('Subgroup: ' + pStyles[i].parent.name + ', Name: ' + pStyles[i].name);
                                }else{
                                    pStyleStringList.push('Name: ' + pStyles[i].name);
                                }
                            }
                        }
                        
                        function selectpStyle (array){
                            var myWindow = new Window ("dialog", "Please select your target paragraph styles.");
                            var myInputGroup = myWindow.add ("group");
                            var select = myInputGroup.add ("listbox", [0, 0, 300, 300], array, {scrolling: true, multiselect: true});
                            var myButtonGroup = myWindow.add ("group");
                            myButtonGroup.add ("button", undefined, "OK");
                            myButtonGroup.add ("button", undefined, "Cancel");
                            if (myWindow.show() == 1){
                                var mySelection = select.selection;
                                var tmpList = [];
                                for(g = 0; g < mySelection.length; g++){
                              tmpList.push(mySelection[g].index);
                                }
                                return tmpList;
                                myWindow.close();
                            }else{
                                exit();
                            }
                        }
                        
                        function getSelectedpStyleNames(getpStylesIndexinpStyles){
                          var currentTargetpStyleName;
                          var SelectedNameArray = new Array();
                          for(j = 0; j < getpStylesIndexinpStyles.length; j++){
                            var tempArray = new Array(2);
                            currentTargetpStyleName = pStyles[getpStyleIndexinpStyles[j]].name;
                            currentTargetpStyleSubgroup = pStyles[getpStyleIndexinpStyles[j]].parent.name;
                            currentTargetpStyleGroup = pStyles[getpStyleIndexinpStyles[j]].parent.parent.name;
                            tempArray[0] = currentTargetpStyleGroup;
                            tempArray[1] = currentTargetpStyleSubgroup;
                            tempArray[2] = currentTargetpStyleName;
                            SelectedNameArray[j] = tempArray;
                            }
                            return SelectedNameArray;
                        }
                        
                        function setGrepStyle(targetGroup, targetSubgroup, targetName){
                          var target
                            error = "";
                            basepStyle = theDoc.paragraphStyles.item(source);
                            if (!basepStyle.isValid) error = 'Source style does not exist';
                          if(targetGroup != "" && targetGroup != theDoc.name && targetGroup != app.name){
                            var temptarget = theDoc.paragraphStyleGroups.itemByName(targetGroup.toString());
                            target = temptarget.paragraphStyleGroups.itemByName(targetSubgroup.toString()).paragraphStyles.itemByName(targetName.toString());
                          }else if(targetSubgroup != "" && targetSubgroup != theDoc.name && targetSubgroup != app.name){
                            target = theDoc.paragraphStyleGroups.itemByName(targetSubgroup.toString()).paragraphStyles.itemByName(targetName.toString());
                          }else{
                            target = theDoc.paragraphStyles.itemByName(targetName.toString());
                          }
                            if (!target.isValid) error += '\rTarget style does not exist';
                            if (error != ""){alert (error); exit()}
                            gs = basepStyle.nestedGrepStyles;
                            for (i = 0; i < gs.length; i++){
                                target.nestedGrepStyles.add (gs[i].properties);
                            }
                        }
                        
                        • 9. Re: import grep styles to more than one paragraph style
                          Colin Flashman Adobe Community Professional

                          I love the idea of this script... something that's been a missing feature of InDesign - mapping grepstyles to other paragraph styles.

                           

                          Is there a way to improve on the script so that rather than finding only the style listed in the script, a UI appeared that allowed a source paragraph (that contained the GREP styles) to be selected, and then carry on with the script to select the target paragraph styles?

                           

                          Colin

                          • 11. Re: import grep styles to more than one paragraph style
                            Colin Flashman Adobe Community Professional

                            I've since modded poster 8's script so that instead of using one predefined paragraph style a la

                             

                            var source = 'GREPSourceStyle';

                             

                            I've used Ariel's function for selecting a Paragraph Style that creates a UI with a dropdown to select the paragraph style with the desired GREP styles to copy across. His function can be found here:

                             

                            http://forums.adobe.com/thread/681085

                             

                            The script then runs as per normal - another UI appears to select one or many paragraph styles... and after clicking OK, the selected paragraph styles then have the GREP styles applied to them.