3 Replies Latest reply on Sep 10, 2010 6:04 AM by Chris Cornelis

    Add nodes to XML

    Chris Cornelis

      My script asks different types of input to the user and I want to store that info in a settings xml file. That way the dialogs can fall back on previously entered data.

      I found out how to read an XML file and search for specific values (xpath), but I also want to add new "settings" to the file. Is there an easy way to do this with InDesign javascript? The code in bold is pseudocode that I want to replace by some real code.

       

       

          var settingsFile = new File(kSettingsFileName);

          if (settingsFile.open("r")) {

              var settingsXML = new XML(settingsFile.read());

              settingsFile.close();

              var currentSettingElement = settingsXML.xpath (settingName);


              if currentSettingElement does not exist

                  create the element and set the value

              else

                  currentSettingElement.setChildren(settingValue);

        • 1. Re: Add nodes to XML
          [Jongware] Most Valuable Participant

          The XML commands in the scripting reference include "insertChildAfter", "insertChildBefore", "appendChild", "prependChild", and "setChildren". At least one of these ought to do something

           

          But it's kind of hard to find out how you are supposed to use the commands. Wasn't there a specific XML guide somewhere in the Scripting Resources section of Adobe's InDesign site?

          • 2. Re: Add nodes to XML
            Kasyan Servetsky Level 5

            I use app.insertLabel() and app.extractLabel() to store and retrieve settings in a dialog box. It is more convenient than using extra txt or xml file.

            If you are interested, take a look at this script.

             

            This function creates default settings if they have not been saved yet.

            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;
            }

             

             

            Read saved settings before creating a dialog:

            function CreateDialog() {
                 var mySettings = GetSettings();
                 var myDialog = new Window('dialog', 'Package for Archive');

            ...

             

            Save chosen settings before closing the dialog after OK button was clicked:

            ...

                 var myShowDialog = myDialog.show();
                 
                 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;
            }

             

            Kasyan

            1 person found this helpful
            • 3. Re: Add nodes to XML
              Chris Cornelis Level 1

              Thanks for the tip Kasyan! I stopped using the XML file and I use the app labels now. Works like a charm.