4 Replies Latest reply on Apr 7, 2015 3:36 PM by Zantcor

    Trees, Branches and Items OHMY!

    Zantcor Level 1

      Hello fellow scripters!

       

      This is my first attempt at useing a tree for anything, I usually stick to radio buttons and drop down lists but the tree is the best option for what I have.  I've got it to populate and everything works really nice but what is the best way to get what is selected from the tree?  Below is my code.

       

      //Created by Daryl R. Smith
      
      #target illustrator
      
      if (1 > 0)
      {
      
      var w = new Window ("dialog", "Choose Calendar Template", undefined,{resize: true});
      w.orientation = "column";
      w.alignChildren = "right";
      w.size = [300,525];
      
      var caltab = w.add ('group',undefined,"Calendar");
      
      var cal_Tree = caltab.add ('treeview',undefined,"Header shapes");
      cal_Tree.size = [280,430]
      
      var calPnS_Branch = cal_Tree.add ('node',"Press N Stick");
      
      var calMS_Branch = cal_Tree.add ('node',"Magna Stick");
      
      var calSS_Branch = cal_Tree.add ('node',"Super Size");
      
      var calSP_Branch = cal_Tree.add ('node',"Screen Print");
      
      var calSP_PnS_Branch = calSP_Branch.add ('node', "Press N Stick")
      var calSP_MS_Branch = calSP_Branch.add ('node', "Magna Stick")
      var calSP_SS_Branch = calSP_Branch.add ('node', "Super Size")
      
      var calFC_Branch = cal_Tree.add ('node', "Full Color")
      var calFC_7871_Branch = calFC_Branch.add ('node', "V7871 Full Color Calendars")
      var calFC_PnC_Branch = calFC_Branch.add ('node', "V7873 Paws-N-Claws")
      
      var cal3month_Branch = cal_Tree.add ('node',"3-Month");
      
      var calMisc_Branch = cal_Tree.add ('node',"Misc");
      
      var bottomsection = w.add ('group', undefined, undefined)
      bottomsection.orientation = "row"
      
      var selectiongrp = bottomsection.add ('group', undefined, undefined)
      selectiongrp.orientation = "column"
      
      var treeItemSelectedis = selectiongrp.add ('statictext', undefined, "Your selection is: ")
      var treeItemSelected = selectiongrp.add ('statictext', undefined, "Item Number")
      
      calPnS_Branch.active = true;
      
      cal_Tree.onChange = function()
      {
          treeItemSelected.text = cal_Tree.name;
      }
      
      
      var bottomspacer = bottomsection.add ('group', undefined, undefined)
      bottomspacer.size = [70,00]
      
      var btngroup = bottomsection .add ('group', undefined, undefined)
      btngroup.orientation = "column"
      
      var okbtn = btngroup.add ('button', undefined, "Ok");
      var cnlbtn = btngroup.add ('button', undefined, "Cancel");
      
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      function fillall_PnS()
      { 
          
      var calPnSTempPath = Folder ("S:/Art/ArtDept/  Vitronic Master Templates/CALENDARS/Calendars/Press N Stick");   
        var calPnSTempFiles = calPnSTempPath.getFiles();  
        var calPnSnamesArr = [];  
        for(var i=0; i<calPnSTempFiles.length; i++){  
        var thiscalPnSFile = calPnSTempFiles[i];  
        calPnSnamesArr.push(thiscalPnSFile.displayName); 
        }  
      
         return calPnSnamesArr;  
      }  
      
         var calPnSarr1 = fillall_PnS();
      
              for(var i=0; i<calPnSarr1.length; i++)
              {  
                  var calPnSitem = calPnSarr1[i];
                  var lbl = calPnS_Branch.add ('item', calPnSitem);  
              }   
          
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      function fillall_MS()
      { 
          
      var calMSTempPath = Folder ("S:/Art/ArtDept/  Vitronic Master Templates/CALENDARS/Calendars/Magna Stick");   
        var calMSTempFiles = calMSTempPath.getFiles();  
        var calMSnamesArr = [];  
        for(var i=0; i<calMSTempFiles.length; i++){  
        var thiscalMSFile = calMSTempFiles[i];  
        calMSnamesArr.push(thiscalMSFile.displayName); 
        }  
      
         return calMSnamesArr;  
      }  
      
         var calMSarr1 = fillall_MS ();
      
              for(var i=0; i<calMSarr1.length; i++)
              {  
                  var calMSitem = calMSarr1[i];
                  var lbl = calMS_Branch.add ('item', calMSitem);  
              }  
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
          function fillall_SP_PNS()
      { 
          
      var calSP_PnSTempPath = Folder ("S:/Art/ArtDept/  Vitronic Master Templates/CALENDARS/Calendars SP/Press N Stick");   
        var calSP_PnSTempFiles = calSP_PnSTempPath.getFiles();  
        var calSP_PnSnamesArr = [];  
        for(var i=0; i<calSP_PnSTempFiles.length; i++){  
        var thiscalSP_PnSFile = calSP_PnSTempFiles[i];  
        calSP_PnSnamesArr.push(thiscalSP_PnSFile.displayName); 
        }  
      
         return calSP_PnSnamesArr;  
      }  
      
         var calSP_PnSarr1 = fillall_SP_PNS();
      
              for(var i=0; i<calSP_PnSarr1.length; i++)
              {  
                  var calSP_PnSitem = calSP_PnSarr1[i];
                  var lbl = calSP_PnS_Branch.add ('item', calSP_PnSitem);  
              }
          
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
              function fillall_SP_MS()
      { 
          
      var calSP_MSTempPath = Folder ("S:/Art/ArtDept/  Vitronic Master Templates/CALENDARS/Calendars SP/Magna Stick");   
        var calSP_MSTempFiles = calSP_MSTempPath.getFiles();  
        var calSP_MSnamesArr = [];  
        for(var i=0; i<calSP_MSTempFiles.length; i++){  
        var thiscalSP_MSFile = calSP_MSTempFiles[i];  
        calSP_MSnamesArr.push(thiscalSP_MSFile.displayName); 
        }  
      
         return calSP_MSnamesArr;  
      }  
      
         var calSP_MSSarr1 = fillall_SP_MS();
      
              for(var i=0; i<calSP_MSSarr1.length; i++)
              {  
                  var calSP_MSitem = calSP_PnSarr1[i];
                  var lbl = calSP_MS_Branch.add ('item', calSP_MSitem);  
              }
       /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////   
                  function fillall_SP_SS()
      { 
          
      var calSP_SSTempPath = Folder ("S:/Art/ArtDept/  Vitronic Master Templates/CALENDARS/Calendars SP/Super Size Press N Stick");   
        var calSP_SSTempFiles = calSP_SSTempPath.getFiles();  
        var calSP_SSnamesArr = [];  
        for(var i=0; i<calSP_SSTempFiles.length; i++){  
        var thiscalSP_SSFile = calSP_SSTempFiles[i];  
        calSP_SSnamesArr.push(thiscalSP_SSFile.displayName); 
        }  
      
         return calSP_SSnamesArr;  
      }  
      
         var calSP_SSarr1 = fillall_SP_SS();
      
              for(var i=0; i<calSP_SSarr1.length; i++)
              {  
                  var calSP_SSitem = calSP_SSarr1[i];
                  var lbl = calSP_SS_Branch.add ('item', calSP_SSitem);  
              }
       /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////   
          
                      function fillall_SS()
      { 
          
      var calSSTempPath = Folder ("S:/Art/ArtDept/  Vitronic Master Templates/CALENDARS/Calendars/Super Size Press N Stick");   
        var calSSTempFiles = calSSTempPath.getFiles();  
        var calSSnamesArr = [];  
        for(var i=0; i<calSSTempFiles.length; i++){  
        var thiscalSSFile = calSSTempFiles[i];  
        calSSnamesArr.push(thiscalSSFile.displayName); 
        }  
      
         return calSSnamesArr;  
      }  
      
         var calSSarr1 = fillall_SS();
      
              for(var i=0; i<calSSarr1.length; i++)
              {  
                  var calSSitem = calSSarr1[i];
                  var lbl = calSS_Branch.add ('item', calSSitem);  
              }
          
       /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////   
                          function fillall_3month()
      { 
          
      var cal3monthTempPath = Folder ("S:/Art/ArtDept/  Vitronic Master Templates/CALENDARS/Calendars/3-Month 8858 & 8860");   
        var cal3monthTempFiles = cal3monthTempPath.getFiles();  
        var cal3monthnamesArr = [];  
        for(var i=0; i<cal3monthTempFiles.length; i++){  
        var thiscal3monthFile = cal3monthTempFiles[i];  
        cal3monthnamesArr.push(thiscal3monthFile.displayName); 
        }  
      
         return cal3monthnamesArr;  
      }  
      
         var cal3montharr1 = fillall_3month();
      
              for(var i=0; i<cal3montharr1.length; i++)
              {  
                  var cal3monthitem = cal3montharr1[i];
                  var lbl = cal3month_Branch.add ('item', cal3monthitem);  
              }
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
                              function fillall_FC_7871()
      { 
          
      var calFC_7871TempPath = Folder ("S:/Art/ArtDept/  Vitronic Master Templates/CALENDARS/FULL COLOR/V7871");   
        var calFC_7871TempFiles = calFC_7871TempPath.getFiles();  
        var calFC_7871namesArr = [];  
        for(var i=0; i<calFC_7871TempFiles.length; i++){  
        var thiscalFC_7871File = calFC_7871TempFiles[i];  
        calFC_7871namesArr.push(thiscalFC_7871File.displayName); 
        }  
      
         return calFC_7871namesArr;  
      }  
      
         var calFC_7871arr1 = fillall_FC_7871();
      
              for(var i=0; i<calFC_7871arr1.length; i++)
              {  
                  var calFC_7871item = calFC_7871arr1[i];
                  var lbl = calFC_7871_Branch.add ('item', calFC_7871item);  
              }
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////   
                                  function fillall_FC_PnC()
      { 
          
      var calFC_PnCTempPath = Folder ("S:/Art/ArtDept/  Vitronic Master Templates/CALENDARS/FULL COLOR/V7873 PAWS N CLAWS");   
        var calFC_PnCTempFiles = calFC_PnCTempPath.getFiles();  
        var calFC_PnCnamesArr = [];  
        for(var i=0; i<calFC_PnCTempFiles.length; i++){  
        var thiscalFC_PnCFile = calFC_PnCTempFiles[i];  
        calFC_PnCnamesArr.push(thiscalFC_PnCFile.displayName); 
        }  
      
         return calFC_PnCnamesArr;  
      }  
      
         var calFC_PnCarr1 = fillall_FC_PnC();
      
              for(var i=0; i<calFC_PnCarr1.length; i++)
              {  
                  var calFC_PnCitem = calFC_PnCarr1[i];
                  var lbl = calFC_PnC_Branch.add ('item', calFC_PnCitem);  
              }
          
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      function fillall_Misc()
      { 
          
      var calMiscTempPath = Folder ("S:/Art/ArtDept/  Vitronic Master Templates/CALENDARS/Misc");   
        var calMiscTempFiles = calMiscTempPath.getFiles();  
        var calMiscnamesArr = [];  
        for(var i=0; i<calMiscTempFiles.length; i++){  
        var thiscalMiscFile = calMiscTempFiles[i];  
        calMiscnamesArr.push(thiscalMiscFile.displayName); 
        }  
      
         return calMiscnamesArr;  
      }  
      
         var calMiscarr1 = fillall_Misc();
      
              for(var i=0; i<calMiscarr1.length; i++)
              {  
                  var calMiscitem = calMiscarr1[i];
                  var lbl = calMisc_Branch.add ('item', calMiscitem);  
              }
          
       /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
      
      okbtn.onClick = function()
      {    
         getchoice()
      alert ("who's awesome?" + itemrequested.text + " is your choice")
      
      }
      
      cnlbtn.onClick = function ()
      {
          w.close()
      }
      
       w.show ()
      }
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      function getchoice()
      {
          var maxPossible = calPnS_Branch.length;
          
              for (var i = 0, l = maxPossible; i < l; i++) {  
                  if (calPnS_Branch[i].selected == true) {  
                      itemrequested.text = calPnS_Branch[i].name;
                      alert ("pns");
                      
                      break;
                  } else if (calMS_Branch[i].selected == true) {  
                      itemrequested.text = calMS_Branch[i].name; 
                      
                      break;
                  } else if (calSS_Branch[i].selected == true) {  
                      itemrequested.text = calSS_Branch[i].name;
      
                      break;
                  } else if (calSP_Branch[i].selected == true) {  
                      itemrequested.text = calSP_Branch[i].name;
      
                      break;
                  } else if (calSP_PnS_Branch[i].selected  == true) {  
                     itemrequested.text = calSP_PnS_Branch[i].name;
      
                      break;
                  } else if (calSP_MS_Branch[i].selected  == true) {  
                      itemrequested.text = calSP_MS_Branch[i].name;
      
                      break;
                  } else if (calSP_SS_Branch[i].selected  == true) {  
                      itemrequested.text = calSP_SS_Branch[i].name;
      
                      break;
                  } else if (calFC_7871_Branch[i].selected  == true) {  
                      itemrequested.text = calFC_7871_Branch[i].name;
      
                      break;
                  } else if (calFC_PnC_Branch[i].selected  == true) {  
                      itemrequested.text = calFC_PnC_Branch[i].name;
      
                      break;
                  } else if (cal3month_Branch[i].selected  == true) {  
                      itemrequested.text = cal3month_Branch[i].name;
      
                      break;
                  } else if (calMisc_Branch[i].selected  == true) {  
                      itemrequested.text = calMisc_Branch[i].name;
      
                      break;
                  } else 
              { 
                  itemrequested.text = "Item Not Found"; 
                      
                      }
              }  
          }
      

       

       

      I'm not sure how to get the selected item, I have the scriptui-2-5 guide from Peter Hahrel and it has been a major help but doesn't say how to get the users choice that I can find.

       

      I made a selection part near the bottom that goes through every item of every branch to get the selection but it never works.  I even tried the "cal_Tree.selected" but it only returns the index number not the name.

       

      Always thankful

       

      -Zantcor-

        • 1. Re: Trees, Branches and Items OHMY!
          Silly-V Adobe Community Professional

          In the treeview you got a "selection" property, so what you can do is refer to it as treeview.selection.  To get the text for example, use treeview.selection.text.

          • 2. Re: Trees, Branches and Items OHMY!
            W_J_T Level 4

            Not sure exactly what you're after but maybe the following will help.

             

            // Replace this...
            // cal_Tree.onChange = function() {
            //    treeItemSelected.text = cal_Tree.name;
            // }
            // -----------------------------------------------
            // With this...
            cal_Tree.onChange = function() {
                treeItemSelected.text = cal_Tree.selection.text;
            }
            

             

            Hope it helps your efforts.

             

            EDIT:  Looks like you found the solution while I was typing.

            • 3. Re: Trees, Branches and Items OHMY!
              Zantcor Level 1

              That works but is there a way to tell which branch the selection is from?  I tried "cal_Tree.branch.selection.text" but that didn't work as well as a couple other options but I'm confused when it comes to using tree view.  Does the code see the selection as a sub selection of the parent node or does it simply see the selection only?

               

              If the code doesn't see the selected item with it's parents node then I'll have to write a code that opens the node, checks all items, check for more nodes then repeats.  When it reaches the bottom without finding the selection it goes back to the parent node, moves down one and tries again.

               

              If the code finds the item.selected then it will take all the variables it has and put it into an array [Tree, Node, Subnodes, item.selected] so that I can then find the file location of what ever choice the user made.

               

               

              The whole purpose of the code I've made is to work exactly like the file prompt but without giving the user access to other options that I don't want them to get into.  Each node is a folder or sub folder and all the items are the templates inside the folder.

              • 4. Re: Trees, Branches and Items OHMY!
                Zantcor Level 1

                I figured it out with your help, the .selection is what I needed but I also needed the parents of the selection so that is what I did.  See the below section of code;

                 

                okbtn.onClick = function()
                {    
                    var topSelectionParent = cal_Tree.selection.parent.parent.text;
                    var selectionParent = cal_Tree.selection.parent.text;
                    var selecteditem = cal_Tree.selection.text;
                    
                    if (topSelectionParent == null)
                    {
                        alert ("You chose \n [ " + cal_Tree.selection.parent.text + " ] \n [ " + cal_Tree.selection.text + " ]")
                    } else {
                
                alert ("You chose \n [ " + cal_Tree.selection.parent.parent.text + " ] \n [ " + cal_Tree.selection.parent.text + " ] \n [ " + cal_Tree.selection.text + " ]")
                     }//end else
                }//end okbtn click
                
                

                 

                Since there is at most 2 parents I was able to set it up to always list the selection regardless if they are 1 deep or 2 deep into the tree. Thanks Silly-V and @W_J_T for your help.