19 Replies Latest reply on Apr 19, 2009 8:30 AM by Peter Kahrel

    JS CS3 import external data into Dialog.

    John.Kordas Level 1

      I would like to create a dropdown in a dialog that is populated by either an XML or txt file. Can this be done?

       

      1. First an xml or txt file would be created listing all the current products we do. This would sit on a server.
      2. The InDesign script would read the xml or txt file and in the dialog screen under products would only provide the user the names in the xml/txt file.
      3. The user could select the product in the list and run the script.
      4. The xml/txt file could be updated at any time (add or remove products as required)
        • 1. Re: JS CS3 import external data into Dialog.
          liedzeit Level 2

          This is certainly possible. Where do you have a problem?

           

          Ralf

          • 2. Re: JS CS3 import external data into Dialog.
            John.Kordas Level 1

            Ok to get my head around this I'm using this simple xml

             

            <xmlElement><item>1</item><item>2</item><item>3</item><item>4</item>
            </xmlElement>

             

            Using the following code I can load the xml and return the third item in the xml.

             

            var myDocument = app.documents.add();
            myDocument.importXML(File("/c/test.xml"));
            var myRootXMLElement = myDocument.xmlElements.item(0);

            $.write(myRootXMLElement.xmlElements.item(2).contents);  //<------ returns 3

             

            Now using the simple dialog example I'm trying to load the same file and display it in the dropmenu.

            var myXMLFile = XMLElement.importXML(File("/c/test.xml")); does not work.

             

            var myDialog = app.dialogs.add({name:"Simple Dialog"});

            //var myXMLFile = XMLElement.importXML(File("/c/test.xml"));

            with(myDialog.dialogColumns.add()){
            staticTexts.add({staticLabel:"This is a very simple dialog box."});

             

            var myProducts = dropdowns.add({stringList:["1", "2", "3", "4"], minWidth:100, selectedIndex:0}); //<------ need to change :["1", "2", "3", "4"] to xml values.

             

            }
            var myResult = myDialog.show();
            if(myResult == true){
            alert("You clicked the OK button.");
            }
            else{
            alert("You clicked the Cancel button.");
            }
            myDialog.destroy();

             

            Are there any examples I've looked through the SDK example but I may have mist it.

            • 3. Re: JS CS3 import external data into Dialog.
              liedzeit Level 2

              Do not import the XML file. Instead read it in and create your own XML object.

               

              var prefXMLFile = File("/c/test.xml");

              if(prefXMLFile != null){

              var myResult = prefXMLFile.open("r", undefined, undefined);

              if(myResult == true){

               

              var xmlstr = prefXMLFile.read();

              }

              else

              {

              alert("Error")

              }

               

              prefXMLFile.close();

              }

              var xmlobj = new XML (xmlstr);

               

               

              Once you have the XML object you can fill a dropdown by looping like this (from one of my projects)

               

               

              var countitems= xmlobj.Categories[0].Category.length();

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

              {

              curitem= xmlobj.Categories[0].Category[i].@name.toString();

              myArtDialog.category_dd.add("item",curitem);

              }

               

              You should read the JavaScript_Tools_Guide_CS3 to get more information on how to work with the XML object.

               

              Ralf

              • 4. Re: JS CS3 import external data into Dialog.
                John.Kordas Level 1

                Thanks Ralf,

                 

                these examples have been a great help.

                 

                I have the loop working I think but get and error myArtDialog is undefined.

                 

                For the example given before I'm using:

                 

                var myitems= xmlobj.item.length();
                for (i=0;i<myitems;i++)
                {
                var curitem= xmlobj.item[i].toString();
                myArtDialog.category_dd.add("item",curitem);
                }

                 

                Am I correct in that the myArtDialog.category_dd.add("item",curitem)line creates an array that can then be put in the dropdown?

                 

                For example something like:

                 

                var myProducts = dropdowns.add({stringList:[myArtDialog], minWidth:100, selectedIndex:0});

                • 5. Re: JS CS3 import external data into Dialog.
                  liedzeit Level 2

                  In my example myArtDialog is the dialog and category_dd is the dropdown (_dd for dropdown ).

                   

                  So just create your dropdown with e.g.

                   

                  var myProducts = add("dropdownlist",undefined, undefined);

                   

                  and then use myProducts  instead of myArtDialog.category_dd.

                   

                  Ralf

                  • 6. Re: JS CS3 import external data into Dialog.
                    John.Kordas Level 1

                    Thanks again Ralf,

                     

                    Using the simple dialog example I get Object does not support the property or method category_dd

                     

                    var myDialog = app.dialogs.add({name:"Simple Dialog"});
                    with(myDialog.dialogColumns.add()){
                    staticTexts.add({staticLabel:"This is a very simple dialog box."});
                      var myProducts = dropdowns.add("dropdownlist",undefined, undefined);
                    for (i=0;i<myitems;i++)
                    {
                    var curitem= xmlobj.item[i].toString();
                    myProducts.category_dd.add("item",curitem);
                    }
                    }

                     

                    Now I gather the line myProducts.category_dd.add("item",curitem); should be making a string to pass the details into the drop menu.

                     

                    Am I correct?

                    • 7. Re: JS CS3 import external data into Dialog.
                      liedzeit Level 2

                      Sorry, I did not see that you were trying to create a regular dialog. Use UI dialogs instead. They are much more flexible. You can use the information of the picked selection of your dropdown for example. (Read the Tools-Guide for more information.)

                       

                      myDialog=new Window('dialog', 'Simple Dialog');

                      with (myDialog)

                      {

                      add('statictext', undefined,"This is a highly complex dialog box.");

                      myProducts=add("dropdownlist",undefined, undefined);

                      var myitems=7;

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

                      {

                      var curitem= "hallo";

                      myProducts.add("item",curitem);

                      }

                      }

                      myDialog.show();

                       

                      Ralf

                      • 8. Re: JS CS3 import external data into Dialog.
                        John.Kordas Level 1

                        Ralf it took me a while but I finally got how you are building the dialog window. Working through the Javascript_tool_guide_CS3 I realized that the examples are for Bridge.

                         

                        In your example you used

                        myDialog=new Window('dialog', 'Simple Dialog');

                        to add the dialog window

                         

                         

                        In the InDesign example this is used

                        var myDialog = app.dialogs.add({name:"Simple Dialog"});

                        to add a dialog window.

                         

                        That is why when I was trying to do myProducts.add("item",curitem); it was not working but works in your example. Do you use this approach for all your dialog windows in In Design?

                         

                        What are the advantages/disadvantages?

                         

                        Is it possible to do the same as your simple dialog in the examples used for In Design

                         

                        myDialog=new Window('dialog', 'Simple Dialog');

                        with(myDialog.dialogColumns.add()){
                        staticTexts.add({staticLabel:"This is a very simple dialog box."});

                        • 9. Re: JS CS3 import external data into Dialog.
                          Dave Saunders Level 4

                          Hi John,

                           

                          There's two different ways you can build dialogs in InDesign. You can do what you're doing, which is using the legacy InDesign Dialog object or you can use ScriptUI. While ScriptUI was first available with Bridge, since InDesign CS3 it is also available within InDesign.

                           

                          Dave

                          • 10. Re: JS CS3 import external data into Dialog.
                            liedzeit Level 2

                            The disadvantage of using UI dialogs is that they are slightly more complicated. (But once you have a few working examples you can reuse them.) The main advantage is they look better and they are dynamic. So you can let the user pic a product in the first dropdown and you can then fill the next dropdown e.g. available colors in the second dropdown.

                            I try to avoid using dialogs but if I need one (and am in CS3) I go for the UI dialogs.

                             

                            Ralf

                            • 11. Re: JS CS3 import external data into Dialog.
                              John.Kordas Level 1

                              Hi Dave

                               

                              So moving forward it would be better to get my head around ScriptUI? From memory all the example I've seen in the In Design SDK are using the legacy Dialog. I'll do a search I think you have some examples on your site.

                               

                              Is it possible to do:

                               

                              var myProducts = dropdowns.add({stringList:[myDroplist], minWidth:100, selectedIndex:0});
                              for (i=0;i<myitems;i++)
                              {
                              var curitem= xmlobj.item[i].toString();
                              myDroplist.add("item",curitem);
                              }

                               

                              In Ralf's example he creates the myProducts dropdown and then in the loop adds the xml items to the list each loop. Is this only available in the ScriptUI or can it also be done in legacy Dialog?

                               

                               

                              Cheers John.

                              • 12. Re: JS CS3 import external data into Dialog.
                                John.Kordas Level 1

                                That's great to know Ralf,

                                 

                                I've got some old CS scripts that have a dropdown dialog the user selects and then from that selection a second dropdown dialog open. I've been wanting to update them and also link an xml into the dropdown and it sounds like the ScriptUI is what I need to pickup.

                                 

                                Much appreciated I think I'll need to get my hands on some SriptUI examples for InDesign and pull them a part.

                                • 13. Re: JS CS3 import external data into Dialog.
                                  John.Kordas Level 1

                                  I think I'm starting to get this ScriptUI. I've got the layout and buttons working just having a problem getting the value to the dropdown selection.

                                   

                                  I'm using myDialog.DDgroup.myProducts.value to alert the users selection but I'm getting "You selected undefined".

                                   

                                  var prefXMLFile = File("/c/test.xml");
                                  if(prefXMLFile != null){
                                  var myResult = prefXMLFile.open("r", undefined, undefined);
                                  if(myResult == true){
                                  var xmlstr = prefXMLFile.read();
                                  }
                                  else
                                  {
                                  alert("Error")
                                  }
                                  prefXMLFile.close();
                                  }
                                  var xmlobj = new XML (xmlstr);

                                   

                                  //$.write(xmlobj.item);

                                   

                                  var myitems= xmlobj.item.length();

                                   

                                  myDialog=new Window('dialog', 'Simple Dialog');
                                  myDialog.orientation = 'column';
                                  myDialog.alignment = 'right';
                                  myDialog.DDgroup = myDialog.add('group');
                                  myDialog.DDgroup.orientation = 'row';
                                  myDialog.DDgroup.add('statictext', undefined,"Simple XML droplist.");
                                  myDialog.DDgroup.myProducts = myDialog.DDgroup.add("dropdownlist",undefined, undefined);

                                   

                                  myDialog.Btngroup = myDialog.add('group');
                                  myDialog.Btngroup.orientation = 'row';
                                  myDialog.Btngroup.alertBtn = myDialog.Btngroup.add('button', undefined, 'Alert Selection');
                                  myDialog.Btngroup.closeBtn = myDialog.Btngroup.add('button', undefined, 'Close');

                                   

                                  for (i=0;i<myitems;i++)
                                  {
                                  var curitem= xmlobj.item[i].toString();
                                  myDialog.DDgroup.myProducts.add("item",curitem);
                                  }
                                  myDialog.Btngroup.alertBtn.onClick = function() {
                                  alert("You selected "+myDialog.DDgroup.myProducts.value);   
                                      }
                                  myDialog.Btngroup.closeBtn.onClick = function() {
                                    this.parent.parent.close();   
                                      }

                                   

                                  myDialog.show();

                                   

                                  any suggestion would be appreciated.

                                   

                                  Cheers, John.

                                  • 15. Re: JS CS3 import external data into Dialog.
                                    John.Kordas Level 1

                                    Fantastic Peter,

                                     

                                    That did the trick and I also appreciate the examples you posted.

                                     

                                    Cheers John.

                                    • 16. Re: JS CS3 import external data into Dialog.
                                      John.Kordas Level 1

                                      I'm trying to expand of this example but I've come across a slight problem.

                                       

                                      I've change the simple xml to this.

                                       

                                      <queues>
                                        <option>
                                          <optionname>PDFFiles</optionname>
                                          <optionpath>/c/pdfs/</optionpath>
                                        </option>
                                        <option>
                                          <optionname>PSFiles</optionname>
                                          <optionpath>/c/postscript/</optionpath>
                                        </option>
                                      </queues>

                                       

                                      The script below should alert the selection no and the selection path

                                       

                                      if the first option is selected:

                                           alert("selection No is "+myDialog.DDgroup.myProducts.selection);  

                                      will return selection No is 0
                                            alert("path is "+xmlobj.option[myDialog.DDgroup.myProducts.selection].optionpath.toString());

                                      returns path is

                                       

                                      Shouldn't xmlobj.option[myDialog.DDgroup.myProducts.selection].optionpath.toString() return /c/pdfs/ if myDialog.DDgroup.myProducts.selection returns 0?

                                       

                                       

                                      var prefXMLFile = File("/c/queues.xml");
                                      if(prefXMLFile != null){
                                      var myResult = prefXMLFile.open("r", undefined, undefined);
                                      if(myResult == true){
                                      var xmlstr = prefXMLFile.read();
                                      }
                                      else
                                      {
                                      alert("Error")
                                      }
                                      prefXMLFile.close();
                                      }
                                      var xmlobj = new XML (xmlstr);

                                       

                                      //$.write(xmlobj.option);

                                       

                                      var myitems= xmlobj.option.length();

                                       

                                      myDialog=new Window('dialog', 'Simple Dialog');
                                      myDialog.orientation = 'column';
                                      myDialog.alignment = 'right';
                                      myDialog.DDgroup = myDialog.add('group');
                                      myDialog.DDgroup.orientation = 'row';
                                      myDialog.DDgroup.add('statictext', undefined,"Simple XML droplist.");
                                      myDialog.DDgroup.myProducts = myDialog.DDgroup.add("dropdownlist",undefined, undefined);

                                       

                                      myDialog.Btngroup = myDialog.add('group');
                                      myDialog.Btngroup.orientation = 'row';
                                      myDialog.Btngroup.alertBtn = myDialog.Btngroup.add('button', undefined, 'Alert Selection');
                                      myDialog.Btngroup.closeBtn = myDialog.Btngroup.add('button', undefined, 'Close');

                                       

                                      for (i=0;i<myitems;i++)
                                      {
                                      var curitem= xmlobj.option[i].optionname.toString();
                                      myDialog.DDgroup.myProducts.add("item",curitem);
                                      }
                                      myDialog.Btngroup.alertBtn.onClick = function() {
                                      if (myDialog.DDgroup.myProducts.selection == null ){
                                      alert("Make Selection");   
                                      }else{
                                      alert("selection No is "+myDialog.DDgroup.myProducts.selection);   
                                      alert("path is "+xmlobj.option[myDialog.DDgroup.myProducts.selection].optionpath.toString());   
                                      }
                                          }
                                      myDialog.Btngroup.closeBtn.onClick = function() {
                                        this.parent.parent.close();   
                                          }

                                       

                                      myDialog.show();

                                      • 17. Re: JS CS3 import external data into Dialog.
                                        Peter Kahrel Adobe Community Professional & MVP

                                        John,

                                         

                                        When you get counter-intuitive results like this, then make sure that you're dealing with the type of object that you think you're dealing with. For example, myDialog.DDgroup.myProducts.selection is displayed as a number (0 or 1 in your case), but do this:

                                         

                                        alert (myDialog.DDgroup.myProducts.selection.constructor.name)

                                         

                                        and you'll see that you're dealing with an object of type ListItem. To return the item's indes, use the .index property (which is a number). Ans as pointed out in an earlier post in this thread, .text returns the lit item's contents. So these two lines display what you want:

                                         

                                        alert("selection No is "+myDialog.DDgroup.myProducts.selection.text);  
                                        alert("path is "+xmlobj.option[myDialog.DDgroup.myProducts.selection.index].optionpath);

                                         

                                        Peter

                                        • 18. Re: JS CS3 import external data into Dialog.
                                          John.Kordas Level 1

                                          Thanks again Peter,

                                           

                                          I did myDialog.DDgroup.myProducts.selection.constructor.name and sure enough I got ListItem. I must remember to use the .constructor.name more.

                                           

                                          I also then checked all the other options for ListItem in the ScriptUI Classes.

                                           

                                          If I'm using myDialog.DDgroup.add('dropdownlist',undefined, undefined); shouldn't the .constructor.name return DropDownList?

                                           

                                          Cheers John.

                                          • 19. Re: JS CS3 import external data into Dialog.
                                            Peter Kahrel Adobe Community Professional & MVP

                                            myDialog.DDgroup.myProducts.constructor.name returns DropDownList

                                             

                                            Peter