Skip to main content
Inspiring
January 9, 2013
Question

Possible to Remove, Then Load All Text Styles?

  • January 9, 2013
  • 1 reply
  • 1115 views

I'd like to remove all the Paragraph Styles and Character Styles from an InDesign CS4 document, and replace them with Paragraph Styles and Character Styles from another InDesign document. A search of this forum resulted in a similar discussion, http://forums.adobe.com/message/2919544 in which a script was utilized to remove all the text styles from a file.  The script is shown below.

The script is a timesaver as is, but I was wondering if it could mofified to "Load All Text Styles" from a file path/InDesign document that I specify?

Thanks in advance for any ideas on this.

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;

}

This topic has been closed for replies.

1 reply

Inspiring
January 9, 2013

Sample of how to load styles from indd-file to document or app:

#target InDesign 

var myFolder = Folder('C:/path/to/Folder/with/Indd-Files');

var allStyleFiles = myFolder.getFiles("*.indd");

var arrayOfStyleFileNames = [];

for(var myIndex = 0; myIndex < allStyleFiles.length; myIndex++ ){arrayOfStyleFileNames.push(allStyleFiles[myIndex].name)}

var selectedStyleFile = chooseStyleFile (arrayOfStyleFileNames)

function chooseStyleFile(arrayOfStyleFileNames){

        var myDialog = app.dialogs.add({name:"Optionale Formatdateien", canCancel:true});

    with(myDialog){

with(dialogColumns.add()){

        with(borderPanels.add()){

                with(dialogColumns.add()){

                    staticTexts.add({staticLabel:"Bitte eine Formatdatei wählen:"});

                }   

                with(dialogColumns.add()){

                    var pickedStyle = dropdowns.add({stringList:arrayOfStyleFileNames, selectedIndex:0});

                }

            }

        }

    }

    if(myDialog.show() == true){

                var pickedStyle = pickedStyle.selectedIndex;

                myDialog.destroy();

    return File(myFolder.toString() + '/' + arrayOfStyleFileNames[pickedStyle])

}

    else{

        myDialog.destroy()

        exit();

    }

  }

if(app.documents.length == 0){

     try{

        

                 RemoveIdStyles();

app.importStyles (ImportFormat.PARAGRAPH_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

app.importStyles (ImportFormat.CHARACTER_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

app.importStyles (ImportFormat.OBJECT_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

app.importStyles (ImportFormat.TABLE_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

app.importStyles (ImportFormat.CELL_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

app.importStyles (ImportFormat.TABLE_AND_CELL_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

app.loadSwatches (selectedStyleFile)

                 delEmptyParStyleGroups();

alert("Die Formate für " + selectedStyleFile.name + " wurden geladen.")

    }catch (e){

    alert ("Folgender Fehler trat auf: " + e)

    }        

     }

if(app.documents.length != 0){

     try{

app.activeDocument.importStyles (ImportFormat.PARAGRAPH_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

app.activeDocument.importStyles (ImportFormat.CHARACTER_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

app.activeDocument.importStyles (ImportFormat.OBJECT_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

app.activeDocument.importStyles (ImportFormat.TABLE_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

app.activeDocument.importStyles (ImportFormat.CELL_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

app.activeDocument.importStyles (ImportFormat.TABLE_AND_CELL_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

app.activeDocument.loadSwatches (selectedStyleFile)

                 delEmptyParStyleGroups();

alert("Die Formate für " + selectedStyleFile.name + " wurden nachgeladen.")

    }catch (e){

    alert ("Folgender Fehler trat auf: " + e)

    }        

     }

//Formate in InDesign löschen

function RemoveIdStyles(){

    var _pS = app.allParagraphStyles;

    for    (var i=0; i<_pS.length; i++) {

        try {

        _pS.remove(); }

     catch (e)            {

         }

        }

   

    var _cS = app.allCharacterStyles;

    for    (var i=0; i<_cS.length; i++) {

                try {

        _cS.remove();}

     catch (e)            {

         }

            }

       

var _pS = app.allObjectStyles;

    for    (var i=0; i<_pS.length; i++) {

        try {

        _pS.remove(); }

     catch (e)            {

         }

        }

   

        var _pS = app.swatches;

    for    (var i=1; i<_pS.length; i++) {

        try {

        _pS.remove();

        }

     catch (e)            {

         }

    }

    }

    //Leere Formatgruppen löschen

function delEmptyParStyleGroups(){

var pGroups = app.paragraphStyleGroups;

for(var z=pGroups.length -1; z >=0; z--){

if(pGroups.allParagraphStyles.length <= 0){

    pGroups.remove();

    }

        }

        }

kmc27Author
Inspiring
January 9, 2013

Thanks Hans

It looks like the script should first clear out all existing Character Styles and Paragraph Styles in the document, and then replace them with the ones from the selected "format" InDesign file. Is that correct? When I run the script, any unique Character Styles and Paragraph Styles are added to the existing Character Styles and Paragraph Styles, it is not replacing them.

I don't want to retain any of the document's existing Character Styles and Paragraph Styles. I want to clear them and replace them from the "format" InDesign file. Am I wrong in thinkng that the text styles should be cleared, then replaced?

Inspiring
January 9, 2013

It's been a example of how to load styles.

If your goal is to replace a bunch of p- and cStyles with some others you've got to write a list ... replace style x with styles y

In our workflow the stylestructure is almost the same for products with the same contents, so we can replace the formatting by loading new styles using the option 'GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE'