5 Replies Latest reply on Jan 9, 2011 10:55 AM by Marijan Tompa [tomaxxi]

    Storing an object within an object

    thomas rollins Level 1

      insertLabel is great, but I have the need to store an object (not an InDesign object, but just a custom one) for later reference with a TextFrame. I thought about serializing the object, but javascript doesn't have a native serialize function, so before I embark down that route, is there an alternative method I could use to do this?



        • 1. Re: Storing an object within an object
          Marijan Tompa [tomaxxi] Level 4



          Can you maybe create custom Object, and convert it to source and save it to app or document label?


          var myObj = {};
          myObj['test'] = "Test String";
          myObj['value'] = 5;
          alert(typeof myObj + "\r" + myObj.toSource());
          app.activeDocument.insertLabel("myObject", myObj.toSource());
          var myObj = eval(app.activeDocument.extractLabel("myObject"));
          alert(typeof myObj + "\r" + myObj.toSource());


          More info here: http://bit.ly/gwpRAl


          Hope that helps.






          • 2. Re: Storing an object within an object
            thomas rollins Level 1

            wow, toSource is a new javascript function to me! in combination with eval, it did the trick!


            thanks Marijan!

            • 3. Re: Storing an object within an object
              Kasyan Servetsky Level 5

              Here is a snippet from my Package for Archive script.

              At start, the script I extracts the previously stored settings with eval() method, if the script is run for the 1st time -- creates default settings.

              function GetSettings() {
                  var mySettings = eval(app.extractLabel("Kas_PackageForArchive_Settings_ver3.0"));
                  if (mySettings == undefined) {
                      mySettings = { copyFontsCheckBox:false, copyGraphicsCheckBox:true, updateGraphicsCheckBox:true, includeHiddenLayers:true, ignorePreflightErrorsCheckBox:true, createReportCheckBox:false, createLogCheckBox:true };
                  return mySettings;


              Then a dialog box appears where a user selects settings and after he/she clicks OK, the settings are converted to string with toSource() and stored as string with insertLabel() method.

                  if (myShowDialog== 1) {
                      var myResult = {};
                      myResult.copyFontsCheckBox = myCopyFontsCheckBox.value;
                      myResult.copyGraphicsCheckBox = myCopyGraphicsCheckBox.value;
                      myResult.updateGraphicsCheckBox = myUpdateGraphicsCheckBox.value;
                      if (myInDesignVersion == 6) myResult.includeHiddenLayers = myIncludeHiddenLayersCheckBox.value;
                      myResult.ignorePreflightErrorsCheckBox = myIgnorePreflightErrorsCheckBox.value;
                      myResult.createReportCheckBox = myCreateReportCheckBox.value;
                      myResult.createLogCheckBox = myCreateLogCheckBox.value;
                      myResult.folder = myFolder;
                      app.insertLabel("Kas_PackageForArchive_Settings_ver3.0", myResult.toSource());
                  return myResult;

              In other words, the setting are stored in a custom object which can be serialized with toSource() and then restored with eval().



              1 person found this helpful
              • 4. Re: Storing an object within an object
                Marijan Tompa [tomaxxi] Level 4

                Great it helped you!


                Well, the thing is that we learn something new every day!






                • 5. Re: Storing an object within an object
                  thomas rollins Level 1

                  thanks Kasyan, that looks like a pretty useful script! If it works with my workflow, it's one thing to cross off the TODO list!