Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Delete Unused Paragraph Styles

New Here ,
Aug 20, 2008 Aug 20, 2008
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!
TOPICS
Scripting
20.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 20, 2008 Aug 20, 2008
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 21, 2008 Aug 21, 2008
Thanks for your reply, Dave.

When I go to the Paragraph Styles palette in InDesign and click the right arrow and click "Select All Unused", then whatever styles that have not been applied to any text in the document are then highlighted for easy deletion. I'd really like to be able to automate that menu action with either a simple command (ie: delete all unused paragraph styles of document 1) or with a menu command (ie: get menu item 3 of menu...). I just have not been able to get the syntax correct to be able to do it.

I get what you are saying. It just seems that if I can click on the item with a mouse and have InDesign perform the action, I should also be able to automate that same action with a command of some type. What do you think?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 21, 2008 Aug 21, 2008
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 21, 2008 Aug 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?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 21, 2008 Aug 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 26, 2008 Aug 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 26, 2008 Aug 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 27, 2008 Aug 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 27, 2008 Aug 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 27, 2008 Aug 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;
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 23, 2010 Jun 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 23, 2010 Jun 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;
                }
            ...

);
    }

    for (c=pStyles.length-1; c>=2; c--){
        myStyle=pStyles;
        if (styleInArray(mySt...















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

It is here.

Kasyan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 23, 2010 Jun 23, 2010

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);
}

for (i = myCharStyles.length-1; i >= 1; 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;
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 24, 2010 Jun 24, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 07, 2015 Oct 07, 2015

Hi.

I'm using this script in my documents but character styles in bullets are removed too.

Have any way to preserve in these cases?

Screen Shot 2015-10-07 at 9.18.40 AM.png

Any body can help me?

Thanks and Hug.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2017 Apr 22, 2017

Sorry to resurrect this old thread, but along with Beto Correa above, I too have found a catastrophic issue with using Kasyan's script.

If a paragraph style has bullets applied that have a character style applied, Kasyan's script will delete the character style applied to the bullet, largely because the character style isn't used in areas of the document where the find/change can look (i.e. applied to a bullet from a paragraph style).

If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 22, 2017 Apr 22, 2017

Can we find the applied bullet character style name

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 22, 2017 Apr 22, 2017

Hi,

Did somebody fix this issue? 

[ Removing the unused character style also deletes the bullet  ]

(^/)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2017 Apr 22, 2017

One way would be to call the menu items as shown by Kasyan Servetsky: How to open any menu item by script in InDesign

A down and dirty script that can be achieved from cutting and pasting from Kasyan's site will yield a script that gives a false impression that it will work:

var myMenu = app.menus.item("$ID/CharStylePanelPopup");

var myMenuItem = myMenu.menuItems.item("Select All Unused");

var myMenuAction = myMenuItem.associatedMenuAction;

myMenuAction.invoke();

var myMenu = app.menus.item("$ID/CharStylePanelPopup");

var myMenuItem = myMenu.menuItems.item("Delete Style...");

var myMenuAction = myMenuItem.associatedMenuAction;

myMenuAction.invoke();

The above script will work if using the English version of ID that has a file that contains unused character styles.

However, the script will throw an error if there are no unused character styles (line 6 specifically). I'd imagine that it would throw an error at line 2 if running this script on a different locale.

I'm sure the above script would work for English users if lines 5-8 were called upon using an if statement based on the selection that was just made. The embarrassing thing is that I'm sure it's something very simple to code, but I personally don't know how I'd write that code.

If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 23, 2017 Apr 23, 2017

However, the script will throw an error if there are no unused character styles (line 6 specifically). I'd imagine that it would throw an error at line 2 if running this script on a different locale.

Hi Colin,

1. Try to enclose the code within a try-catch block so if there are no styles (an error occurs), either nothing happens, or the script gives a warning like this:

23-04-2017 15-10-42.png

Here's my version of your script:

try {

    var menu = app.menus.item("$ID/CharStylePanelPopup"); 

    var menuItem = menu.menuItems.item("$ID/SelectAllUnused");

    var menuAction = menuItem.associatedMenuAction; 

    menuAction.invoke(); 

    menu = app.menus.item("$ID/CharStylePanelPopup"); 

    menuItem = menu.menuItems.item("$ID/Delete Style...");

    if (!menuItem.isValid) {

        menuItem = menu.menuItems.item("$ID/Delete Styles..."); 

    }

    menuAction = menuItem.associatedMenuAction; 

    menuAction.invoke();

}

catch(err) {

    if (err.number == 53762) alert("There are no unused character styles in the document.", "Remove unused character styles script", false);

}

2. I guess (though I can't test it) the script will work in all locales if you use app.menus.item("$ID/SomeName");

If memory serves me right, the problem with other locales occurs when you use app.menus.item("SomeName"); or app.menus.itemByName("SomeName");

You can get the locale-independent string(s) from the internal string localization database using app.findKeyStrings("Some Menu Command"); like so:

23-04-2017 15-08-28.png

3. Note: the menu name changes depending on how many styles are selected: plural or singular.

23-04-2017 14-36-35.png

23-04-2017 14-34-24.png

Hope this helps.

— Kas

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 23, 2017 Apr 23, 2017

Almost there...

I found that if the Character Styles panel was not open, then the script would throw the alert, despite having character styles that could be deleted.

This means opening the Character Styles panel and closing it after the script is run.

The following would change the code:

app.panels.itemByName("$ID/Character Styles").visible = true; 

   try { 

        var menu = app.menus.item("$ID/CharStylePanelPopup");   

        var menuItem = menu.menuItems.item("$ID/SelectAllUnused");  

        var menuAction = menuItem.associatedMenuAction;   

        menuAction.invoke();   

        menu = app.menus.item("$ID/CharStylePanelPopup");   

        menuItem = menu.menuItems.item("$ID/Delete Style..."); 

        if (!menuItem.isValid) { 

            menuItem = menu.menuItems.item("$ID/Delete Styles...");   

        } 

        menuAction = menuItem.associatedMenuAction;   

        menuAction.invoke();  

    } 

    catch(err) { 

        if (err.number == 53762) alert("There are no unused character styles in the document.", "Remove unused character styles script", false); 

    }   

app.panels.itemByName("$ID/Character Styles").visible = false; 

I can get that to work successfully.

If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 16, 2017 Aug 16, 2017
LATEST

I just need a script that deletes all character styles and “Preserves Formatting” and “Apply to All” and “Replace with:[None]” (screenshot below).

Thanks in advance to anyone who can help.

Delete all character styles after “[None]”.

Screen Shot 2017-08-16 at 16.59.59.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 27, 2008 Aug 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!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 28, 2008 Aug 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines