9 Replies Latest reply on Oct 12, 2010 2:15 AM by dennismacaburas

    JS CS3 script form word styles to indesign styles

    dennismacaburas

      I have search over the forums and can find a script the can change WORD styles (with a diskette sign) to Indesign paragraph styles (without the sign). Can any one help me on this?

       

      Thanks.

        • 1. Re: JS CS3 script form word styles to indesign styles
          Kasyan Servetsky Level 5

          var myDoc = app.activeDocument;
          removeFloppySymbols();

          function removeFloppySymbols(){
               for(var myCounter = myDoc.paragraphStyles.length - 1; myCounter > 0; myCounter--){
                    if (myDoc.paragraphStyles[myCounter].imported == true) {
                         var myParProperties = myDoc.paragraphStyles[myCounter].properties;
                         myDoc.paragraphStyles[myCounter].remove();
                         var myNewParStyle = myDoc.paragraphStyles.add();
                         myNewParStyle.properties = myParProperties;
                    }
               }

               for(var myCounter = myDoc.characterStyles.length - 1; myCounter > 0; myCounter--){
                    if (myDoc.characterStyles[myCounter].imported == true) {
                         var myCharProperties = myDoc.characterStyles[myCounter].properties;
                         myDoc.characterStyles[myCounter].remove();
                         var myNewCharStyle = myDoc.characterStyles.add();
                         myNewCharStyle.properties = myCharProperties;
                    }
               }
          }

          • 2. Re: JS CS3 script form word styles to indesign styles
            [Jongware] Most Valuable Participant

            Kasyan, isn't changing any setting enough?

             

            Just do something like

             

            myDoc.paragraphStyles[myCounter].underline ^= true;

            myDoc.paragraphStyles[myCounter].underline ^= true;

            on every style. You don't even have to check if it's imported or not.

            • 3. Re: JS CS3 script form word styles to indesign styles
              Kasyan Servetsky Level 5

              Hi Jongware,

               

              When you change an imported style In UI, the floppy icon disappears, but this doesn't happen if you change it by script.

               

              I liked you trick with bitwise XOR assignment operator -- very elegant -- I've never used such operators in my practice before. Every day I learn something new on this forum.

               

              Kasyan

              • 6. Re: JS CS3 script form word styles to indesign styles
                [Jongware] Most Valuable Participant
                Every day I learn something new on this forum.

                 

                Likewise! For example,

                 

                When you change an imported style In UI, the floppy icon disappears, but this doesn't happen if you change it by script.

                 

                (which of course I had to try out for myself. It seems you are right. Strange )

                • 7. Re: JS CS3 script form word styles to indesign styles
                  dennismacaburas Level 1

                  Kasyan,

                   

                  I downloaded ProtectLocalStyling InDesign.jsx from your site. And I found it usefull in our workflow but there are russian words that I don't understand. As I run the script I got errors. Do you have english version for this?

                   

                  Thanks,

                   

                  DennisM

                  • 8. Re: JS CS3 script form word styles to indesign styles
                    Kasyan Servetsky Level 5

                    Here I translated the script from Russian into English for you. BTW, it is based on a script written by Dave Saunders (thanks Dave for it) — I totally reworked it to my needs. You should replace paragraph style names, font names, find-search strings, etc. with your own stuff.

                     

                    Kasyan

                     

                    // Description: Script for InDesign CS3
                    // Version 4
                    // Removes local formatting of the text after importing it from Word transforming it into characters styles
                    // Removes non CMYK colors
                    // Removes imported styles
                    // Adds non-breaking space before some abbreviations in text. For example: В, У, гг., млрд.
                    // Removes superfluous. Finds-replaces some sequences of characters:  For example:"--" to "—"
                    // Removes hyperlinks
                    // Copyright © 2008 Kasyan Servetsky

                    #target indesign
                    if (app.documents.length == 0) {
                         alert("Open a document and try again.", "Error", true);
                    }
                    else {
                         app.activate();
                         var myDoc = app.activeDocument;
                         if (myDoc.selection.length == 1) {
                              if (myDoc.selection[0].constructor.name ==  "TextFrame") {
                              var myTextFrame = myDoc.selection[0];
                              }
                              else {
                                   alert("Select a TEXT BOX.", "Error", true);
                                   exit();
                              }
                         }
                         else {
                              alert("Select a single text block.", "Error", true);
                              exit();
                         }
                         if (!myDoc.saved) {
                              alert("This document has never been saved. Save it and try again.", "Error", true);
                              exit();
                         }
                    //--------------------------------- Progressbar ---------------------------------
                         var stop = 11;
                         var w = new Window ( 'window', 'Preparing the file', undefined, {closeButton:false, maximizeButton:false, minimizeButton:false} );
                         var pb = w.add ('progressbar', [0, 0, 350, 20], 0, stop);
                         var txt = w.add('statictext', undefined, 'Starting the process');
                         txt.alignment = "fill";
                         w.show();
                         pb.value = 1;
                         var myTxtReplace;
                         var myGrpReplace;
                         myReplaceStuff();
                         txt.text = "Finding-replacing text";
                         myProblems = 0;
                         myReplaceNonCMYK();
                         pb.value = 2;
                         txt.text = "Removing non CMYK colors";
                    //---------------------------------------------------------------------------------------
                         if (app.activeDocument.paragraphStyles.item("Body Text") == null) {
                              pb.value = 3;
                              txt.text = "Creating paragraph style \"Body Text\"";
                              var paraStyle = myDoc.paragraphStyles.add( {     basedOn:"[No Paragraph Style]",
                                                                                                                        name:"Body Text",
                                                                                                                        alignToBaseline:true,
                                                                                                                        firstLineIndent:2,
                                                                                                                        leftIndent:0,
                                                                                                                        rightIndent:0,
                                                                                                                        spaceBefore:0,
                                                                                                                        spaceAfter:0,
                                                                                                                        justification:Justification.LEFT_JUSTIFIED,
                                                                                                                        appliedFont:"TimesPalKasN\tRegular",
                                                                                                                        pointSize:8.5,
                                                                                                                        leading:10.6,
                                                                                                                        kerningMethod:"Metrics",
                                                                                                                        tracking:0,
                                                                                                                        hyphenateCapitalizedWords:true,
                                                                                                                        hyphenation:true,
                                                                                                                        hyphenateBeforeLast:2,
                                                                                                                        hyphenateAfterFirst:2,
                                                                                                                        hyphenateWordsLongerThan:5,
                                                                                                                        hyphenateLadderLimit:3,
                                                                                                                        hyphenationZone:12.7,
                                                                                                                        hyphenWeight:5,
                                                                                                                        appliedLanguage:"Russian",
                                                                                                                        fillColor:"Black"} );
                         }
                         else {
                              var paraStyle = myDoc.paragraphStyles.item("Body Text");
                              pb.value = 3;
                         }
                         removeImportedStyles();
                         pb.value = 4;
                         txt.text = "Removing imported styles";
                         protectLocalStyling();
                         pb.value = 5;
                         txt.text = "Saving local formatting";
                         myTextFrame.parentStory.texts.everyItem().paragraphs.everyItem().applyParagraphStyle(paraStyle, true);
                         pb.value = 6;
                         txt.text = "Applying paragraph style \"Body Text\"";
                         removeInTables();
                         pb.value = 7;
                         txt.text = "Processing tables";
                         myTextFrame.parentStory.texts.everyItem().paragraphs.everyItem().clearOverrides(OverrideType.all); // ***
                         pb.value = 8;
                         txt.text = "Removing local formatting";
                         replaceTxt();
                         pb.value = 9;
                         txt.text = "Replacing text";
                         replaceGrep();
                         pb.value = 10;
                         myRemoveHyperLinks();
                         pb.value = 11;
                         txt.text = "Removing hyperlinks";
                         w.hide();
                         
                         if (myProblems == 0) {
                              alert ("No problems were found.", "All done");
                         }
                         else {
                              alert ("Problems were found and corrected.", "All done");
                         }
                    }
                    //************************************** Functions *******************************************************
                    function protectLocalStyling() {
                        var myStyles = ["Italic","Bold", "Bold Italic"];
                        var myPosns = ["superscript","subscript"];
                        var myPosnEnums = [Position.superscript, Position.subscript];
                         var noCharStyle = myDoc.characterStyles[0];
                        for (var j = myStyles.length - 1; j >= 0; j--) {
                          var myCharStyle = myDoc.characterStyles.item(myStyles[j]);
                          if (myCharStyle == null) {
                            var myCharStyle = myDoc.characterStyles.add({name:myStyles[j], fontStyle:myStyles[j]});
                          }
                              app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
                              app.findTextPreferences.appliedCharacterStyle = noCharStyle;
                              app.findTextPreferences.fontStyle = myStyles[j];
                              app.changeTextPreferences.appliedCharacterStyle = myStyles[j];
                              myTextFrame.parentStory.changeText();
                        }
                        for (var j = myPosns.length - 1; j >= 0; j--) {
                          var myPosCharStyle = myDoc.characterStyles.item(myPosns[j]);
                          if (myPosCharStyle == null) {
                            var myPosCharStyle = myDoc.characterStyles.add({name:myPosns[j], position:myPosnEnums[j]});
                          }
                              app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
                              app.findTextPreferences.appliedCharacterStyle = noCharStyle;
                              app.findTextPreferences.position = Position[myPosns[j]];
                              app.changeTextPreferences.appliedCharacterStyle = myPosns[j];
                              myTextFrame.parentStory.changeText();
                         }
                         app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
                    }
                    //  Removing non CMYK colors
                    function myReplaceNonCMYK(){
                         for(var myCounter = myDoc.colors.length; myCounter > 0; myCounter--){
                              if (myDoc.colors[myCounter-1].name != "Registration" && myDoc.colors[myCounter-1].name != "Paper" && myDoc.colors[myCounter-1].name != "Cyan" && myDoc.colors[myCounter-1].name != "Magenta" && myDoc.colors[myCounter-1].name != "Yellow" && myDoc.colors[myCounter-1].name != "Black") {
                                   if ((myDoc.colors[myCounter-1].space != ColorSpace.cmyk)  || (myDoc.colors[myCounter-1].model != ColorModel.process)){
                                        myProblems = myProblems + 1;
                                        myDoc.colors[myCounter-1].remove ("Black")
                                   }
                              }
                         }
                    }
                    // Removing imported styles
                    function removeImportedStyles(){
                         var paraStyle = myDoc.paragraphStyles.item("Body Text");
                         var noneCharaStyle = myDoc.characterStyles.item("[None]");

                         for(var myCounter = myDoc.paragraphStyles.length-1; myCounter >= 2; myCounter--){
                              if (myDoc.paragraphStyles[myCounter].imported == true ) {
                                   myDoc.paragraphStyles[myCounter].remove(paraStyle);
                              }
                         }

                         for(var myCounter = myDoc.characterStyles.length-1; myCounter >= 2; myCounter--){
                              if (myDoc.characterStyles[myCounter].imported == true) {
                                   myDoc.characterStyles[myCounter].remove(noneCharaStyle);
                              }
                         }
                    }
                    // Processing tables
                    function removeInTables(){
                         if (myTextFrame.parentStory.texts.everyItem().tables.length > 0) {
                              if (myDoc.paragraphStyles.item("05. TABL_BODY") == null) {
                                   myTableStyle = myDoc.paragraphStyles.add({name:"05. TABL_BODY", appliedFont:"HeliosKas\tCondensed Regular", pointSize:8, leading:8});
                              }
                              else {myTableStyle = myDoc.paragraphStyles.item("05. TABL_BODY")}
                         myTextFrame.parentStory.texts.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().applyParagraphStyle(myTableStyle, true);
                         myTextFrame.parentStory.texts.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().clearOverrides(OverrideType.all);
                         }
                    }
                    // Find-replace pairs
                    function myReplaceStuff() {
                         myTxtReplace =     [ // TEXT
                                                            [" А ", " А^S"],
                                                            [" В ", " В^S"],
                                                            [" С ", " С^S"],
                                                            [" К ", " К^S"],
                                                            [" О ", " О^S"],
                                                            [" У ", " У^S"],
                                                            [" На ", " На^S"],
                                                            [" По ", " По^S"],
                                                            [" Из ", " Из^S"],
                                                            ["г-н ", "г-н^S"],
                                                            ["г-на ", "г-на^S"],
                                                            ["г-ну ", "г-ну^S"],
                                                            [" т", " ^Sт"],
                                                            [" г.", " ^Sг."],
                                                            [" гг.", " ^Sгг."],
                                                            [" грн.", " ^Sгрн."],
                                                            [" коп.", " ^Sкоп."],
                                                            [" тыс", " ^Sтыс"],
                                                            [" млн", " ^Sмлн"],
                                                            [" млрд", " ^Sмлрд"],
                                                            [" т.п.", " ^Sт.п."],
                                                            [" т.д.", " ^Sт.д."],
                                                            [" др.", " ^Sдр."],
                                                            [" км", " ^Sкм"],
                                                            ["см. стр.", "см.^Sстр."],
                                                            [" ^p", "^p"],
                                                            [" —", "^S—"],
                                                            [" —", "^S—"],
                                                            ["№ ", "№"],
                                                            ["$ ", "$"],
                                                            ["EUR ", "EUR"],
                                                            [" .", "."],
                                                            [" ,", ","],
                                                            [" …", "…"],
                                                            ["...", "…"],
                                                            [" ?", "?"],
                                                            [" )", ")"],
                                                            ["( ", "("],
                                                            ["«", "“"],
                                                            ["»", "”"],
                                                            ]
                                                            
                         myGrpReplace =     [ // GREP
                                                            ["[~m~>~f~|~S~s~<~/~.~3~4~% ]{2,}", " "], // Multiple Space to Single Space
                                                            ["--", "~_"] // Dash Dash to Em-dash
                                                            ]
                                                            // ["~b~b+", "\\r"], Multiple Return to Single Return
                    }
                    function replaceTxt() {
                         app.findTextPreferences = NothingEnum.nothing;
                         app.changeTextPreferences = NothingEnum.nothing;
                         app.findChangeTextOptions.wholeWord = false;
                         app.findChangeTextOptions.caseSensitive = true;
                         app.findChangeTextOptions.includeMasterPages = false;
                         for (i = 0; i <  myTxtReplace.length; i++) {
                              var myCurArray = myTxtReplace[i];
                              app.findTextPreferences.findWhat = myCurArray[0];
                              app.changeTextPreferences.changeTo = myCurArray[1];
                              myTextFrame.parentStory.changeText();
                         }
                         app.findTextPreferences = NothingEnum.nothing;
                         app.changeTextPreferences = NothingEnum.nothing;
                    }
                    function replaceGrep() {
                         app.findGrepPreferences = NothingEnum.nothing;
                         app.changeGrepPreferences = NothingEnum.nothing;
                         for (i = 0; i <  myGrpReplace.length; i++) {
                              var myCurArray = myGrpReplace[i];
                              app.findGrepPreferences.findWhat = myCurArray[0];
                              app.changeGrepPreferences.changeTo = myCurArray[1];
                              myTextFrame.parentStory.changeGrep();
                         }
                         app.findGrepPreferences = NothingEnum.nothing;
                         app.changeGrepPreferences = NothingEnum.nothing;
                    }
                    function myRemoveHyperLinks(){
                         if (myDoc.hyperlinks.length > 0){
                              for(var myCounter = myDoc.hyperlinks.length; myCounter > 0 ; myCounter--){
                                   myProblems++;
                                   myDoc.hyperlinks[myCounter - 1].remove();
                              }
                         }
                    }

                    • 9. Re: JS CS3 script form word styles to indesign styles
                      dennismacaburas Level 1

                      Thanks Kasyan.

                      I can now work on this script.

                      Thank you again.