0 Replies Latest reply on Sep 14, 2016 7:15 AM by ChocoBeast

    .replace() changes following paragraph style

    ChocoBeast

      I'm having an issue with a script. I have one version that converts numbers in a specific paragraph style to words, and it works just fine. I've gotten the corresponding version that converts words to numbers to work, but with one problem: it also changes the following paragraph to the same style, even though it doesn't do this in the other version. Am I missing something here?

       

      Note that I do plan to merge these into the same script - they're just separate right now for testing purposes.

       

      Numbers to Words (no issues)

       

      #target "InDesign"

       

       

      function getParagraphStyle(styleName, targetDoc) {

       

       

        if (targetDoc == null) {

        targetDoc = app.activeDocument;

        }

       

        if (targetDoc.paragraphStyles.itemByName(styleName) != null) {

        return targetDoc.paragraphStyles.itemByName(styleName);

        } else {

        var max = targetDoc.paragraphStyleGroups.count()

        for (var i = 0; i < max; i++) {

        if (targetDoc.paragraphStyleGroups[i].paragraphStyles.itemByName(styleName) != null) {

        return targetDoc.paragraphStyleGroups[i].paragraphStyles.itemByName(styleName);

        }

        }

        }

      }

       

       

      function main(options) {

        if (options[0] == "Numbers") {

        if (options[1] == "Words") {

        for (var i = 0; i < options[2].length; i++) {

        parseNumbers(options[2][i], getActiveStory(), ChangecaseMode.TITLECASE);

        }

        } else if(options[1] == "Roman Numerals") {

        // STILL NEED TO WRITE THIS FUNCTION

        alert("This doesn't work yet. Sorry.");

        }

        } else if (options[0] == "Words") {

        if (options[1] == "Numbers") {

        // STILL NEED TO WRITE THIS FUNCTION

        alert("This doesn't work yet. Sorry.");

        } else if (options[1] == "Roman Numerals") {

        // STILL NEED TO WRITE THIS FUNCTION

        alert("This doesn't work yet. Sorry.");

        }

        } else if (options[0] == "Roman Numerals") {

        if (options[1] == "Numbers") {

        // STILL NEED TO WRITE THIS FUNCTION

        alert("This doesn't work yet. Sorry.");

        } else if (options[1] == "Words") {

        // STILL NEED TO WRITE THIS FUNCTION

        alert("This doesn't work yet. Sorry.");

        }

        }

      }

       

       

      function parseNumbers(targetStyle, targetStory, capitalization) {

        var targets = itemsByParagraphStyle(targetStyle, targetStory);

        var tempString, newString;

       

        for (var i=targets.length-1; i > -1; i--) {

        try {

        tempString = parseInt (targets[i].contents.match(/\d+/)[0]);

        } catch(e) {

        continue;

        }

       

       

        newString = spellNumber(tempString);

        if (capitalization = ChangecaseMode.TITLECASE) {

        newString = capitalize(newString);

        } else if (capitalization = ChangecaseMode.UPPERCASE) {

        newString = newString.toUpperCase();

        } else if (capitalization = ChangecaseMode.LOWERCASE) {

        newString = newString.toLowerCase();

        }

        targets[i].contents = targets[i].contents.replace(tempString, newString);

        }

       

       

        return true;

      }

       

       

      function itemsByParagraphStyle(style, targetStory) {

        resetFindPrefs();

        app.findTextPreferences.appliedParagraphStyle = getParagraphStyle(style);

        return targetStory.findText();

      }

       

       

      function spellNumber(num) {

        var word;

        num = num.toString();

       

        if (num.length == 1) {

        word = wordForNumber(num);

        } else if(num.length == 2) {

        if (parseInt(num) > 19) {

        var num1 = num.charAt(0) +"0";

        var num2 = num.charAt(1);

        word = wordForNumber(num1) + "-" + wordForNumber(num2);

        } else if (parseInt(num) < 20) {

        word = wordForNumber(num);

        }

        }

        return word;

      }

       

       

      function wordForNumber(num) {

        var word;

       

        switch(num) {

        case "0":

        word = "";

        break;

        case "1":

        word = "one";

        break;

        case "2":

        word = "two";

        break;

        case "3":

        word = "three";

        break;

        case "4":

        word = "four";

        break;

        case "5":

        word = "five";

        break;

        case "6":

        word = "six";

        break;

        case "7":

        word = "seven";

        break;

        case "8":

        word = "eight";

        break;

        case "9":

        word = "nine";

        break;

        case "10":

        word  = "ten";

        break;

        case "11":

        word = "eleven";

        break;

        case "12":

        word = "twelve";

        break;

        case "13":

        word = "thirteen";

        break;

        case "14":

        word = "fourteen";

        break;

        case "15":

        word = "fifteen";

        break;

        case "16":

        word = "sixteen";

        break;

        case "17":

        word = "seventeen";

        break;

        case "18":

        word = "eighteen";

        break;

        case "19":

        word = "nineteen";

        break;

        case "20":

        word = "twenty";

        break;

        case "30":

        word = "thirty";

        break;

        case "40":

        word = "forty";

        break;

        case "50":

        word = "fifty";

        break;

        case "60":

        word = "sixty";

        break;

        case "70":

        word = "seventy";

        break;

        case "80":

        word = "eighty";

        break;

        case "90":

        word = "ninety";

        break;

        }

        return word;

      }

       

       

      function resetFindPrefs(){

          app.findTextPreferences = null;

          app.changeTextPreferences = null;

      }

       

       

      function capitalize(string) {

        var strings = string.split(" ");

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

        strings[i] = strings[i].charAt(0).toUpperCase() + strings[i].substr(1);

        }

        return strings.join(" ");

      }

       

       

      // BUILD USER INTERFACE

      function displayWindow() {

        var userOptions;

        var win = new Window("dialog", 'Adjust Number Style', [0,0,280,234], );

        with(win){

        win.ddFrom = add( "dropdownlist", [50,5,150,27], undefined, {items: ['Numbers','Words','Roman Numerals'], } );

        win.sText1 = add( "statictext", [5,7,50,27], 'Convert' );

        win.sText2 = add( "statictext", [155,7,170,27], 'to' );

        win.ddTo = add( "dropdownlist", [175,5,275,27], undefined, {items: ['Numbers','Words','Roman Numerals'], } );

        win.sText3 = add( "statictext", [5,32,110,52], 'for text styled as:' );

        win.tStyles = add( "listbox", [5,52,275,202], undefined, {multiselect: true} );

        win.butRun = add( "button", [130,207,200,229], 'Run', {name: 'OK', } );

        win.butCancel = add( "button", [205,207,275,229], 'Cancel', {name: 'Cancel', } );

        }

        win.center();

       

        // ADD EVENT HANDLERS

        win.onShow = function() {

        var styleCount, styleGroupCount, itemHolder;

       

        styleCount = app.activeDocument.paragraphStyles.count();

        for (var i = 0; i < styleCount; i++) {

        win.tStyles.add("item", app.activeDocument.paragraphStyles[i].name);

        }

        styleGroupCount = app.activeDocument.paragraphStyleGroups.count();

        for (var i = 0; i < styleGroupCount; i++) {

        itemHolder = win.tStyles.add("item", app.activeDocument.paragraphStyleGroups[i].name);

        itemHolder.enabled = false;

        styleCount = app.activeDocument.paragraphStyleGroups[i].paragraphStyles.count();

        for (var k = 0; k < styleCount; k++) {

        win.tStyles.add("item",app.activeDocument.paragraphStyleGroups[i].paragraphStyles[k].name );

        }

        }

       

       

        win.ddFrom.selection = win.ddFrom.items[0];

        win.ddTo.selection = win.ddTo.items[1];

        }

        win.butRun.onClick = function () {

        var from = win.ddFrom.selection.text;

        var to = win.ddTo.selection.text;

        var styles = [];

        var selectionCount;

       

        selectionCount = win.tStyles.selection.length;

        for (i = 0; i < selectionCount; i++) {

        styles.push(win.tStyles.selection[i].text);

        }

       

        userOptions = [from, to, styles];

        win.close(1);

        }

       

        if (win.show() == 1) {

        return userOptions;

        } else {

        return false;

        }

      }

       

       

      var userOptions = displayWindow();

      if (userOptions) main(userOptions);

       

      Words to Numbers (paragraph style issue)

      #target "InDesign"

       

       

      function getParagraphStyle(styleName, targetDoc) {

       

       

        if (targetDoc == null) {

        targetDoc = app.activeDocument;

        }

       

        if (targetDoc.paragraphStyles.itemByName(styleName) != null) {

        return targetDoc.paragraphStyles.itemByName(styleName);

        } else {

        var max = targetDoc.paragraphStyleGroups.count()

        for (var i = 0; i < max; i++) {

        if (targetDoc.paragraphStyleGroups[i].paragraphStyles.itemByName(styleName) != null) {

        return targetDoc.paragraphStyleGroups[i].paragraphStyles.itemByName(styleName);

        }

        }

        }

      }

       

       

      function main(options) {

        if (options[0] == "Numbers") {

        if (options[1] == "Words") {

        for (var i = 0; i < options[2].length; i++) {

        parseNumbers(options[2][i], getActiveStory(), ChangecaseMode.TITLECASE);

        }

        } else if(options[1] == "Roman Numerals") {

        // STILL NEED TO WRITE THIS FUNCTION

        alert("This doesn't work yet. Sorry.");

        }

        } else if (options[0] == "Words") {

        if (options[1] == "Numbers") {

        for (var i = 0; i < options[2].length; i++) {

        parseWords(options[2][i], getActiveStory());

                      }

        } else if (options[1] == "Roman Numerals") {

        // STILL NEED TO WRITE THIS FUNCTION

        alert("This doesn't work yet. Sorry.");

        }

        } else if (options[0] == "Roman Numerals") {

        if (options[1] == "Numbers") {

        // STILL NEED TO WRITE THIS FUNCTION

        alert("This doesn't work yet. Sorry.");

        } else if (options[1] == "Words") {

        // STILL NEED TO WRITE THIS FUNCTION

        alert("This doesn't work yet. Sorry.");

        }

        }

      }

       

       

      function parseWords(targetStyle, targetStory) {

        var targets = itemsByParagraphStyle(targetStyle, targetStory);

        var tempStr, tempPos, tempString, newString;

       

        for (var i=targets.length-1; i > -1; i--) {

        try {

        tempStr = targets[i].contents.toString();

        tempPos = tempStr.search(/\r/);

        tempString = tempStr.substring(0,tempPos);

        } catch(e) {

        continue;

        }

        newString = convertWord(tempString);

        targets[i].contents = targets[i].contents.replace(tempString, newString);

        }

        return true;

      }

       

       

      function itemsByParagraphStyle(style, targetStory) {

        resetFindPrefs();

        app.findTextPreferences.appliedParagraphStyle = getParagraphStyle(style);

        return targetStory.findText();

      }

       

       

      function convertWord(word) {

        var num;

        word = word.toString();

        var numberOfSpaces = (word.match(/ /g) || []).length;

        var numberOfHyphens = (word.match(/-/g) || []).length;

       

        if (numberOfSpaces == 0 && numberOfHyphens == 0) { // no space, no hyphen

        num = numberForWord(word);

              } else if (numberOfSpaces == 1 && numberOfHyphens == 0) { // one space, no hyphen

                  var tempParse = word.split(" ");

                  var word1 = tempParse[0];

                  var word2 = tempParse[1];

                  num = word1 + " " + numberForWord(word2);

              } else if (numberOfSpaces == 0 && numberOfHyphens == 1) { // no space, hyphen

                  var tempParse = word.split("-");

                  var word1 = tempParse[0];

                  var word2 = tempParse[1];

                  num = numberForWord(word1).substring(0,1) + numberForWord(word2);

              } else if (numberOfSpaces == 1 && numberOfHyphens == 1) { // one space, hyphen

                  var tempParse = word.split(" ");

                  var tempParse2 = tempParse[1].split("-");

                  var word1 = tempParse2[0];

                  var word2 = tempParse2[1];

                  num = tempParse[0] + " " + numberForWord(word1).substring(0,1) + numberForWord(word2);

              } else if (numberOfSpaces > 1 && numberOfHyphens == 1) { // multiple spaces, hyphen

                  var tempParse = word.lastIndexOf(" ");

                  var tempParseSpace = word.substring(0,tempParse);

                  var tempParseSpaceSplit = word.substring(tempParse);

                  var tempParse2 = tempParseSpaceSplit.split("-");

                  var word1 = tempParse2[0].substring(1);

                  var word2 = tempParse2[1];

                  num = tempParseSpace + " " + numberForWord(word1).substring(0,1) + numberForWord(word2);

              } else if (numberOfSpaces > 1 && numberOfHyphens == 0) { // multiple spaces, no hyphen

                  var tempParse = word.lastIndexOf(" ");

                  var tempParseSpaceSplit = word.substring(tempParse);

                  var word1 = word.substring(0,tempParse);

                  var word2 = tempParseSpaceSplit.substring(1);

                  num = word1 + " " + numberForWord(word2);

              }

          else {

              num = word;

              }

        return num;

      }

       

       

      function numberForWord(word) {

        var num;

       

        switch(word.toLowerCase()) {

        case "one":

        num = "1";

        break;

        case "two":

        num = "2";

        break;

        case "three":

        num = "3";

        break;

        case "four":

        num = "4";

        break;

        case "five":

        num = "5";

        break;

        case "six":

        num = "6";

        break;

        case "seven":

        num = "7";

        break;

        case "eight":

        num = "8";

        break;

        case "nine":

        num = "9";

        break;

        case "ten":

        num = "10";

        break;

        case "eleven":

        num = "11";

        break;

        case "twelve":

        num = "12";

        break;

        case "thirteen":

        num = "13";

        break;

        case "fourteen":

        num = "14";

        break;

        case "fifteen":

        num = "15";

        break;

        case "sixteen":

        num = "16";

        break;

        case "seventeen":

        num = "17";

        break;

        case "eighteen":

        num = "18";

        break;

        case "nineteen":

        num = "19";

        break;

        case "twenty":

        num = "20";

        break;

        case "thirty":

        num = "30";

        break;

        case "forty":

        num = "40";

        break;

        case "fifty":

        num = "50";

        break;

        case "sixty":

        num = "60";

        break;

        case "seventy":

        num = "70";

        break;

        case "eighty":

        num = "80";

        break;

        case "ninety":

        num = "90";

        break;

        default:

        num = word;

        break;

        }

        return num;

      }

       

       

      function resetFindPrefs(){

          app.findTextPreferences = null;

          app.changeTextPreferences = null;

      }

       

      // BUILD USER INTERFACE

      function displayWindow() {

        var userOptions;

        var win = new Window("dialog", 'Adjust Number Style', [0,0,280,234], );

        with(win){

        win.ddFrom = add( "dropdownlist", [50,5,150,27], undefined, {items: ['Numbers','Words','Roman Numerals'], } );

        win.sText1 = add( "statictext", [5,7,50,27], 'Convert' );

        win.sText2 = add( "statictext", [155,7,170,27], 'to' );

        win.ddTo = add( "dropdownlist", [175,5,275,27], undefined, {items: ['Numbers','Words','Roman Numerals'], } );

        win.sText3 = add( "statictext", [5,32,110,52], 'for text styled as:' );

        win.tStyles = add( "listbox", [5,52,275,202], undefined, {multiselect: true} );

        win.butRun = add( "button", [130,207,200,229], 'Run', {name: 'OK', } );

        win.butCancel = add( "button", [205,207,275,229], 'Cancel', {name: 'Cancel', } );

        }

        win.center();

       

        // ADD EVENT HANDLERS

        win.onShow = function() {

        var styleCount, styleGroupCount, itemHolder;

       

        styleCount = app.activeDocument.paragraphStyles.count();

        for (var i = 0; i < styleCount; i++) {

        win.tStyles.add("item", app.activeDocument.paragraphStyles[i].name);

        }

        styleGroupCount = app.activeDocument.paragraphStyleGroups.count();

        for (var i = 0; i < styleGroupCount; i++) {

        itemHolder = win.tStyles.add("item", app.activeDocument.paragraphStyleGroups[i].name);

        itemHolder.enabled = false;

        styleCount = app.activeDocument.paragraphStyleGroups[i].paragraphStyles.count();

        for (var k = 0; k < styleCount; k++) {

        win.tStyles.add("item",app.activeDocument.paragraphStyleGroups[i].paragraphStyles[k].name );

        }

        }

       

       

        win.ddFrom.selection = win.ddFrom.items[1];

        win.ddTo.selection = win.ddTo.items[0];

        }

        win.butRun.onClick = function () {

        var from = win.ddFrom.selection.text;

        var to = win.ddTo.selection.text;

        var styles = [];

        var selectionCount;

       

        selectionCount = win.tStyles.selection.length;

        for (i = 0; i < selectionCount; i++) {

        styles.push(win.tStyles.selection[i].text);

        }

       

        userOptions = [from, to, styles];

        win.close(1);

        }

       

        if (win.show() == 1) {

        return userOptions;

        } else {

        return false;

        }

      }

       

       

      var userOptions = displayWindow();

      if (userOptions) main(userOptions);