Skip to main content
manuelb27477138
Inspiring
June 11, 2018
Answered

Delete unused Paragrap Styles in a Group Styles

  • June 11, 2018
  • 1 reply
  • 1109 views

Hi everyone!

I used this script and is working fine in my InDesign file. I get an alert like this:

But, please I need:

1. Delete the unused paragraph Styles only in my Group Style called APPENDIX

2. Really DELETE the paragraph Styles not only show an Alert.

here is my Group:

I try to do this:

// https://forums.adobe.com/thread/500399

ArrayCompress=function(array){ 

    var str = array.sort().join('\r')+'\r'   

    str = str.replace(/([^\r]+\r)(\1)+/g,'$1')   

    str = str.replace(/\r$/,'')   

    return str.split('\r')   

IsInArray = function (item,array){ 

   for(var i=0;i<array.length;i++){ 

       if(array == item){return true;} 

   } 

   return false; 

var doc = app.documents[0]; 

var styles = doc.stories.everyItem().paragraphStyleGroups.itemByName("APPENDIX").paragraphs.everyItem().appliedParagraphStyle; 

try{ 

styles = styles.concat(doc.stories.everyItem().footnotes.everyItem().paragraphs.everyItem().appliedParagraphStyle); 

}catch(e){} 

try{ 

styles = styles.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().appliedParagraphStyle); 

}catch(e){} 

var names = []; 

for(var i=0;i<styles.length;i++){ 

names = styles.name; 

names = ArrayCompress(names); 

var allStyles = doc.allParagraphStyles; 

for(var i=allStyles.length-1;i>=0;i--){ 

if(IsInArray(allStyles.name,names)){ 

    allStyles.splice(i,1); 

alert(allStyles.length + " unused styles"); 

I hope you can help me. thanks so much!

This topic has been closed for replies.
Correct answer BarlaeDC

sorry, I get the similar error:

ArrayCompress=function(array){

    var str = array.sort().join('\r')+'\r'  

    str = str.replace(/([^\r]+\r)(\1)+/g,'$1')  

    str = str.replace(/\r$/,'')  

    return str.split('\r')  

}

IsInArray = function (item,array){

  for(var i=0;i<array.length;i++){

      if(array == item){return true;}

  }

  return false;

}

var doc = app.documents[0];

var styles = doc.stories.everyItem().paragraphs.everyItem().appliedParagraphStyle;

try{

styles = styles.concat(doc.stories.everyItem().footnotes.everyItem().paragraphs.everyItem().appliedParagraphStyle);

}catch(e){}

try{

styles = styles.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().appliedParagraphStyle);

}catch(e){}

var names = [];

for(var i=0;i<styles.length;i++){

names = styles.name;

}

names = ArrayCompress(names);

// var allStyles = doc.allParagraphStyles;

var allStyles = doc.paragraphStyleGroups.itemByName("APPENDIX").paragraphStyles;

for(var i=allStyles.length-1;i>=0;i--){

if(IsInArray(allStyles.name,names)){

    allStyles.splice(i,1);

}

}

alert(allStyles.length + " unused styles");

If you want test, here is my Indesign: Download

Thanks!


HI,

I apologies I thought the solution was easier that it turned out to be, please find the code below which should do what is required.

ArrayCompress=function(array){ 

              var str = array.sort().join('\r')+'\r'   

              str = str.replace(/([^\r]+\r)(\1)+/g,'$1')   

              str = str.replace(/\r$/,'')   

              return str.split('\r')   

         } 

         IsInArray = function (item,array){ 

             for(var i=0;i<array.length;i++){ 

                 if(array == item){return true;} 

             } 

             return false; 

         } 

     

    var doc = app.documents[0]; 

    var styles = doc.stories.everyItem().paragraphs.everyItem().appliedParagraphStyle; 

    try{ 

         styles = styles.concat(doc.stories.everyItem().footnotes.everyItem().paragraphs.everyItem().appliedParagraphStyle); 

    }catch(e){} 

    try{ 

         styles = styles.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().appliedParagraphStyle); 

    }catch(e){} 

    var names = []; 

    for(var i=0;i<styles.length;i++){ 

         names = styles.name; 

    } 

    names = ArrayCompress(names); 

    var allStyles = doc.allParagraphStyles; 

    for(var i=allStyles.length-1;i>=0;i--){ 

         if(IsInArray(allStyles.name,names)){ allStyles.splice(i,1);   } 

    } 

   

    var appendixStyles = doc.paragraphStyleGroups.itemByName("APPENDIX").paragraphStyles;

    var stylesToRemove = [];

   

    for ( var k = 0; k < allStyles.length; k++)

    {

        for ( var l = 0; l < appendixStyles.length; l++)

        {

            if ( allStyles.name == appendixStyles.name)

            {

                stylesToRemove.push ( appendixStyles);

            }

        }

    }

for ( var p = stylesToRemove.length-1; p > 0; p--)

{

    stylesToRemove

.remove();

}

I have left the original code alone this time, to get a list of all the paragraph styles that are not needed, then I have compared that this to the list of paragraph styles in the paragraph style group you wanted, then finally I have removed the styles, I have done it like this as it can get really confusing to delete styles while still working out which styles you need.

Hope this helps.

Malcolm

1 reply

BarlaeDC
Community Expert
Community Expert
June 11, 2018

HI,

I think you need to adjust your approach, it is not possible to find unused paragraph styles that are in a specific paragraphStyleGroup, as when they are used the story does not track which group the style was in.

It would be better to change the code later on to only compare against the group you wish to remove from.

There is line 31 in the code you posted

var allStyles = doc.allParagraphStyles;

if you changed this to only get the paragraph styles in the specific group you wanted you would then compare against that list.

var allStyles = doc.paragraphStyleGroup.itemByName("APPENDIX").paragraphStyles;

Hope this helps

Malcolm

manuelb27477138
Inspiring
June 11, 2018

Hi Malcom,

sorry, I gert an error:

ArrayCompress=function(array){ 

    var str = array.sort().join('\r')+'\r'   

    str = str.replace(/([^\r]+\r)(\1)+/g,'$1')   

    str = str.replace(/\r$/,'')   

    return str.split('\r')   

IsInArray = function (item,array){ 

   for(var i=0;i<array.length;i++){ 

       if(array == item){return true;} 

   } 

   return false; 

var doc = app.documents[0]; 

var styles = doc.stories.everyItem().paragraphs.everyItem().appliedParagraphStyle; 

try{ 

styles = styles.concat(doc.stories.everyItem().footnotes.everyItem().paragraphs.everyItem().appliedParagraphStyle); 

}catch(e){} 

try{ 

styles = styles.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().appliedParagraphStyle); 

}catch(e){} 

var names = []; 

for(var i=0;i<styles.length;i++){ 

names = styles.name; 

names = ArrayCompress(names); 

// var allStyles = doc.allParagraphStyles; 

var allStyles = doc.paragraphStyleGroup.itemByName("APPENDIX").paragraphStyles;

for(var i=allStyles.length-1;i>=0;i--){ 

if(IsInArray(allStyles.name,names)){ 

    allStyles.splice(i,1); 

alert(allStyles.length + " unused styles");

BarlaeDC
Community Expert
Community Expert
June 11, 2018

Hi ,

Sorry there was a typo in my initial message, it should be

var allStyles = doc.paragraphStyleGroups.itemByName("APPENDIX").paragraphStyles;

notice the 's' at the end of paragraphStyleGroups

Regards

Malcolm