6 Replies Latest reply: Aug 14, 2012 12:44 AM by zjbook RSS

    BIG Problem about save selection!

    zjbook Community Member

      Hi,everybody!

      I an new about js.  A few day ago, I wrote a "saveSelection dialog.jsx". It can work in "Dialog" windows type. BUT it can't work immediately while I change "Dialog" to "Palette", because I want to the palette stay the screen so as to I can use when I need.

       

      Could you help ME!

      Forgive me poor english, thanks in advance for your help

      below is code:

       

      // JavaScript Document
      //(function () {
          var doc=app.activeDocument,u,array=new Array();;
      var w=new Window("palette"),//dialog is work
      listPnl=w.add("panel");
      //listPnl.size=
      entry = listPnl.add ("edittext", u," Type the first letter"); //请输入首字进行查找
      entry.graphics.font = ScriptUI.newFont ("Arial", "", 16);
      entry.size=[200,30];
      entry.active = true;
      G_Slist=listPnl.add('group');
      G_Slist.orientation='row';
      Slist=G_Slist.add('listbox');//,u,u,{multiselect: true}
      Slist.alignment='left';
      Slist.size=[140,100];
      Slist.scrolling=true;
      orderG=G_Slist.add('group');
      orderG.orientation='column';
      orderPnl=orderG.add('panel');
      orderPnl.margins=2;
      orderPnl.size=[50,100];
      orderPnl.alignChildren=['fill','center'];
      var select=orderPnl.add("button", undefined, "SEL");
      select.size=[40,20];
      var up =orderPnl.add ("button", undefined, "Up");//Up
      up.size=[40,20];
      var down = orderPnl.add ("button", undefined, "Down");
      down.size=[40,20];
      addSel=listPnl.add('button{text:"+ add 增加选择到清单"}');
      addSel.size=[200,20];
      deleteSel=listPnl.add('button{text:"- delete 从清单中删除"}');
      deleteSel.size=[200,20];
      updateItems ();
      
      
      function updateItems() {
      if (doc.pageItems.length>0) {
          for (var i=0;i<doc.pageItems.length;i++) {
              if (doc.pageItems[i].note !='') {
                  if (!(Slist.find(doc.pageItems[i].note))) list=Slist.add('item',doc.pageItems[i].note);        
                  }
              }    
          }
      }
      
      
      
      addSel.onClick=function() {
          var p = prompt("Save Selection","Name the Selection");//存储所选对象请输入名称
          if (doc.selection.length>0 &&  p!=null){
              var list=Slist.add('item',p);
              for (var i=0;i<doc.selection.length ;i++ ){
                  doc.selection[i].note=p
                  }
              app.redraw()
              }
      
      
      //删除空项 delete null items
              for (var j=0;j<Slist.items.length;j++){
                  var replice=true;
                  var temp=Slist.items[j].text;
                  for (var k=0;k<doc.pageItems.length ;k++ ){
                  if (temp==doc.pageItems[k].note) replice=false;
                  }
              if (replice)   Slist.remove(temp);
              }
                updateItems ()
          }
      
      
      var temp;
      Slist.onDoubleClick=function(){
          doc.selection=null;
              var temp=Slist.selection.text;
              for (var i=0;i<doc.pageItems.length;i++) {
                  if (doc.pageItems[i].note ==temp) {
                      doc.pageItems[i].selected=true;
                     }
                 }
             app.redraw()        
          }
      
      
      deleteSel.onClick=function(){
          if (Slist.selection.length!=0) {
              var temp=Slist.selection.text;
              Slist.remove(temp);
              for (var i=0;i<doc.pageItems.length;i++) {
                  if (doc.pageItems[i].note ==temp) {
                      doc.pageItems[i].note='';
                     }
                 }
             }
         app.redraw();
         updateItems ();
         }
      
      
      select.onClick=function(){
          doc.selection=null;
              var temp=Slist.selection.text;
              for (var i=0;i<doc.pageItems.length;i++) {
                  if (doc.pageItems[i].note ==temp) {
                      doc.pageItems[i].selected=true;
                     }
                 }
             app.redraw()     
          }
      
      
      up.onClick = function (){
          var n = Slist.selection.index;
          if (n > 0){
              swap (Slist.items [n-1], Slist.items [n]);
              Slist.selection = n-1;
              }
          }
      
      
      
      
      down.onClick = function (){
          var n = Slist.selection.index;
          if (n < Slist.items.length-1){
              swap (Slist.items [n], Slist.items [n+1]);
              Slist.selection = n+1;
              }
          }
      
      
      function swap (x, y){
          var temp = x.text;
          x.text = y.text;
          y.text = temp;
          }
      
      
      
      
      
      
      var array=new Array();
      for (var n=0;n<Slist.items.length;n++) {
          array.push(Slist.items[n].text)
          }
      entry.onChanging = function (){
          var tmp = this.text;
          tmp=tmp.toString();
          Slist.removeAll ();
          //if (tmp!='') {
              for (var m = 0; array[m]; m++) {
                  if (array[m].indexOf (tmp) == 0) list=Slist.add ('item', array[m]);
                  if (Slist.items.length > 0)   Slist.selection = 0;
                  }
              }
      
      w.onClick=w.onMove=function () {
        entry.text='';
        entry.active=true;
      }
      
      
      
      
      /*
      entry.onChange = function () {
              var array1=new Array();
          for (var n=0;n<Slist.items.length;n++) {
              array1.push(Slist.items[n].text)
              }
          for (var m1 = 0; array1[m1]; m1++) {
              list=Slist.add ('item', array1[m1]);
              }
          }*/
      
      
      
      
      /*
      function ShiftList (){
          if (this.selection != null)  {
              var idx = this.selection.pop().index;
              if (idx < this.items.length)  this.revealItem (idx);    
              }
          }
       */ 
      
      
      
      w.show()
      
      
      //}
      //)()