Skip to main content
Known Participant
August 20, 2008
Question

Delete Unused Paragraph Styles

  • August 20, 2008
  • 13 replies
  • 20271 views
Hi All,

I realize this one is probably pretty easy, but it has given me some grief these last few minutes. I'm trying to delete all unused paragraph styles in a document in InDesign CS. Here's the latest on what I have:

tell application "InDesign CS" to delete every unused paragraph style of document 1

I can't even get it to compile that. I've tried several variations, but all had the same result...it does not seem to like where I placed the word "unused", but I can't seem to figure out any other correct syntax.

Any help is greatly appreciated!
This topic has been closed for replies.

13 replies

Known Participant
August 28, 2008
Kasyan,

Thanks so much for that! I tweaked yours and here is the CS version:

tell application "InDesign CS"
set theDoc to active document
set myParagraphStyles to every paragraph style of theDoc
set theCount to count of myParagraphStyles
repeat with n from (count myParagraphStyles) to 2 by -1
set myCurrentStyle to item n of myParagraphStyles
my removeUnusedStyle(myCurrentStyle)
end repeat
end tell
on removeUnusedStyle(myStyle)
tell application "InDesign CS"
set find preferences to nothing
set change preferences to nothing
set theDoc to active document
search theDoc with find attributes {applied paragraph style:myStyle}
tell active document
set myFoundStyles to search theDoc with find attributes {find text:""}
if (count myFoundStyles) = 0 then
delete myStyle
end if
end tell
end tell
end removeUnusedStyle

I started my search at 2 instead of three so that it would check everything except for "[No Paragraph Style]", as Dave was saying before in the notes of his JavaScript version. This runs much faster than the one I wrote and it does get all the unused styles on the first pass. Thanks so much to all of you...this was exactly the help I needed!

hayden
Kasyan Servetsky
Legend
August 28, 2008
Hayden,

Here is the same script translated into AS. Its for CS3 I dont have CS any more so I cant help you with it. Find/Change is totally different in CS so check out your scripting reference and make necessary changes.

Kasyan
------------------------------------
tell application Adobe InDesign CS3
set theDoc to active document
set myParagraphStyles to every paragraph style of theDoc
set theCount to count of myParagraphStyles
repeat with n from (count myParagraphStyles) to 3 by -1
set myCurrentStyle to item n of myParagraphStyles
my RemoveUnusedStyle(myCurrentStyle)
end repeat
end tell

on RemoveUnusedStyle(myStyle)
tell application Adobe InDesign CS3
set find text preferences to nothing
set change text preferences to nothing
set applied paragraph style of find text preferences to myStyle
tell active document
set myFoundStyles to find text
if (count myFoundStyles) = 0 then
delete myStyle
end if
end tell
end tell
end RemoveUnusedStyle
Known Participant
August 27, 2008
Dave/Kasyan,

Thanks so much for your help. Unfortunately, my greatest exposure to JavaScript happened just the other day when I read Dave's blog. I tried to translate your concepts into AppleScript, but I had a bit of trouble.

However, I was able get enough of the concept from your script to point me in the right direction, I think. It looks like both of you used a handler, but I just used a repeat loop. For the record, here's what I have:

tell application "InDesign CS"
set theDoc to document 1
set myParagraphStyles to every paragraph style of theDoc
set theCount to count of myParagraphStyles
repeat 5 times
repeat with i from 1 to theCount
try
set theStyle to paragraph style i of theDoc
set find preferences to nothing
set change preferences to nothing
set foundStyle to search theDoc with find attributes {applied paragraph style:theStyle}
if foundStyle is equal to {} then delete theStyle
set find preferences to nothing
set change preferences to nothing
end try
end repeat
end repeat
end tell

You'll notice that I am repeating it 5 times. This is because every style does not delete on one pass...or two or three. I think this might be because some of my documents have "next style" set to "[Same Style]", but I'm not totally sure if that is why all unused styles are not deleting on the first pass. At any rate, after running this script five times, all of my unused styles are deleted from my document.

If Shane, Ole or any of the Applescript gurus have any improvements to my script that may help it run faster, please post them here. I'd love to hear your suggestions. For now, this script is functional enough, though perhaps a bit clumsy.

Anyway, thanks so much for your help, Dave!
Kasyan Servetsky
Legend
August 27, 2008
Hi Hayden,

Here I tried to implement Dave's idea. It's for CS3 and in JavaScript, but the same can be done in AS as well.

Kasyan
//--------------------------
var myDoc = app.activeDocument;

var myParStyles = myDoc.paragraphStyles;

for (i = myParStyles.length-1; i >= 2; i-- ){
removeUnusedStyle(myParStyles)
}

function removeUnusedStyle(myStyle) {
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedParagraphStyle = myStyle;
var myFoundStyles = myDoc.findText();
if (myFoundStyles == 0) {
myStyle.remove();
}
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
}
June 23, 2010

Hi Kasyan,

Here I am again.

I have modified little to your javascript for removing paragraph styles. It is working fine in CS3 and not in CS4. In CS4, in the ExtendScript Toolkit, it is not throwing any error message but nothing is happening in the indesign file. I hope some references might got changed in the later version. Here is the code:

var myDoc = app.activeDocument;
var myParStyles = myDoc.paragraphStyles;
var myCharStyles = myDoc.characterStyles;

for (i = myParStyles.length-1; i >= 2; i-- ){
   removeUnusedParaStyle(myParStyles)
}
for (i = myCharStyles.length-1; i >= 2; i-- ){
   removeUnusedCharStyle(myCharStyles)
}


function removeUnusedParaStyle(myPaStyle) {
   app.findTextPreferences = NothingEnum.nothing;
   app.changeTextPreferences = NothingEnum.nothing;
   app.findTextPreferences.appliedParagraphStyle = myPaStyle;
   var myFoundStyles = myDoc.findText();
      if (myFoundStyles == 0) {
         myPaStyle.remove();
      }
   app.findTextPreferences = NothingEnum.nothing;
   app.changeTextPreferences = NothingEnum.nothing;
}

function removeUnusedCharStyle(myChStyle) {
   app.findTextPreferences = NothingEnum.nothing;
   app.changeTextPreferences = NothingEnum.nothing;
   app.findTextPreferences.appliedCharacterStyle = myChStyle;
   var myFoundStyles = myDoc.findText();
      if (myFoundStyles == 0) {
         myChStyle.remove();
      }
   app.findTextPreferences = NothingEnum.nothing;
   app.changeTextPreferences = NothingEnum.nothing;
}

Could you please advise me what to do to make this script work in CS4? and also where can I find the relevent javascript reference for CS4 application?

Thanks in advance for your great help.

Regards,

Muthuraj. D

Kasyan Servetsky
Legend
June 23, 2010

Here are a few alternative versions of the script that were posted on the forum a while ago:

This script was written by Harbs

     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 one by Jongware

var allpars = app.activeDocument.stories.everyItem().paragraphs.everyItem().appliedParagraphS tyle;
var pStyles = app.activeDocument.paragraphStyles;
var parlist = new Array;

rmvPStyles();

function rmvPStyles(){
    for (a=0; a<allpars.length; a++)
        {
        add_me = true;
        for (b=0; b<parlist.length; b++)
            {
            if (parlist == allpars)
                {
                add_me = false;
                break;
                }
            }
        if (add_me)
            parlist.push (allpars
);
    }

    for (c=pStyles.length-1; c>=2; c--){
        myStyle=pStyles;
        if (styleInArray(myStyle, parlist)){
            pStyles.remove();
        }
    }
}

function styleInArray(ParaStyle, myArray){
    for (y in parlist){
        if (ParaStyle == parlist){
            return false;
            }
        }
    return true;
    }

alert ("DONE");

where can I find the relevent javascript reference for CS4 application?

It is here.

Kasyan

Inspiring
August 27, 2008
I don't know AppleScript well enough, but here it is in JavaScript. Note: This is for CS. In CS2, you'd have to skip over Basic Paragraph style, too. In CS3, the searching is completely different:
app.findPreferences = NothingEnum.nothing;

app.changePreferences = NothingEnum.nothing;

for (i=(myStyles.length - 1); i>0; i--)
{
//avoid 1 to ignore No Paragraph Style
//start at end to facilitate immediate deletion
app.findPreferences.appliedParagraphStyle = myStyles;
myFound = app.documents[0].search("",false,false);
if (myFound == "")
{
myStyles.remove();
}
}
Wow! Thats a very old script. I don't write them like that any more.

Dave
Known Participant
August 27, 2008
I guess what I am wondering is what the actual Applescript command is to search for and delete the unused style. As I said in my original post, I tried "tell application "InDesign CS" to delete every unused paragraph style of document 1" and several other versions of that, but I keep getting an error.

So how would I search for styles with a list length of zero? Then how would I delete them? If you don't mind, I'd love to know the actual syntax for it. I've tried my hand at it and scoured my Applescript books as well as the internet, but it seems to be beyond me.

I'm hearing you say it can be done, which is encouraging. I just need you to help me connect the dots here. Thanks, Dave.
Inspiring
August 26, 2008
Ah but you can search for them. If no text is set in them, you'll get a list of length zero. Ergo, it is unused. So delete it.

Dave
Known Participant
August 26, 2008
Dave/Shane/Ole,

In my document, none of my styles are using either "based on" or "next style." Everything is based on "[No Paragraph Style]". Given this information, what's the Applescript syntax for deleting a style with no based on and no text set to it? I didn't see in your blog where you were deleting the unused styles. I just saw that your script (in JavaScript) only alerts the user. I need to actually delete the style.

So to answer your original questions:
Is any text set in them? Use search for that.
--No text is set in the item, so I can't search for it that way.

Is any other style based on this one? If yes, does that mean it is in use? Up to you to decide.
--No other styles are based on any of the styles I am trying to delete.

Is this the "Next Style" for some other style? If yes, does that mean it is in use? Again, up to you.
--No, none of the styles I'm wanting to delete used next style for any other style.

I appreciate you sticking with me on this one. I'm determined to get this to work...it will save me a TON of time cleaning up my files! Thanks for all your help!

hayden
Inspiring
August 21, 2008
Yes. You just have to translate the concepts -- and most of the time that's pretty straightforward. (Aha, you're even using CS, so the search style I used is the same you'll need.)

The objects are the same -- it's just that the names are somewhat different in AppleScript and AppleScript has that mysterious, allegedly English-like syntax.

Dave
Known Participant
August 21, 2008
Thanks so much for your help, Dave. I'm still pretty new to Applescript and I'm not familiar with Javascript at all. I read your article on your blog, which was in Javascript. Is there a way of doing the same thing in Applescript?