Highlighted

Delete All Styles and Groups Script - Preserve All Formatting

Community Beginner ,
Aug 09, 2019

Copy link to clipboard

Copied

I found a InDesign script that deletes all paragraph styles, character styles, object styles and style group folders. The script preserves all formatting for the paragraph and character styles, but it doesn't preserve the object styles formatting. I'm not a scripter, but would appreciate any help with preserving the object style formatting issue. Thanks in advance.

Here is the script:

main();

function main(){

    var targetDoc = app.activeDocument;

    removeAllTypeStyles (targetDoc);

}

function removeAllTypeStyles (targetDoc) {

    //remove all paragraph styles

    for (var ps = targetDoc.allParagraphStyles.length - 1; ps >= 0; ps--) {

        if (targetDoc.allParagraphStyles[ps].name != "[No Paragraph Style]" && targetDoc.allParagraphStyles[ps].name != "[Basic Paragraph]") {

            targetDoc.allParagraphStyles[ps].remove (targetDoc.paragraphStyles.itemByName ("[No Paragraph Style]"));

        }

    }

    //remove all character styles

    for (var cs = targetDoc.allCharacterStyles.length - 1; cs >= 0; cs--) {

        if (targetDoc.allCharacterStyles[cs].name != "[None]") {

            targetDoc.allCharacterStyles[cs].remove (targetDoc.characterStyles.itemByName ("[None]"));

        }

    }

    //remove all object styles

    for (var os = targetDoc.allObjectStyles.length - 1; os >= 0; os--) {

        if (targetDoc.allObjectStyles[os].name != "[No Object Style]" && targetDoc.allObjectStyles[os].name != "[None]") {

            targetDoc.allObjectStyles[os].remove (targetDoc.objectStyles.itemByName ("[None]"));

        }

    }

    //remove all paragraph style groups

    for (var pg = targetDoc.paragraphStyleGroups.length - 1; pg >= 0; pg--) {

        targetDoc.paragraphStyleGroups[pg].remove ();

    }

    //remove all character style groups

    for (var cg = targetDoc.characterStyleGroups.length - 1; cg >= 0; cg--) {

        targetDoc.characterStyleGroups[cg].remove ();

    }

    //remove all object style groups

    for (var og = targetDoc.objectStyleGroups.length - 1; og >= 0; og--) {

        targetDoc.objectStyleGroups[og].remove ();

    }

}

Topics

Scripting

Views

447

Likes

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

Delete All Styles and Groups Script - Preserve All Formatting

Community Beginner ,
Aug 09, 2019

Copy link to clipboard

Copied

I found a InDesign script that deletes all paragraph styles, character styles, object styles and style group folders. The script preserves all formatting for the paragraph and character styles, but it doesn't preserve the object styles formatting. I'm not a scripter, but would appreciate any help with preserving the object style formatting issue. Thanks in advance.

Here is the script:

main();

function main(){

    var targetDoc = app.activeDocument;

    removeAllTypeStyles (targetDoc);

}

function removeAllTypeStyles (targetDoc) {

    //remove all paragraph styles

    for (var ps = targetDoc.allParagraphStyles.length - 1; ps >= 0; ps--) {

        if (targetDoc.allParagraphStyles[ps].name != "[No Paragraph Style]" && targetDoc.allParagraphStyles[ps].name != "[Basic Paragraph]") {

            targetDoc.allParagraphStyles[ps].remove (targetDoc.paragraphStyles.itemByName ("[No Paragraph Style]"));

        }

    }

    //remove all character styles

    for (var cs = targetDoc.allCharacterStyles.length - 1; cs >= 0; cs--) {

        if (targetDoc.allCharacterStyles[cs].name != "[None]") {

            targetDoc.allCharacterStyles[cs].remove (targetDoc.characterStyles.itemByName ("[None]"));

        }

    }

    //remove all object styles

    for (var os = targetDoc.allObjectStyles.length - 1; os >= 0; os--) {

        if (targetDoc.allObjectStyles[os].name != "[No Object Style]" && targetDoc.allObjectStyles[os].name != "[None]") {

            targetDoc.allObjectStyles[os].remove (targetDoc.objectStyles.itemByName ("[None]"));

        }

    }

    //remove all paragraph style groups

    for (var pg = targetDoc.paragraphStyleGroups.length - 1; pg >= 0; pg--) {

        targetDoc.paragraphStyleGroups[pg].remove ();

    }

    //remove all character style groups

    for (var cg = targetDoc.characterStyleGroups.length - 1; cg >= 0; cg--) {

        targetDoc.characterStyleGroups[cg].remove ();

    }

    //remove all object style groups

    for (var og = targetDoc.objectStyleGroups.length - 1; og >= 0; og--) {

        targetDoc.objectStyleGroups[og].remove ();

    }

}

Topics

Scripting

Views

448

Likes

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
Adobe Community Professional ,
Aug 09, 2019

Copy link to clipboard

Copied

Since paragraph styles could be part of an object style you could not expect that the object styles stay untouched if you remove all paragraph styles.

Regards,
Uwe

Likes

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
Reply
Loading...
Community Beginner ,
Aug 09, 2019

Copy link to clipboard

Copied

Uwe,

If I manually delete an object style that contains both paragraph and character styles, replace with [None], and check Preserve Formatting, it preserves all styles formatting. Is there a way to script this?

Thanks, Mike

Object Style-Preserve Formatting.png

Likes

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
Reply
Loading...
Community Beginner ,
Aug 09, 2019

Copy link to clipboard

Copied

I found another script that does delete paragraph, character and object styles that are NOT located in group folders and preserves the formatting. Is there code that can be added to remove styles that are contained IN group folders and preserve formatting?

Here is the 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 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();     

    };     

Likes

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
Reply
Loading...
Community Beginner ,
Jul 27, 2020

Copy link to clipboard

Copied

I got the above code also, from neal_derekl34934377 and with help from brianp311 figured out how to add code for table and cell styles (thank you, gentlemen!). But yes, the styles living in group folders still live on. The code in your OP works well but doesn't account for grep or object styling as you noticed.

 

If I can find a script that can 1) do what your first script does, and 2) maintain ALL styling (grep, objects included), that would be awesome. I want to be able to strip ALL styles (tables, cells, objects, paragraphs, and cells), whether in folders or not, and preserve the formatting.

 

Is there such a script out there?

 

Stay safe. Happy Monday.

Likes

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
Reply
Loading...
Contributor ,
Sep 20, 2019

Copy link to clipboard

Copied

Nice work.   What code would you add to have it look at a folder and batch process all indd's in that folder?

Likes

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
Reply
Loading...
Adobe Community Professional ,
Jul 27, 2020

Copy link to clipboard

Copied

belindat said: "Is there such a script out there?"

 

Hm. You could try a script by Hans Haesler hosted at hilfdirselbst.ch:

DefinitionenAufloesen

https://www.hilfdirselbst.org/scripts/downloads.php?groupIDX=&orderby=name&sort=ASC&search_query=&it...

 

Please come bach after testing if the script was doing what you want.

 

Thanks,
Uwe Laubender

( ACP )

Likes

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
Reply
Loading...
Community Beginner ,
Jul 27, 2020

Copy link to clipboard

Copied

Thank you, Uwe.

 

Sadly, the only thing Hans' script did was delete all the grep styles without preserving formatting. Is it possible my American MacBook is getting hung up on German scripting?

Likes

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
Reply
Loading...
Adobe Community Professional ,
Jul 28, 2020

Copy link to clipboard

Copied

Hi belindat,

ok. Hans' script has problems with "cascading" GREP styles.

GREP styles that are using character styles with the same set of properties, eg. fill color, on a similar range of characters.

 

Like the sample below where I apply one character style to all digits, fill color "blue" and another character styles that applies fill color "red" to a range of digits, two to five digits in a row:

 

01-Cascading-GREP-Styles.PNG

 

After running the script the GREP styles are removed from the paragraph style, but all digits are in blue:

 

02-AfterScriptRun.PNG

So the script did two things:

[1] First apply the character style for all digits in the range two to five digits

[2] Then apply the character style for all digits

That leaves all digits with color blue, because one character cannot have two or more character styles at the same time.

 

Ok. That means: Hans' script will not help you as a first step to get rid of the GREP styles.

 

Regards,
Uwe Laubender

( ACP )

Likes

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
Reply
Loading...
Community Beginner ,
Jul 28, 2020

Copy link to clipboard

Copied

Very possible. I have a lot of grep styles in this particular style, all rather important.

 

I re-examined the other scripts I've been sampling and they all have issues with cascading grep styles, in addition to their other faults.

 

So far, then, that elusive script is yet to be found.

 

Thank you, Uwe. I'm not a scripter and think that coding is magical at the least. We all have our superpowers. Scripting is not mine, lol.

Likes

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
Reply
Loading...
Adobe Community Professional ,
Jul 28, 2020

Copy link to clipboard

Copied

I think, we already discussed all this years ago in the old InDesign Scripting forum.

Alas, the thread from back then is not available anymore…

 

For scripters I can only throw some things into this discussion:

 

[1] Check in what exact order nested styles, nested line styles, GREP styles, applied character styles and simple style overrides are applied if they hit a single character. Try to follow the found rules of order and:

[2] Apply the character styles used with nested styles, nested line styles, GREP styles in the right order to the right characters. Note: textStyleRanges are no big help. The sample paragraph of my last post where two GREP styles are applied consists of only one single textStyleRange!

[3] If done break the link to all applied paragraph styles on all texts.

[4] Scripting only: Further apply character style [None] to all texts.

Formatting will be retained if you do this:

text.appliedCharacterStyle = app.documents[0].characterStyles[0];

 

Only then remove all custom character styles and paragraph styles.

 

Well, and even then you may find cases where you need to "merge" character styles and add new character styles temporarily so that the formatting can be maintained with an individual character.

 

Is that a perfect plan? Maybe not…

 

Regards,
Uwe Laubender

( ACP )

Likes

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
Reply
Loading...