• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Delete unused Paragrap Styles in a Group Styles

Engaged ,
Jun 11, 2018 Jun 11, 2018

Copy link to clipboard

Copied

Hi everyone!

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

Captura de pantalla 2018-06-11 a las 13.45.15.png

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:

Captura de pantalla 2018-06-11 a las 13.49.19.png

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"); 

Captura de pantalla 2018-06-11 a las 13.43.22.png

I hope you can help me. thanks so much!

TOPICS
Scripting

Views

881

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 11, 2018 Jun 11, 2018

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 ==

...

Votes

Translate

Translate
Community Expert ,
Jun 11, 2018 Jun 11, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 11, 2018 Jun 11, 2018

Copy link to clipboard

Copied

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");

Captura de pantalla 2018-06-11 a las 15.39.21.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 11, 2018 Jun 11, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 11, 2018 Jun 11, 2018

Copy link to clipboard

Copied

sorry, I get the similar error:

Captura de pantalla 2018-06-11 a las 16.03.59.png

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 11, 2018 Jun 11, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 11, 2018 Jun 11, 2018

Copy link to clipboard

Copied

LATEST

Amazing!

Malcolm, you help me a lot, it's just I need

thanks so much!

DOWNLOAD FILES:

Download

QUICK VIEW THE SCRIPT:

Before run:

01.png

After run:

02.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines