Skip to main content
Tete San
Participating Frequently
November 9, 2021
Answered

Delete imported styles (Paragraph and Character Styles from MS Word) - No Preserve Formatting

  • November 9, 2021
  • 3 replies
  • 1570 views

Hello everyone,

I need to create several files in indesign using text import from MS Word. (I don't use "Show import Options", just copy and paste with "All information" marked in Preferences > clipboard handling).

 

After applying some actions in GREP, I need to delete all the styles that appeared due to import, they are indicated by indesign with an icon as shown in the image below:

 


Is it possible to modify the script below to identify and delete these imported styles without keeping the formatting? Thanks in advance if anyone can help me modify it.

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

    }
	
} 
This topic has been closed for replies.
Correct answer m1b

Hi @Tete San, sure! Here's one way:

function main() {

    // you can set this to how you want it:
    var deleteImportedStylesFromAllDocuments = true;

    // get array of every document, or just the active document
    var docs = (deleteImportedStylesFromAllDocuments == true)
        ? app.documents
        : [app.activeDocument];

    if (docs.length == 0) return;

    // remove imported styles from every document in docs
    for (var i = 0; i < docs.length; i++) {
        removeImportedTypeStyles(docs[i]);
    }


    function removeImportedTypeStyles(targetDoc) {

        //remove all imported paragraph styles
        var paraStyles = targetDoc.allParagraphStyles,
            replacementParagraphStyle = targetDoc.paragraphStyles.item(0);
        for (var i = targetDoc.allParagraphStyles.length - 1; i >= 0; i--) {
            if (
                paraStyles[i].name != replacementParagraphStyle.name
                && paraStyles[i].imported == true
            ) {
                paraStyles[i].remove(replacementParagraphStyle);
            }
        }

        //remove imported character styles
        var charStyles = targetDoc.allCharacterStyles,
            replacementCharacterStyle = targetDoc.characterStyles.item(0);
        for (var i = targetDoc.allCharacterStyles.length - 1; i >= 0; i--) {
            if (
                charStyles[i].name != replacementCharacterStyle.name
                && charStyles[i].imported == true
            ) {
                charStyles[i].remove(replacementCharacterStyle);
            }
        }

    }


} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Delete Imported Type Styles');

 

- Mark

3 replies

James Gifford—NitroPress
Legend
November 10, 2021

You can also purge styles before you import from Word; perhaps the simplest approach in the future is not to bring all that baggage into the ID document at step 1, rather than having to resort to scripts to clean it all up. 🙂

Tete San
Tete SanAuthor
Participating Frequently
November 10, 2021

Hi NitroPress, yes better to purge first and not bring garbage to the document! However in my case I need the imported styles at some point because I need to see them and run GREP routines before deleting them. The annoying was really deleting them because there are many styles and many documents, which the script solves perfectly. Thanks

m1b
Community Expert
Community Expert
November 9, 2021

Hi @Tete San, here is your modified script that removed imported type styles. Just checks the 'imported' property.

- Mark

 

 

 

 

function main() {

    var targetDoc = app.activeDocument;
    removeImportedTypeStyles(targetDoc);

    function removeImportedTypeStyles(targetDoc) {

        //remove all imported paragraph styles
        var paraStyles = targetDoc.allParagraphStyles,
            replacementParagraphStyle = targetDoc.paragraphStyles.item(0);
        for (var i = targetDoc.allParagraphStyles.length - 1; i >= 0; i--) {
            if (paraStyles[i].name != replacementParagraphStyle.name
                && paraStyles[i].imported == true
            ) {
                paraStyles[i].remove(replacementParagraphStyle);
            }
        }

        //remove imported character styles
        var charStyles = targetDoc.allCharacterStyles,
            replacementCharacterStyle = targetDoc.characterStyles.item(0);
        for (var i = targetDoc.allCharacterStyles.length - 1; i >= 0; i--) {
            if (
                charStyles[i].name != replacementCharacterStyle.name
                && charStyles[i].imported == true
            ) {
                charStyles[i].remove(replacementCharacterStyle);
            }
        }

    }


} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Delete Imported Type Styles');

 

 

 

 

Tete San
Tete SanAuthor
Participating Frequently
November 10, 2021

Hi Mark, thanks for your attention, I have tested it and it's working perfectly. I also really liked the way you organized the code. Is it possible to apply this script to all documents opened in indesign as a loop?

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
November 10, 2021

Hi @Tete San, sure! Here's one way:

function main() {

    // you can set this to how you want it:
    var deleteImportedStylesFromAllDocuments = true;

    // get array of every document, or just the active document
    var docs = (deleteImportedStylesFromAllDocuments == true)
        ? app.documents
        : [app.activeDocument];

    if (docs.length == 0) return;

    // remove imported styles from every document in docs
    for (var i = 0; i < docs.length; i++) {
        removeImportedTypeStyles(docs[i]);
    }


    function removeImportedTypeStyles(targetDoc) {

        //remove all imported paragraph styles
        var paraStyles = targetDoc.allParagraphStyles,
            replacementParagraphStyle = targetDoc.paragraphStyles.item(0);
        for (var i = targetDoc.allParagraphStyles.length - 1; i >= 0; i--) {
            if (
                paraStyles[i].name != replacementParagraphStyle.name
                && paraStyles[i].imported == true
            ) {
                paraStyles[i].remove(replacementParagraphStyle);
            }
        }

        //remove imported character styles
        var charStyles = targetDoc.allCharacterStyles,
            replacementCharacterStyle = targetDoc.characterStyles.item(0);
        for (var i = targetDoc.allCharacterStyles.length - 1; i >= 0; i--) {
            if (
                charStyles[i].name != replacementCharacterStyle.name
                && charStyles[i].imported == true
            ) {
                charStyles[i].remove(replacementCharacterStyle);
            }
        }

    }


} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Delete Imported Type Styles');

 

- Mark

Legend
November 9, 2021

Hello @Tete San,

 

In Indesign if you go to File > Place > select on a Word document and show import options the below dialog will open giving you options for importing, Let us know if that helps.

Regards,

Mike

Tete San
Tete SanAuthor
Participating Frequently
November 10, 2021

Hi Mike, thanks for the reply. I already know these options. They don't work in my case as I get .doc and .docx from different versions of MS Word and different stakeholders. Imported styles become a huge mess.