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:
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!
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 ==
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
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");
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
Copy link to clipboard
Copied
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!
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
Copy link to clipboard
Copied
Amazing!
Malcolm, you help me a lot, it's just I need
thanks so much!
DOWNLOAD FILES:
QUICK VIEW THE SCRIPT:
Before run:
After run: