10 Replies Latest reply on Nov 8, 2015 12:25 PM by Vamitul

    Need help with an arithmetic mystery

    Dick Kain

      I'm trying to save a numeric value in the environment and get it back into a UI  edit box and then perform arithmetic on it. I've tried so many permutations on all the possibilities that I can imagine, and none of them work. Here's one of them:


                      var gb = myRectangle.geometricBounds;

                      var myX1 = gb[1]

                      var myY1 = gb[2] + myOffset.valueOf()


      The arithmetic here doesn't work, unless the value of myOffset was set in an assignment statement, such as


                     myOffset = 9


      Even if this assigned value is sent through a UI edit box, it works properly. Fine!


      BUT if the value is saved. as in $.setenv(..) and later resotred as in $.getenv(..), it doesn't work. Under a different option (not detailed here for simplicity) I get a message saying that the system expected a real and received "9." If that isn't a real, I don't know what is! Anyhow, I tried lots of combinations, including the one above - but this one works fine if the value was set by an assignment, but when the value came from .getenv(..) and comes through the edit box and back out, it is treated as a string (despite the presence of "valueOf()), and the last line above performs a concatenation of the character string of the value with the character string value of the offset. What's the problem here????? And what's the fix to get around this nonsense?


      So I need help.. Thanks in advance.

        • 1. Re: Need help with an arithmetic mystery
          Vamitul Level 4

          if it received a "9" then that is a String!

          use parseFloat(myOffset)

          • 2. Re: Need help with an arithmetic mystery
            Dick Kain Level 1

            Thank you. That does work. But it seems that this should be a big error in the implementation. And, who ever heard of an operator parseFloat??? It is almost impossible to think of how storing a value by $.setenv(..) and restoring it later by $.getenv(..) would so modify the character of the value.

            • 3. Re: Need help with an arithmetic mystery
              Vamitul Level 4

              A) parseFloat, like parseInt are global functions, not operators.

              B) who ever heard of them? I guess almost anyone who ever picked up a book or tutorial about javascript.

              C) environmental variables for any operating system are strings and only strings. So, then, what would you expect such an environmental variable to return? A piece of pie? Speaking of which, why are you writing stuff into the os's environment? Unless you have some insanely solid reason to do so (and, in the 5+ years i've been dealing with scripting i never found one) do try and rethink your approach.

              D) I would say the big error in implementation lies in relying on the javascript automatic type conversion. It does make things easier, but it will come back to bite you when you least expect (been there, done that).

              • 4. Re: Need help with an arithmetic mystery
                Dick Kain Level 1

                Here are a few comments:

                1. I haven't really read a lot about Javascript, but I have made a number of scripts for Photoshop (see them at Behance.net/Dick-Kain). I did find paseFloat and ParseInt in my O'Reilly Pocket Reference, but I hadn't noticed them before, since I didn't rally read that book - just used it as a reference.


                2. I'm not really trying to write into the InDesign environment, but into the document's environment so that user's settings will be preserved when using the same script several times within the same document. Is there some way of doing that, without having the values show up in the UI?


                Anyhow, thanks for your help. It is a relief to get this resolved. I would appreciate any suggestions about #2 here.

                • 5. Re: Need help with an arithmetic mystery
                  Vamitul Level 4

                  Well, currently you are not writing into the Indesign environment, but into the whole operating system environment.

                  If you want to save settings to be available for all documents, use
                  app.insertLabel(key,value) -- both parameters are strings, so don't get spooked by the automatic conversion again.

                  If you want to save settings into a specific document


                  To retrieve the saved values:

                  app.extractLabel(key) (or document.extractLabel)


                  And, since i got started with this, let's say you have some more interesting settings than just a plain numeric value. Something like:

                  var settings={


                  There is a nice and simple "trick" to save this:

                  app.insertLabel('MyAwesomeScript_Settings',settings.toSource()); //remember that the labels are strings, so you need toSource() to convert the object to a string representation


                  Then, to retrieve it and use it:


                  • 6. Re: Need help with an arithmetic mystery
                    Dick Kain Level 1

                    Thank you so much!!!!!!!!


                    Now I have an elegant solution to my script design. It will really help my photo book project. ANd I have a friend who I will see in a few weeks who might be interested in this also....perhaps a few $$!



                    • 7. Re: Need help with an arithmetic mystery
                      Trevorׅ Adobe Community Professional

                      HI Vlad


                      I'm a bit tired here so might be missing something but why use parseFloat when one can just use + ?

                      var myY1 = gb[2] + +myOffset



                      • 8. Re: Need help with an arithmetic mystery
                        Vamitul Level 4

                        Hi Trev,

                        While i'm not completely averse to using operators for type conversions, i do tend to shy away for them.

                        That's because I like my code as explicit as possible. In 6 months time, when i'll have to debug some piece of code i can't even remember what was is for, and i see such a line, i'll stare at it and try to figure out if i was trying to increment some variable, or cast it to another type or what. Using .toString and parseInt/parseFloat takes a few more keystrokes but down the line it saves me a ton of headache.



                        • 9. Re: Need help with an arithmetic mystery
                          Dick Kain Level 1

                          I think that you didn't read all of my original posting. At the end, I commented that when the value has passed into and back out of the environment, the plus sign is interpreted as concatenation, so the simple plus sign doesn't work. It seems that even though I put in the valueOf(), the value was still a string, so the implementation converted the numeric first operand into a string and performed a concatenation. Surprise!!

                          • 10. Re: Need help with an arithmetic mystery
                            Vamitul Level 4

                            Please reread Trevor's post. You will notice there TWO "+" operators, one that is "normal" and does addition (or concatenation) and one that is unary (and will convert the string to a numeric value). But i think you just illustrated another reason why you should not use javascript shortcuts, especially when trying to help beginners out