10 Replies Latest reply on Oct 28, 2014 8:08 AM by Andymc7

    increase prices by a percentage in CS2

    jvolenceCMK

      I have this VERY old script written for me in 2004. 

       

      It worked then, but for some reason now when I run it, it updates the same number 3 times!  If I turn on "enable redraw", I can see this happen.  It updates the number to the correct value (but adds two decimal places to the end, then updates it two more times and then finally removes the decimals. 

       

      I tried removing the spots in the array dealing with whole numbers with decimals, because all my numbers do not have decimal places (except if an item is less than a whole dollar)

       

      Or, is this script outdated and could be re-written?

       

      Basically I have a few files full of prices with dollar signs scattered around in various stories (text boxes), and need to increase all of them by a percentage that is manually entered.

       

      Also, if the prices could go out to 5 places instead of 4, that might help in the future.

       

      Thanks!

       

      //PriceUpdateByStrings.js
      //An InDesign CS example script
      //
      //Updates values of all dollar amounts from $.01 to $9999.99.
      //
      //If no documents are open, then do nothing.
      if(app.documents.length !=0){
      //If the document does not contain any text, or if the document does not contain any paragraph styles, then  do nothing.
      if(app.activeDocument.stories.length != 0){
        myDisplayDialog();
      }
      }
      function myDisplayDialog(){
      var myDialog = app.dialogs.add({name:"UpdatePrices"});
      with(myDialog.dialogColumns.add()){
        with(dialogRows.add()){
         staticTexts.add({staticLabel:"Percentage Change:"});
         var myPercentageField = percentEditboxes.add({editValue:5});
        }
      }
      var myResult = myDialog.show();
      if(myResult == true){
        //Get the control settings from the dialog box.
        var myPercentage = myPercentageField.editValue;
        myDialog.destroy();
        myUpdatePrices(myPercentage);
      }
      else{
        myDialog.destroy();
      }
      }
      function myUpdatePrices(myPercentage){
      var myFoundItem, myPrice;
      //Set the percentage change to five percent.
      myPercentage = (myPercentage + 100)*.01;
      app.findPreferences = NothingEnum.nothing;
      app.changePreferences = NothingEnum.nothing;
      var myFindStrings = ["$^9^9^9^9", "$^9^9^9", "$^9^9", "$^9.^9^9", "$.^9^9", "$^w^9^9^9^9", "$^w^9^9^9", "$^w^9^9", "$^w^9.^9^9", "$^w.^9^9", "$^9^9^9^9.^9^9","$^9^9^9.^9^9","$^9^9.^9^9","$^w^9^9^9^9.^9^9","$^w^9^9^9.^9^9","$^w^9^9 .^9^9"]
      for(myFindStringCounter = 0; myFindStringCounter < myFindStrings.length; myFindStringCounter++){
        var myFoundItems = app.activeDocument.search(myFindStrings[myFindStringCounter]);
        if(myFoundItems.length != 0){
         for(myCounter = myFoundItems.length-1; myCounter >= 0; myCounter --){
          myFoundItem = myFoundItems[myCounter];
          myFoundItem = myFoundItem.characters.itemByRange(1, -1);
          //Convert the price.
          myFoundItem.contents = myMakePrice(myFoundItem.contents*myPercentage);   
         }
        }
      }
      }


      function myMakePrice(myValue){ 
      var myString = "" + Math.round(myValue * 100) / 100 
      var myIndex = myString.indexOf('.') 
      if (myIndex < 0){     return myString + ".00";    }  
      var myPrice = myString.substring(0, myIndex + 1) + myString.substring(myIndex + 1, myIndex + 3) 
      if (myIndex + 2 == myString.length){ 
        myPrice += "0"; 
      }
      var myCents = parseInt(myPrice.substr(myPrice.length-2,2));
      var myBucks = myPrice.substr(0, myPrice.length-3);
      if (myCents >= 50) {
        myBucks = parseInt(myBucks)+1;
      }
      myPrice = myBucks + ""; 
      return myPrice;
      }

        • 2. Re: increase prices by a percentage in CS2
          Trevorׅ Adobe Community Professional

          Before I checked out Kasyans link I wrote this.

          It's a lot shorter than the linked script but not as flexible.

           

          // http://forums.adobe.com/thread/1292700?tstart=0
          // by Trevor
          var doc = app.documents.length && app.activeDocument;
          if (doc) app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "Change Prices");
          else alert ("No document open !!");
          function main() {
              app.changeGrepPreferences = app.findGrepPreferences = null;
              app.findGrepPreferences.findWhat = "(?<=\\$)\\d+\\.?\\d*";
              var myNumberFinds = doc.findGrep(),
                    l = myNumberFinds.length, percentage;
              if (l) promtPercent ("Please enter the percentage increase (- for decrease)") else alert ("No finds");
              while (l--)   {
                      myNumberFinds[l].contents =  (Math.round (percentage * (myNumberFinds[l].contents) *100) / 100).toString();       
                      }
          function isNumber(n) {return !isNaN(parseFloat(n)) && isFinite(n)}; // isNumber function from http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/1830844#1830844
          function promtPercent (message) {
              if (isNumber (percentage = prompt (message, "", "Change document $ prices"))) percentage = (1 + (percentage / 100));
              else if (percentage) promtPercent("Please enter A VALID percentage increase (- for decrease)");
              }
          }  
          
          • 3. Re: increase prices by a percentage in CS2
            jvolenceCMK Level 1

            neither seem to work in CS2.

             

            first link gives an error on findGrepPereferences,

             

            and Trevor's script gives this error:

             

            ---------------------------
            Adobe InDesign
            ---------------------------
            JavaScript Error!

            Error Number: 2
            Error String: UndoModes is undefined
            Line: 4
            Source: if (doc) app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "Change Prices");
            ---------------------------
            OK  
            ---------------------------

             

            I am debating on purchasing CS6.

             

            If I can be sure that either posted script (or both) would work, then I will make the plunge.  I would also need to know if CS6 can save in an older version so that I don't have to buy the product for each workstation - only for people needing to run the script.

             

            Thanks!

            • 4. Re: increase prices by a percentage in CS2
              Trevorׅ Adobe Community Professional

              The scripts work on cs6 (haven't tested the other one)

              For cs2 you can try (I didn't notice the script was for cs2)

               

              // http://forums.adobe.com/thread/1292700?tstart=0
              // by Trevor
              var doc = app.documents.length && app.activeDocument;
              if (parseInt( app.version) > 6 && doc) app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "Change Prices");
              else if (doc) main();
              else alert ("No document open !!");
              function main() {
                  app.changeGrepPreferences = app.findGrepPreferences = null;
                  app.findGrepPreferences.findWhat = "(?<=\\$)\\d+\\.?\\d*";
                  var myNumberFinds = doc.findGrep(),
                        l = myNumberFinds.length, percentage;
                  if (l) promtPercent ("Please enter the percentage increase (- for decrease)") else alert ("No finds");
                  while (l--)   {
                          myNumberFinds[l].contents =  (Math.round (percentage * (myNumberFinds[l].contents) *100) / 100).toString();       
                          }
              function isNumber(n) {return !isNaN(parseFloat(n)) && isFinite(n)}; // isNumber function from http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/1830844#1830844
              function promtPercent (message) {
                  if (isNumber (percentage = prompt (message, "", "Change document $ prices"))) percentage = (1 + (percentage / 100));
                  else if (percentage) promtPercent("Please enter A VALID percentage increase (- for decrease)");
                  }
              }  
              
              • 5. Re: increase prices by a percentage in CS2
                jvolenceCMK Level 1

                That doesn't work either - CS2 doesn't seem to understand findGrepPereferences.

                • 6. Re: increase prices by a percentage in CS2
                  Trevorׅ Adobe Community Professional

                  Definitely time to upgrade then!

                  • 7. Re: increase prices by a percentage in CS2
                    Laubender Adobe Community Professional & MVP

                    @jvolenceCMK – GREP search/replace functionality was introduced in InDesign CS3.

                     

                    Alternatively one could use JavaScript RegEx feature on text strings (Story object contents, Table object contents). But you have to watch text formatting, if you want to change the length of a string!

                     

                    "doScript()" was introduced with InDesign CS4 ExtendScript, I think…

                     

                    Uwe

                    • 8. Re: increase prices by a percentage in CS2
                      Andymc7

                      Hey all, I know this is getting to be an older post, but I wanted to reply here instead of starting a new discussion...

                       

                      I have 3 separate questions about, or requests you might say for the above script:

                       

                      1. How can this code be modified so that the percentage increase is applied to a specific selection or story?

                       

                      2. Is there a way to keep a certain number of decimal places? The script works great for me, however, for example a price in a give table went from $120.00 to $126

                       

                      3. Can rounding be incorporated into the script? so that $45.00 ends up being $47.00 instead of $47.25?

                       

                      Now I know what you MAY be thinking, and yes I can just do these functions in excel first, paste as a link, update when there are price markups, create table styles, etc etc, but any time I work exclusively in ID, it's a bit easier for me. Especially when I'm only dealing with a few pricing tables.

                       

                      Thanks in advance!!

                       

                      -Andy

                      • 9. Re: increase prices by a percentage in CS2
                        Trevorׅ Adobe Community Professional

                        Are you also CS2!

                        • 10. Re: increase prices by a percentage in CS2
                          Andymc7 Level 1

                          Hi Trevor, thanks for the quick response..

                           

                          No I'm on CS6, Win7 64