10 Replies Latest reply on Jun 18, 2009 3:30 PM by John.Kordas

    JS CS3 Dynamic XML Dropmenu

    John.Kordas Level 1

      I'm trying to create a dynamic drop menu dialog that is populated by an xml file. I think I have the xml file setup correctly but not sure how to get the second drop menu to populate according to the first drop menu selection. Would the best way be to have the myDialog.fDetails.fPath.dd.onChange = function () { set the path and section value in var curitem= xmlbooks.path[0].section[0].size.name[j].text();

       

      This is the xml file:

       

      
      

      <books>
          <path>/c/temp/
              <section>Book1
                <size>
                  <name>Double Page Spread 420 x 275</name>
                  <width>420</width>
                  <height>275</height>
                </size>
                <size>
                  <name>Single Page Spread 210 x 275</name>
                  <width>210</width>
                  <height>275</height>
                </size>
                <size>
                  <name>1/2 Page Horizontal 180 x 120</name>
                  <width>180</width>
                  <height>120</height>
                </size>
                </section>
                <section>Book2
                <size>
                  <name>Double Page Spread 440 x 275</name>
                  <width>440</width>
                  <height>275</height>
                </size>
                <size>
                  <name>Single Page Spread 220 x 275</name>
                  <width>220</width>
                  <height>275</height>
                </size>
                <size>
                  <name>1/2 Page Horizontal 120 x 190</name>
                  <width>120</width>
                  <height>190</height>
                </size>
                </section>
            </path>

       

      This will run in ESTK

      
      

      var booksXMLFile = File("/c/booklist.xml");
      if(booksXMLFile != null){
      var mybooks = booksXMLFile.open("r", undefined, undefined);
      if(mybooks == true){
              var xmlbooksStr = booksXMLFile.read();
          }else{
              alert("Could not load booklist.xml file.")
          }
      booksXMLFile.close();
      }
      var xmlbooks = new XML (xmlbooksStr);
      var mybooks= xmlbooks.path.section.length();
      $.write(mybooks);

       

      var myDialog = new Window('dialog', ' Books');

       

          myDialog.fDetails = myDialog.add('panel',undefined,'Book details');
          myDialog.fDetails.alignChildren = 'left';
          myDialog.fDetails.fPath = myDialog.fDetails.add('group');
          myDialog.fDetails.fPath.group = myDialog.fDetails.fPath.add('group');
          myDialog.fDetails.fTitle = myDialog.fDetails.add('group');
          myDialog.fDetails.btnGroup = myDialog.fDetails.add('group');
          with (myDialog.fDetails){
          fPath.group.orientation = 'column';
          fPath.group.alignChildren = 'right';
          fPath.group.preferredSize = [90,15];
          fPath.group.st  = fPath.group.add('statictext',undefined,'Select Book:');

      //first drop menu
          fPath.dd = fPath.add('dropdownlist', undefined, undefined)
              for (i=0;i<mybooks;i++)
              {
              var curitem= xmlbooks.path.section[i].text();
              fPath.dd.add("item",curitem);
              }
          fTitle.st  = fTitle.add('statictext',undefined,'Select Size:');
          fTitle.st.preferredSize = [90,15];

      //second drop menu
          fTitle.dd = fTitle.add('dropdownlist', undefined, undefined)
                  for (j=0;j<mybooks;j++)
              {
              var curitem= xmlbooks.path[0].section[0].size.name[j].text();
              fTitle.dd.add("item",curitem);
              }
          fTitle.dd.enabled = false;
          btnGroup.btn = btnGroup.add('button', undefined, 'Alert Selection');
          btnGroup.alignment = 'right';
          }

       

      myDialog.fDetails.fPath.dd.onChange = function () {
          if(myDialog.fDetails.fPath.dd.selection) {
                  myDialog.fDetails.fTitle.dd.enabled = true;
                  }
              }

       

      myDialog.fDetails.btnGroup.btn.onClick = function() {
          var myValStr
          if (myDialog.fDetails.fPath.dd.selection == null ){
              myValStr = "Make Book Selection \n"
              }else{
              myValStr = ""   
              }
          if (myDialog.fDetails.fTitle.dd.selection == null ){
              myValStr = myValStr + "Make Size Selection"
              }
          if(myValStr.length >1){
          alert(myValStr);
          }else{
              myDialog.close();
              alert("Thanks for your selections.");
             
              }
      }

       

      myDialog.show();

        • 1. Re: JS CS3 Dynamic XML Dropmenu
          Randomsen

          var booksXMLFile = File("/c/booklist.xml");

           

          if(booksXMLFile != null){

          var mybooks = booksXMLFile.open("r", undefined, undefined);

          if(mybooks == true){

                  var xmlbooksStr = booksXMLFile.read();

              }else{

                  alert("Could not load booklist.xml file.")

              }

          booksXMLFile.close();

          }

          var xmlbooks = new XML (xmlbooksStr);

          var mybooks= xmlbooks.path.section.length();

          $.write(mybooks);

           

           

           

          var myDialog = new Window('dialog', ' Books');

           

           

           

              myDialog.fDetails = myDialog.add('panel',undefined,'Book details');

              myDialog.fDetails.alignChildren = 'left';

              myDialog.fDetails.fPath = myDialog.fDetails.add('group');

              myDialog.fDetails.fPath.group = myDialog.fDetails.fPath.add('group');

              myDialog.fDetails.fTitle = myDialog.fDetails.add('group');

              myDialog.fDetails.btnGroup = myDialog.fDetails.add('group');

              with (myDialog.fDetails){

              fPath.group.orientation = 'column';

              fPath.group.alignChildren = 'right';

              fPath.group.preferredSize = [90,15];

              fPath.group.st  = fPath.group.add('statictext',undefined,'Select Book:');

           

          //first drop menu

              fPath.dd = fPath.add('dropdownlist', undefined, undefined)

                  for (i=0;i<mybooks;i++)

                  {

                  var curitem= xmlbooks.path.section[i].text();

                  fPath.dd.add("item",curitem);

                  }

              fTitle.st  = fTitle.add('statictext',undefined,'Select Size:');

              fTitle.st.preferredSize = [90,15];

           

          //second drop menu

              fTitle.dd = fTitle.add('dropdownlist', undefined, undefined)

              fTitle.dd.enabled = false;

              btnGroup.btn = btnGroup.add('button', undefined, 'Alert Selection');

              btnGroup.alignment = 'right';

          fTitle.dd.preferredSize = [90,15];

              }

           

           

           

          myDialog.fDetails.fPath.dd.onChange = function () {

              if(myDialog.fDetails.fPath.dd.selection) {

                               for (j=0;j<mybooks;j++)

                  {

                  if (xmlbooks.path.section[j].text() == myDialog.fDetails.fPath.dd.selection.text){

                  var myindex =j;     

                  break;      

                  } 

                      }

           

          var mylength = xmlbooks.path.section[myindex].size.length();

          //reset the list

          myDialog.fDetails.fTitle.dd.removeAll();

          for(i = 0; i<mylength ;i++){

             var curitem = xmlbooks.path.section[myindex].size[i].name.text();

          myDialog.fDetails.fTitle.dd.add("item",curitem);

                      }

                  myDialog.fDetails.fTitle.dd.enabled = true;

                  }

          }

           

          myDialog.fDetails.btnGroup.btn.onClick = function() {

              var myValStr

              if (myDialog.fDetails.fPath.dd.selection == null ){

                  myValStr = "Make Book Selection \n"

                  }else{

                  myValStr = ""  

                  }

              if (myDialog.fDetails.fTitle.dd.selection == null ){

                  myValStr = myValStr + "Make Size Selection"

                  }

              if(myValStr.length >1){

              alert(myValStr);

              }else{

                  myDialog.close();

                  alert("Thanks for your selections.");

                

                  }

          }

           

           

           

          myDialog.show();

           

           

           

          // Rather than add an insane amount of length to this post i just edited it.

          // The issue was xmlbooks.path[0].section[j], which was limiting it to the first instance of path

          • 2. Re: JS CS3 Dynamic XML Dropmenu
            Randomsen Level 1

            If you add attributes to the XML tags, Xpath can be an easy way of getting what you want as well.

            • 3. Re: JS CS3 Dynamic XML Dropmenu
              John.Kordas Level 1

              Thanks Randomsen,

               

              That seems to work. One this I didn't see was the xml code I posted was cut short for some reason.  What I wanted to do was have a second or third path range. But for some reason if I try and run the xml code below I get a no option is the second drop menu if I select Book 1 and 2 and if I select the third option I get a Clear runtime error. I am guessing this is because the var mybooks= xmlbooks.path.section.length(); is being affected.

               

              
              

              <books>
                  <path>/c/temp/
                      <section>Book1
                        <size>
                          <name>Double Page Spread 420 x 275</name>
                          <width>420</width>
                          <height>275</height>
                        </size>
                        <size>
                          <name>Single Page Spread 210 x 275</name>
                          <width>210</width>
                          <height>275</height>
                        </size>
                        </section>
                        <section>Book2
                        <size>
                          <name>Double Page Spread 440 x 275</name>
                          <width>440</width>
                          <height>275</height>
                        </size>
                        </section>
                   </path>
                   <path>/c/temp2/
                      <section>GlossBook1
                        <size>
                          <name> Spread 420 x 275</name>
                          <width>420</width>
                          <height>275</height>
                        </size>
                        </section>
                    </path>
              </books>

               

               

              When you mentioned XML tags do you mean like the example below (taken form xmlrules sdk folder).

               

              
              

              <XMLElement name = "Root">
                  <XMLElement name = "A">A
                      <XMLElement name = "AA">AA</XMLElement>
                      <XMLElement name = "AB">AB</XMLElement>
                      <XMLElement name = "AC">AC</XMLElement>
                  </XMLElement>

              • 4. Re: JS CS3 Dynamic XML Dropmenu
                Randomsen Level 1

                Yes, and then you could access the content without having to list out the path like you have.

                 

                Check it out:

                 

                http://www.w3schools.com/XPath/xpath_syntax.asp

                • 5. Re: JS CS3 Dynamic XML Dropmenu
                  John.Kordas Level 1

                  Thanks again Randomsen,

                   

                  I've looked at the link but I'm just not getting it. Will try out a few tests tonight.  For anyone else this will return your selections:

                   

                   

                  
                  

                  var mybooks= xmlbooks.path.section.length();
                  var mySecCount = ""; ///------add this under the mybooks variable.

                   

                         var myindex =j; 
                         mySecCount = j; ////--------add this under the myindex variable.

                   

                   

                   

                  myDialog.close();  ///------add the code below and replace the previous alert.
                         
                          for (r=0;r<xmlbooks.path.section[mySecCount].size.length();r++)
                          {

                  if (xmlbooks.path.section[mySecCount].size[r].name.text() == myDialog.fDetails.fTitle.dd.selection.text){
                                      var myName = xmlbooks.path.section[mySecCount].size[r].name.text()
                                      var myWidth = xmlbooks.path.section[mySecCount].size[r].width.text()                   
                                      var myHeight = xmlbooks.path.section[mySecCount].size[r].height.text()
                                   }
                              }
                         
                      alert("Thanks for selecting: \n Name:"+myName+" \n Width: "+myWidth+" \n Height: "+myHeight);

                  • 6. Re: JS CS3 Dynamic XML Dropmenu
                    John.Kordas Level 1

                    I have a feeling I've gotten my xml code wrong. In the previous post I'm able to get the name height and width with no problem but if I try and get the <path> for that set I obviously need to return path[0] or path[1].

                     

                    Adding  var myPath = xmlbooks.path.text(); just gives me Path:/c/temp//c/temp2/ not just /c/temp/ which I expected. Is it possible to get the script to return the selection path? Would I need to put the path after the <section> tags?

                     

                     

                    ---------------------------
                    Script Alert
                    ---------------------------
                    Thanks for selecting:
                    Path:/c/temp//c/temp2/
                    Section:Book1
                    Name:Double Page Spread 420 x 275
                    Width: 420
                    Height: 275
                    ---------------------------
                    OK  
                    ---------------------------

                     

                     

                    Cheers John.

                    • 7. Re: JS CS3 Dynamic XML Dropmenu
                      Randomsen Level 1

                      I think if you were to do this all over again, you would make the xml file keeping in mind how you want to parse out the data.

                       

                      Though the path tag seems to be a parent or container of the information within(conceptually), you might want to make it an attribute of the section tag.  Set up the xml file like this:

                       

                      <books>
                              <section path = "/c/temp/"
                      >Book1
                                <size>
                                  <name>Double Page Spread 420 x 275</name>
                                  <width>420</width>
                                  <height>275</height>
                                </size>
                                <size>
                                  <name>Single Page Spread 210 x 275</name>
                                  <width>210</width>
                                  <height>275</height>
                                </size>
                                </section>
                                <section path = "/c/temp/">Book2
                                <size>
                                  <name>Double Page Spread 440 x 275</name>
                                  <width>440</width>
                                  <height>275</height>
                                </size>
                                </section>

                              <section path = "/c/temp2/" >GlossBook1

                                <size>
                                  <name> Spread 420 x 275</name>
                                  <width>420</width>
                                  <height>275</height>
                                </size>
                                </section>
                      </books>

                       

                      I took at the path tags completely, so you'll have to delete ".path" out of all your code.

                      Then to access the attribute, just add this in the end when you're putting together that last string for the 'alert selection' alert dialog.

                       

                      if (xmlbooks.section[mySecCount].size[r].name.text() == myDialog.fDetails.fTitle.dd.selection.text){
                                          var myName = xmlbooks.section[mySecCount].size[r].name.text()
                                          var myWidth = xmlbooks.section[mySecCount].size[r].width.text()                  
                                          var myHeight = xmlbooks.section[mySecCount].size[r].height.text()
                                          var myPath = xmlbooks.section[mySecCount].attribute("path");              
                                       }
                                  }  
                         alert("Thanks for selecting: \n Name:"+myName+" \n Width: "+myWidth+" \n Height: "+myHeight + " \n Path: " + myPath);
                      }

                      ~Mike

                      • 8. Re: JS CS3 Dynamic XML Dropmenu
                        John.Kordas Level 1

                        Thanks again Mike,

                         

                        I knew there had to be a better way I tried moving the path tags under the section tags which worked but your method is much easier to work with.

                         

                        Now that the dialog is in place I removed the alert and use the following lines:

                         

                        
                        

                        var myFile =File(myPath).openDlg("Choose a File to Place");
                        if(myFile == null){alert("Please select a file and then try again."); exit(); } 
                          var myDocument = app.documents.add();

                         

                        The myPath takes me to the correct folder if I select nothing I get my alert and the dialog closes but if I select a file and click ok I get:

                         

                        Cannot handle the request because a modal dialog or alert is active.

                         

                        I've tried using both the main and session targetengine but no luck.

                         

                        Cheers, John

                        • 9. Re: JS CS3 Dynamic XML Dropmenu
                          Randomsen Level 1

                          its getting a little complex without seeing all your code, but I'm going to take a couple of guesses.

                           

                           

                          1. Use #targetengine "session"

                           

                          2. You don't have to use a dialog( for example: new Window('window', ' Books');, you can put 'window' as the parameter instead. Put the myDialog.close() in the onClick function of the button as the last thing(so it closes after you run the code to add/create the document. )

                           

                          2. I'm not sure about using the close() method for a dialog, Dialogs stay in the memory unless you destroy them, which may be why its still considering active?..... myDialog.destroy(); I'm pretty sure the File().openDlg(); wouldn't interfere.

                           

                          But like I said, just guesses.

                           

                          ~Mike

                          • 10. Re: JS CS3 Dynamic XML Dropmenu
                            John.Kordas Level 1

                            The var myDialog = new Window('window', ' Books'); did the trick.  Now I can start to apply all the doc prefs and the script should be working like a treat.

                             

                            Much appreciated Mike.