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

Remove all Object Styles, Paragraph Styles, Character Styles

Contributor ,
Jul 11, 2018 Jul 11, 2018

Copy link to clipboard

Copied

Attempting to remove all Object Styles, Paragraph Styles, Character Styles while not affecting text frame attributes or text attributes currently present in the indd.

Basically keeping the visual presentation and position of all frames and it’s text content intact while removing all styles sheets in the document.

The below code successfully removes character styles and paragraph styles but:

• It gives an error on the paragraph style (yet removes them except the [Basic Paragraph] default style) (screenshot below).

• doesn’t remove object styles at all.

What I would like is to not get that paragraph error somehow?

In addition to obviously removing all Object Styles.

Any leads are greatly appreciated.

*Script below.

Error thrown regarding Paragraph Style default style issue.

Screen Shot 2018-07-11 at 4.55.32 PM.png

Current script.

#target InDesign   

    myDoc = app.activeDocument;   

       

    var chs1 = myDoc.characterStyles.length;   

    for(var j=chs1-1; j>0; j--) //j++ replaced here by j--   

    {   

      myDoc.characterStyles.remove();   

    }   

  var pghs1 = myDoc.paragraphStyles.length;   

    for(var j=pghs1-1; j>0; j--) //j++ replaced here by j--   

    {   

      myDoc.paragraphStyles.remove();   

    }   

  var objs1 = myDoc.objectStyles.length;   

    for(var j=objs1-1; j>0; j--) //j++ replaced here by j--   

    {   

      myDoc.objectStyles.remove();   

    }

TOPICS
Scripting

Views

3.0K

Translate

Translate

Report

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

correct answers 1 Correct answer

Contributor , Jul 11, 2018 Jul 11, 2018

for(var j=pghs1-1; j>1; j--)

you can not remove basic paragraph styles, because it is root style which is already define.

Votes

Translate

Translate
Contributor ,
Jul 11, 2018 Jul 11, 2018

Copy link to clipboard

Copied

for(var j=pghs1-1; j>1; j--)

you can not remove basic paragraph styles, because it is root style which is already define.

Votes

Translate

Translate

Report

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
Contributor ,
Jul 12, 2018 Jul 12, 2018

Copy link to clipboard

Copied

Yeah I gathered that.

But how would I get the script to not attempt to remove the default, root, Paragraph Style, while still removing every other non-root Paragraph Style?

Does it have something to do with the variable equation?:

for(var j=pghs1-1; j>1; j--)

I can play around with that, but I don’t completely understand it, for instance what do the semicoloons ; stand for when in between parenthesis in that way (i.e. when set up with for(var _________)?

*Also, any clues on how to cleanly remove all Object Styles (similar to Character Styles, which is the only part of the script that fully works cleanly)?

Thanks.

Votes

Translate

Translate

Report

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
Contributor ,
Jul 12, 2018 Jul 12, 2018

Copy link to clipboard

Copied

Put try catch over removing statement, when it is root style it won't  get removes and loop moves to next.

try{

       myDoc.paragraphStyles.remove();

    }

catch(e){}

code for removing object style is working fine in my system.

Regards,

      Payal

Votes

Translate

Translate

Report

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
Contributor ,
Jul 12, 2018 Jul 12, 2018

Copy link to clipboard

Copied

The below script works great also, even while the Paragraph Style part contains the i>0.

What’s the difference between this script and the one I previously posted? Do they in any way behave differently at in certain scenarios?

Cheers.

#target InDesign   

    myDoc = app.activeDocument;   

       

    var chs1 = myDoc.characterStyles.length;   

    for(var j=chs1-1; j>0; j--) //j++ replaced here by j--   

    {   

      myDoc.characterStyles.remove();   

    };   

  var pghs1 = myDoc.paragraphStyles.length;   

    for(var i=pghs1-1; i>0; i--) //j++ replaced here by j--   

  try  {   

      myDoc.paragraphStyles.remove();   

    }   

catch(e){} ;

  var objs1 = myDoc.objectStyles.length;   

    for(var n=objs1-1; n>0; n--) //j++ replaced here by j--   

    {   

      myDoc.objectStyles.remove();   

    };   

Votes

Translate

Translate

Report

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
Contributor ,
Jul 12, 2018 Jul 12, 2018

Copy link to clipboard

Copied

If you use i > 0 without using try catch it throws error and execution stops and If you use i > 1 program run without any error because that root style does not come in loop.

If you use i > 0 with try catch it again throws error but error is handled by catch method and execution does not stops while using try catch program run successfully.

Votes

Translate

Translate

Report

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
Contributor ,
Jul 12, 2018 Jul 12, 2018

Copy link to clipboard

Copied

*Palms on face* Ha.

It’s funny I just tested making all the variables distinct (i.e. j, i, n) and placing a “1” instead of a “0” at the i>0 part of the script, and that did it.

Removes everything including object styles, without giving that error on paragraph styles not being able to remove root style, etc.

Then when I came back here to post, I just noticed that‘s what you literally wrote in your post, duh, smh. Thanks a lot man. Really appreciate it.

So that number, is the style from the top of the list of styles? So whatever is the top style doesn’t get removed?

Then again, object styles contains 3 root, default styles, that also don’t get removed and that part of the script still contains a zero, “0”?

So what do the numbers really represent?

Final script below btw.

Screen Shot 2018-07-12 at 04.29.48.png

Screen Shot 2018-07-12 at 04.29.40.png

Screen Shot 2018-07-12 at 04.29.58.png

#target InDesign  

    myDoc = app.activeDocument;  

      

    var chs1 = myDoc.characterStyles.length;  

    for(var j=chs1-1; j>0; j--) //j++ replaced here by j--  

    {  

      myDoc.characterStyles.remove();  

    }  

  var pghs1 = myDoc.paragraphStyles.length;  

    for(var i=pghs1-1; i>1; i--) //j++ replaced here by j--  

    {  

      myDoc.paragraphStyles.remove();  

    }  

  var objs1 = myDoc.objectStyles.length;  

    for(var n=objs1-1; n>0; n--) //j++ replaced here by j--  

    {  

      myDoc.objectStyles.remove();  

    }

Votes

Translate

Translate

Report

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 Beginner ,
Jul 24, 2020 Jul 24, 2020

Copy link to clipboard

Copied

Hello from Texas!

 

I am dearly wishing to have a script that works to strip all the text, table, and object styles, but am not geek enough to incorporate your above code into a script file and get it to work.

 

I copy/pasted into a new file in ExtendScript Toolkit and saved it to the InDesign Scripts folder. It's showing up in my Scripts panel, but sadly I'm getting a JavaScript error #24 (Error String: myDoc.characterStyles.remove is not a function). 

 

Using a MacBook Pro running macOS Mojave.

 

What am I messing up?

Votes

Translate

Translate

Report

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 ,
Jul 24, 2020 Jul 24, 2020

Copy link to clipboard

Copied

The script is missing its index numbers in the for loop. I also added table styles. See if you can write cellStyles too! Try this: 

 

 

 

#target InDesign   

    myDoc = app.activeDocument;   
    var chs1 = myDoc.characterStyles.length;   
    for(var j=chs1-1; j>0; j--) //j++ replaced here by j--   
    {   
      try { myDoc.characterStyles[j].remove(); 
      } catch(e) {}
    }  

  var pghs1 = myDoc.paragraphStyles.length;   
    for(var i=pghs1-1; i>1; i--) //j++ replaced here by j--   
    {   
      try { myDoc.paragraphStyles[i].remove();   
      } catch(e) {}
    }   

  var objs1 = myDoc.objectStyles.length;   
    for(var n=objs1-1; n>0; n--) //j++ replaced here by j--   
    {   
      try { myDoc.objectStyles[n].remove();   
      } catch(e){}
    }

var tbls1 = myDoc.tableStyles.length;   
    for(var t=tbls1-1; t>0; t--) //j++ replaced here by j--   
    {   
      try { myDoc.tableStyles[t].remove();   
      } catch(e){}
    }

 

 

Votes

Translate

Translate

Report

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 Beginner ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

LATEST

This is sweet! Thanks so much. Amazingly, I WAS able to figure out how to code for cell styles.

 

One caveat I have to figure out: a lot of my styles are in style folders within the palettes, and they had to be moved up to the top level of hierarchy before the script worked on them.

 

I'll do some research or maybe end up asking for a script that does that, too.

 

Thanks again! Happy Monday, and stay safe.

 

Votes

Translate

Translate

Report

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 ,
Jul 12, 2018 Jul 12, 2018

Copy link to clipboard

Copied

Hi Neal,

also take a look at www.hilfdirselbst.ch where Hans Häsler did a similar job.

The discussion can be found here:

Alle Formate entfernen

https://www.hilfdirselbst.ch/gforum/gforum.cgi?post=543325#543325

Script "KeineFormate_502d.js" by Hans Haesler

http://www.fachhefte.ch/downloads.php?groupIDX=&orderby=name&sort=ASC&search_query=&itemIDX=&begin=&...

Regards,
Uwe

Votes

Translate

Translate

Report

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