9 Replies Latest reply on Jan 22, 2009 2:17 AM by Emanuele Radaelli

    $setenv and $getenv problem

    Emanuele Radaelli Level 1
      Hi, I'm tryin to set and get an environment var in InDesign CS3 (mac) using javascript.

      I made a script (to InDesign) to set a var:
      $.setenv("myVar", "my value");

      And another one (also to InDesign) to get the var:
      alert($.getenv("myVar"));

      While both are running I get the var value in the alert, but when I restart InDesign and launch the second script it doesn't works.
      What's the problem with this?
      I thought environment vars were supposed to live even if I close the application.
      Am I wrong?

      Thanks anticipately guys!
        • 1. Re: $setenv and $getenv problem
          Level 1
          > I thought environment vars were supposed to live even if I close the application.

          When you use setenv, it is modifying environment variables for that process and
          any subprocesses (shells, for instance) that may be launched by that process.
          When the app finishes, those settings disappear.

          -X
          • 2. Re: $setenv and $getenv problem
            Emanuele Radaelli Level 1
            Thanks for this tip xbytor.
            So, is there a way (apart from saving it in a file) to let a var live despite of its app closure?

            Thanks again.
            • 3. Re: $setenv and $getenv problem
              Level 1
              > So, is there a way (apart from saving it in a file) to let a var live despite of its app closure?
              >

              There may be some weird Windows Registry stunt you could pull on XP to do this.
              If it exists, it would modify the user's env vars in a persistent fashion; it
              would survive a reboot, for instance.

              On OS X, there may be a similar hook somewhere, but I doubt it. This kind of
              thing just doesn't fit the traditional Unix model of how env vars are handled,
              so it would likely require adding a setting to a plist file somewhere.

              Saving it to a file is probably your best/safest bet.

              -X
              • 4. Re: $setenv and $getenv problem
                Emanuele Radaelli Level 1
                Yeah I saw something about those Windows Registry while googling but I have to develop this script for a mac based client.
                Thanks again!
                • 5. Re: $setenv and $getenv problem
                  Harbs. Level 6
                  Since I've never used $.setenv or $.getenv and I might be missing
                  something simple... What's the advantage over just using simple global
                  variables?

                  --
                  Harbs
                  http://www.in-tools.com
                  • 6. Re: $setenv and $getenv problem
                    Level 1
                    > What's the advantage over just using simple global
                    > variables?

                    I would only consider them useful for setting up env vars for other applications
                    that you maybe invoking from ID/JS.

                    Beyond that, $.getenv is useful for things like $HOME, etc...

                    $.setenv wouldn't really serve any other purpose except when:
                    a) you have some code that already looks to $.getenv for info.
                    and
                    b) that info isn't in your env vars (for some reason) so $.setenv can insert
                    some defaults

                    That's kind of convoluted and not what I would consider good practice when
                    scripting Adobe apps.

                    -X
                    • 7. Re: $setenv and $getenv problem
                      Kasyan Servetsky Level 5
                      Hi cinematomico,

                      I use application’s insertLabel and extractLabel methods to store variables between ID sessions.
                      For example:
                      // store variable
                      
                      var myVariable =  "my value";
                      app.insertLabel("myVar", myVariable);

                      // restore variable
                      if (app.extractLabel("myVar") != ""){//if myVar has been set some value
                      var myVariable = app.extractLabel("myVar");
                      }
                      else {//if not set some default value
                      var myVariable = "some default value";
                      }

                      Kasyan
                      • 8. Re: $setenv and $getenv problem
                        Emanuele Radaelli Level 1
                        Hi Kasyan,
                        I didn't know the insertLabel and extractLabel instance...
                        I'm going to test this solution very soon.
                        Once again, thanks for the tip!
                        • 9. Re: $setenv and $getenv problem
                          Emanuele Radaelli Level 1
                          Thanks a lot guys, from a first test I have to say this solution works really well.
                          Just a few other questions...

                          Do you know where this data is stored (I mean, I think ID write it in a plist file...)?

                          If so, when I delete ID preferences (you know, it happens sometimes...) do I also delete these vars?

                          Is there any limit about var size (sometimes I have to store XML preferences data which are just enormous...)?

                          Cheers!