Skip navigation
Currently Being Moderated

Delete Unused Paragraph Styles

Aug 20, 2008 8:10 AM

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!
 
Replies
  • Currently Being Moderated
    Aug 20, 2008 8:42 AM   in reply to (Hayden_Wreyford)
    There is no such thing as an unused paragraph style as far as the object model is concerned. You have to examine the paragraph styles and determine whether or not they're used.

    Is any text set in them? Use search for that.
    Is any other style based on this one? If yes, does that mean it is in use? Up to you to decide.
    Is this the "Next Style" for some other style? If yes, does that mean it is in use? Again, up to you.

    Dave
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 21, 2008 6:26 AM   in reply to (Hayden_Wreyford)
    You have to think of the UI as a large, efficient script that was written using the object model. Many of the things it does have a one-to-one relationship with what can be done in the object model, but equally, many of the things it does don't have such a relationship. You've picked one of the latter.

    But yes, you can write such a script. I have in the past. I even wrote a blog entry about it:

    http://jsid.blogspot.com/2005_08_01_archive.html

    Scroll down to "Style in Use" -- it was written three years ago, so it might not be completely up-to-date; it probably uses the older search mechanism, for example.

    Dave
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 21, 2008 7:44 AM   in reply to (Hayden_Wreyford)
    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
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 26, 2008 1:27 PM   in reply to (Hayden_Wreyford)
    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
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 27, 2008 5:53 AM   in reply to (Hayden_Wreyford)
    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[i];
      myFound = app.documents[0].search("",false,false);
      if (myFound == "")
      {
        myStyles[i].remove();
      }
    }
    Wow! Thats a very old script. I don't write them like that any more.

    Dave
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 27, 2008 6:12 AM   in reply to (Hayden_Wreyford)
    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[i])
    }

    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;
    }
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 28, 2008 6:33 AM   in reply to (Hayden_Wreyford)
    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
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 23, 2010 7:31 AM   in reply to Kasyan Servetsky

    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[i])
    }
    for (i = myCharStyles.length-1; i >= 2; i-- ){
       removeUnusedCharStyle(myCharStyles[i])
    }
     
    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 23, 2010 9:20 AM   in reply to Muthuraj Durairaj

    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[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[i] = 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)){
              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[b] == allpars[a])
                    {
                    add_me = false;
                    break;
                    }
                }
            if (add_me)
                parlist.push (allpars[a]);
        }
     
        for (c=pStyles.length-1; c>=2; c--){
            myStyle=pStyles[c];
            if (styleInArray(myStyle, parlist)){
                pStyles[c].remove();
            }
        }
    }
     
    function styleInArray(ParaStyle, myArray){
        for (y in parlist){
            if (ParaStyle == parlist[y]){
                return false;
                }
            }
        return true;
        }
     
    alert ("DONE");
     
    

     

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

    It is here.

     

    Kasyan

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 23, 2010 10:18 AM   in reply to Muthuraj Durairaj

    This works for me:

    var myDoc = app.activeDocument;
    var myParStyles = myDoc.paragraphStyles;
    var myCharStyles = myDoc.characterStyles;
     
    for (j = myParStyles.length-1; j >= 2; j-- ) {
       removeUnusedParaStyle(myParStyles[j]);
    }
     
    for (i = myCharStyles.length-1; i >= 1; i-- ) {
       removeUnusedCharStyle(myCharStyles[i]);
    }
     
    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;
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 24, 2010 1:37 AM   in reply to Kasyan Servetsky

    Hi Kasyan,

     

    Thanks again for your great help. Now the script is working fine in Indesign CS4 as well. I am in the process of creating a Javascript to cleanup a indesign document (remove unused paragraph and character styles, remove unused swatches, remove unused master pages and save as a copy to remove document history). I have got the right script for to remove the unused styles with your help.

     

    I have tried the following script to remove the unused swatches. It is working fine but the problem is, this script removes the available gradeient information in the indesign document and changes into black color.

     

     

    #target indesign
    var myIndesignDoc = app.activeDocument;
    var myUnusedSwatches = myIndesignDoc.unusedSwatches;
    for (var j=myIndesignDoc.unusedSwatches.length; j > 0; j--)
      myIndesignDoc.unusedSwatches[j-1].remove();
    


    I could not attach the screenshot image of the gradient here and I have sent you through mail (askoldich@yahoo.com).

     

    Please look into it and advise. Thanks a lot.

     

    Muthuraj. D

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)