-
1. Re: Remove all character styles unless in group 'X'
Mac_rk Aug 25, 2013 11:41 PM (in response to Asher31)try this...
var myDoc = app.activeDocument;
var myStyleGroup = myDoc.characterStyleGroups
for (var i = myStyleGroup.length-1;i>=0;i--){
alert (myStyleGroup[i].name)
if (myStyleGroup[i].name!='X'){
myStyleGroup[i].remove()
}
}
-
2. Re: Remove all character styles unless in group 'X'
pkahrel Aug 26, 2013 3:41 AM (in response to Mac_rk)Mac,
That doesn't remove styles that are not in a style group. You need to make a second pass to delete all character styles that aren't in a style group, or use something like this:
var cStyles = app.documents[0].allCharacterStyles; for (var i = cStyles.length-1; i > 0; i--){ if (cStyles[i].parent.constructor.name != 'CharacterStyleGroup' || cStyles[i].parent.name != 'X'){ cStyles[i].remove(app.documents[0].characterStyles[0]); } }Peter
-
3. Re: Remove all character styles unless in group 'X'
Vamitul Aug 26, 2013 4:26 AM (in response to pkahrel)peter, yours won't work either, because of the "||" in the condition. It will only remove the cstyles outside any groups
-
4. Re: Remove all character styles unless in group 'X'
pkahrel Aug 26, 2013 4:53 AM (in response to Vamitul)Not exactly. That script deletes all character styles except those in group 'X', but it doesn't delete emptied groups (which I see now that I tried with more than one character style group (). Something like this, then:
removeStyles (app.documents[0]); var groups = app.documents[0].characterStyleGroups.everyItem().getElements(); for (var i = groups.length-1; i > -1; i--){ if (groups[i].name != 'X'){ removeStyles (groups[i]); groups[i].remove(); } } function removeStyles (scope){ var cStyles = scope.characterStyles.everyItem().getElements(); for (var i = cStyles.length-1; i > 0; i--){ cStyles[i].remove(app.documents[0].characterStyles[0]); } }Peter



