Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Copying all GREP-styles from one paragraph style to annother

Tagged: 

Return to Member Forum

  • Author
    Posts
    • #66192
      Marcin Ploch
      Member

      Hello,
      I’m trying to find a simple way to copy all GREP-styles included in one paragraph style to others. Sometimes it is nessecary to just copy GREP-styles insted of inheritance.

      Can someone please help me?

      Best regards
      Marcin

    • #66193
      valedot
      Member
    • #66307
      John Korchok
      Member

      Here’s a script that will copy GREP styles, even if they’re nested in up to 2 levels of style groups. 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. The first 4 lines of comments explain how to use it:

      //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
      //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);//[1,2,3,4, 5]
      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);
      }
      }

    • #66337

      I’ve made an improvement to John’s script that presents a dialog box to select a “source” Paragraph style (instead of going into the code and typing the paragraph style out each time the script needs to be run). From there, the script runs as per John’s script:

      //https://forums.adobe.com/thread/681085 (Ariel’s script to select a paragraph style)
      //https://forums.adobe.com/message/5888276 (jkorchok2’s script to apply grep styles from a predefined paragraph style)
      //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
      myDoc = app.activeDocument;
      myStyle = SelectParagraph();

      function SelectParagraph(){
      mydialog = app.dialogs.add({name:”Source Paragraph Style”, canCancel:true});
      myStyles = myDoc.allParagraphStyles;
      var mystring = [];
      for (aa = 0; aa < myStyles.length; aa ++){
      mystring[aa] = myStyles[aa].name;
      if (myStyles[aa].parent.constructor.name == “ParagraphStyleGroup”) mystring[aa]+=” [“+myStyles[aa].parent.name+”]”;
      }
      with (mydialog.dialogColumns.add()){
      staticTexts.add({staticLabel:”Please choose:”});
      }
      with (mydialog.dialogColumns.add()){
      mymenu = dropdowns.add({stringList:mystring, selectedIndex:0});
      }
      if (mydialog.show()) myresult = myStyles[mymenu.selectedIndex]
      else myresult =-1;
      mydialog.destroy();
      return(myresult);
      }

      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 the 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 = myresult;
      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);
      }
      }

    • #67671
      Marcin Ploch
      Member

      Thank You John ad Colin for help. I’m tryingg to use the script but i found some syntax problems. I’m not familiar with scripting :(

      Adobe script edithor stops here:

      while(l–

      and I don’t know why :(

    • #67673
      Marcin Ploch
      Member

      If I delete “-” after “l” debugging goes OK, script starts and works til I try to choose destination style.

    • #67674
      Marcin Ploch
      Member

      Hi, my friend helped me to realize that web browser changed “–” to “-” long style.

      After that change script works PERFECT !!!

      Thanks a lot again :)))

    • #67698
      Masood Ahmad
      Participant

      Hi Marcinploch, would it be possible to upload the script to avoid any errors. I’m too getting errors.

    • #67717
      Masood Ahmad
      Participant

      Hi Marcinploch, could you please upload the zip version of the script. I’m on Mac and unable to fix the issue. I do not know about scripting. Getting numerous errors when trying to fix the script.

    • #67718
      Masood Ahmad
      Participant

      Sorry Marcinploch, I should have asked Colin Flashman for the script. Colin: could you please arrange the script in a zip format.

      • #67727
        Marcin Ploch
        Member

        Hi, write me please your e-mail. I’ll send you a zipped script which works on both platforms OSX and Win
        regards
        marcin

    • #67732

      If you are on a Mac, there is an applescript available that help copy Grep Styles to styles from a user-chosen paragraph style in a source doc to a user-chosen paragraph style in a destination document.

      It was done by Rick Gordon and is available here: https://github.com/rickgordon/Migrate-GREP-Styles

    • #67735
      Masood Ahmad
      Participant

      Thanks Jean, that was really very good. I’m indeed on MAC but I do use PC.

      @Marcinploch, Thanks for such an initiative, here is my email-id: masoodahmad@inbox.com

    • #68010
      Anonymous
      Inactive

      Thank you John and Colin.
      Both posted script needed a bit of work, but the 5 minute of fixing is well worth the tremendous amount of time the script is going to save my team and I. Thanks again!

Viewing 12 reply threads
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads