4 Replies Latest reply on Jan 10, 2012 2:28 PM by KirkFu

    AppleScript Newbie - percentage increase

    KirkFu Level 1

      Hello All, I need to create an AppleScript for InDesign CS5 that will do the following:

       

      Find any number starting with the $ symbol in document

      Multiply that value by "X" percentage.

      Perform percentage increase on initial number value.

      Replace old number value with new value.

      Repeat loop until all values have been updated.

       

      Basically: $47.50 * 0.03 = $48.93 (This math across the document)

       

      Today is my first day mucking with AppleScript and any help will be greatly appreciated.

        • 1. Re: AppleScript Newbie - percentage increase
          [Jongware] Most Valuable Participant

          Do you have a particular reason to try AppleScript?

           

          (Javascript is also a quite useful language to learn, not just for InDesign but also for web pages, for example. ANDmit's platform-independent -- your scripts will also run on Windows machines. To be totally fair though, using AppleScript you can boss around just about everything on your entire Mac, something Javascript cannot.)

           

          (Reason to ask is because (1) if needed, I can do this from memory -- in Javascript, that is -- :-) and (2) there is a Javascript somewhere on InDesignSecrets.com that does something similar.)

          • 2. Re: AppleScript Newbie - percentage increase
            KirkFu Level 1

            Hello Jongware, I would appreciate any help at this point. If this is something you can do from memory, I'd be very greatful.

             

            Thank you.

            • 3. Re: AppleScript Newbie - percentage increase
              [Jongware] Most Valuable Participant

              Ah you call my bluff Well, I just typed this and only made two bad assumptions[*] as well as a typo [**] before I got it to work properly. It rounds everything to the penny and always writes two digits as well.

               

              (This is Javascript so save as "adjustBy103.jsx" in your User Script folder.)

               

              app.findGrepPreferences = null;
              app.findGrepPreferences.findWhat = "(?<=\\x{24})\\d+\\.?\\d*";
              foundList = app.activeDocument.findGrep(true);
              for (i=0; i<foundList.length; i++)
              {
                        val = parseFloat(foundList[i].contents)*1.03;
                        tempstring = String(Math.round(val*100)/100);
                        if (tempstring.indexOf('.') > -1)
                                  tempstring = (tempstring+"00").substr(0,tempstring.indexOf('.')+3);
                        else
                                  tempstring += ".00";
                        foundList[i].contents = tempstring;
              }
              

               

              [*] Math.round(val*100+0.5)/100 didn't work to get the last digit right ... Also initially got the GREP wrong as it picked up the $ sign as well.

              [**] parsefloat. someHow i canNeverRemember the camelCase for a couple of comMands.

              • 4. Re: AppleScript Newbie - percentage increase
                KirkFu Level 1

                You are a brilliant person. That did the trick. Please let me know how I can repay your kindness.

                 

                Best regards

                Kirk