Skip to main content
Known Participant
September 16, 2021
Question

Deleting unused Character and Paragraph Styles

  • September 16, 2021
  • 2 replies
  • 1272 views

Hi, do you have an up-to-date script (jsx) that removes all unused character styles and paragrap styles? I have searched the forum and every one I found is not working.

This topic has been closed for replies.

2 replies

Legend
July 28, 2022

There are some more ways to reference a paragraph style - the based-on style comes to mind, following style within the pargraph style, table- and object styles, indexing. Same for character styles, referenced via run-in styles such as grep/line etc., bullets and numbering and so forth.

I think there is a script that went thru "select unused styles" menu actions in the panel flyout menus, and would delete those instead.

Community Expert
September 16, 2021

Hi @KHKH,

Please share which script are you using and what does not work. Does it error out or does it not work as expected? It would be easier to fix an already written code rather than writing something from scratch.

-Manan

-Manan
KHKHAuthor
Known Participant
September 16, 2021

This is an example of a script that i tested

    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);
     }
}
alert(allStyles.length + " unused styles");

 

 And this is an error:

Community Expert
September 16, 2021

The code you shared was dealing only with paragraph styles and that too it was just showing an alert on the number of unused paragraph styles. I have made some edits it seems to work now, try the following

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[i] == 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.push(styles[i].name);
}
names = ArrayCompress(names);
var allStyles = doc.allParagraphStyles;
for(var i=allStyles.length-1;i>=0;i--){
     if(!IsInArray(allStyles[i].name,names)){
          try{
			  var nm = allStyles[i].name
			  allStyles[i].remove()
			  $.writeln("Removed style " + nm)
			 }catch(e){}
     }
}

P.S. :- Can you share the original discussion from where you picked this code. We should post links to eidts on that as well

-Manan

-Manan