12 Replies Latest reply on Jul 27, 2014 3:59 AM by Jump_Over

    How can I show the swatches.everyItem in the dropdown list

    Johnwhite Level 1

      Hi, everyone

       

      Can you correct my script, aim to let it show the swatches.everyItem in the dropdown list

       

      if ( app.selection.length > 0 && ( app.selection[0].constructor.name == "Cell" || app.selection[0].constructor.name == "Table" ) ) {  
          if ( app.scriptPreferences.version >= 6 ) { 
              app.doScript(main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "style every second column in selection" );          
          }  
          else { 
              main(); 
          } 
      } else { 
          alert ( "Nothing or wrong selection!" ); 
      }

       

      //---------------------------------------------------------------------------------------- --------------has some prolems in this part


      var sN = doc.swatches.everyItem ().name, 
          sID = doc.swatches.everyItem ().id,

       

      function main() {

         var l, c = 0, o, p, pO = {}, pA = []; 
           if (s == null) {alert("No Swatch Selected") ; return;};               // I want to show swatches.everyItem.name as myColor
          s = doc.swatches.itemByID (sID[s.index]);

      //---------------------------------------------------------------------------------------- --------------has some prolems in this part

       

       


          var myFillTint = 20; 
          var myCharStyleName = "Bold"; 
          var myStrokeWeight = 0.5; 
       
          var curSel = app.selection[0]; 
          var allCells = curSel.cells; 
          var startCol = curSel.cells[0].name.split(":")[0]*1;    
          var endCol = curSel.cells[-1].name.split(":")[0]*1; 
          var startRow = curSel.cells[0].name.split(":")[1]*1;    
          var endRow = curSel.cells[-1].name.split(":")[1]*1;   
          var counter = startCol + 1; 
       
          for ( var i = 0 ; i < allCells.length; i++ ) { 
              var curCell = allCells[i]; 
              var curCol = curCell.name.split(":")[0]*1; 
              var curRow = curCell.name.split(":")[1]*1; 
       
              if ( curCol == counter ) { 
                  with ( curCell ) { 
                      fillColor = myColor; 
                      fillTint = myFillTint; 
                      texts[0].appliedCharacterStyle = myCharStyleName; 
                      rightEdgeStrokeWeight = myStrokeWeight; 
                      rightEdgeStrokeColor= myColor;  
                      leftEdgeStrokeWeight = myStrokeWeight; 
                      leftEdgeStrokeColor= myColor;                  
                  } 
                  if ( curRow == startRow ) { 
                      curCell.topEdgeStrokeWeight = myStrokeWeight; 
                      curCell.topEdgeStrokeColor= myColor; 
                  } 
                  else if (curRow == endRow ) { 
                      curCell.bottomEdgeStrokeWeight = myStrokeWeight; 
                      curCell.bottomEdgeStrokeColor= myColor; 
                  } 
                   
                  counter = counter + 2; 
              } 
               
              if ( counter > endCol ) { 
                  counter = startCol + 1; 
              } // end if 
          } // end for 
      } // end main

       

       

      John

        • 1. Re: How can I show the swatches.everyItem in the dropdown list
          Jump_Over Level 5

          Hi,

           

          Looks like careless copy-pasted.

           

          //...
          function main() {
            var l, c = 0, o, p, pO = {}, pA = [];      //    var declared but not used
              if (s == null)                           //    var used but not declared
          //...
          

           

          Jarek

          • 2. Re: How can I show the swatches.everyItem in the dropdown list
            Johnwhite Level 1

            Hi, Jarek

             

            How can fix it?

             

            John

            • 3. Re: How can I show the swatches.everyItem in the dropdown list
              Jump_Over Level 5

               

              How can fix it?

               

              John.

              1st at all - a main part of your code is trying to format cells in a selection, which looks like out of your subject's goal..

              2nd - there is no even one line in your code which is trying to show a list of swatches to user.

              3rd - your code's main logic is - do main() if some condition == true and do main() if this condition ==  false.

              4th - your code is executing main() function with no variables declared.

               

              There is nothing to fix, I am afraid. Better to create it from an empty point.

               

              Jarek

              • 4. Re: How can I show the swatches.everyItem in the dropdown list
                Johnwhite Level 1

                Hi Jarek,

                 

                the orginal script is:

                 

                //-------------------------------------------------------------------------------------

                 

                if ( app.selection.length > 0 && ( app.selection[0].constructor.name == "Cell" || app.selection[0].constructor.name == "Table" ) ) {  

                    if ( app.scriptPreferences.version >= 6 ) { 

                        app.doScript( main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "style every second column in selection" );          

                    }  

                    else { 

                        main(); 

                    } 

                } else { 

                    alert ( "Nothing or wrong selection!" ); 

                 

                function main() { 

                     

                    var myColor = "Black"; 

                    var myFillTint = 20; 

                    var myCharStyleName = "Bold"; 

                    var myStrokeWeight = 0.5; 

                 

                    var curSel = app.selection[0]; 

                    var allCells = curSel.cells; 

                    var startCol = curSel.cells[0].name.split(":")[0]*1;    

                    var endCol = curSel.cells[-1].name.split(":")[0]*1; 

                    var startRow = curSel.cells[0].name.split(":")[1]*1;    

                    var endRow = curSel.cells[-1].name.split(":")[1]*1;   

                    var counter = startCol + 1; 

                 

                    for ( var i = 0 ; i < allCells.length; i++ ) { 

                        var curCell = allCells[i]; 

                        var curCol = curCell.name.split(":")[0]*1; 

                        var curRow = curCell.name.split(":")[1]*1; 

                 

                        if ( curCol == counter ) { 

                            with ( curCell ) { 

                                fillColor = myColor; 

                                fillTint = myFillTint; 

                                texts[0].appliedCharacterStyle = myCharStyleName; 

                                rightEdgeStrokeWeight = myStrokeWeight; 

                                rightEdgeStrokeColor= myColor;  

                                leftEdgeStrokeWeight = myStrokeWeight; 

                                leftEdgeStrokeColor= myColor;                  

                            } 

                            if ( curRow == startRow ) { 

                                curCell.topEdgeStrokeWeight = myStrokeWeight; 

                                curCell.topEdgeStrokeColor= myColor; 

                            } 

                            else if (curRow == endRow ) { 

                                curCell.bottomEdgeStrokeWeight = myStrokeWeight; 

                                curCell.bottomEdgeStrokeColor= myColor; 

                            } 

                             

                            counter = counter + 2; 

                        } 

                         

                        if ( counter > endCol ) { 

                            counter = startCol + 1; 

                        } // end if 

                    } // end for 

                } // end main

                 

                //---------------------------------------------------------------------------------------- ---------------------------------------

                 

                I want to add a function aim to show my swatches.everyItem then I selected a colour for fillColor

                 

                How can I do that

                 

                John

                • 5. Re: Re: How can I show the swatches.everyItem in the dropdown list
                  Jump_Over Level 5

                  John,

                   

                  In this case your main function should start with a dialog window to take user choice.

                  The rest of function stay as it is. So:

                  //...
                  function main() {
                  var
                    w = new Window("dialog","What is your choice", undefined, {closeButton: false}),
                    mPanel1 = w.add ("panel",undefined, "choose a color"),
                    mPanel2 = w.add ("panel",undefined, "choose a charStyle"),
                    mCoList = mPanel1.add("dropdownlist",undefined,app.activeDocument.swatches.everyItem().name),
                    mStyList = mPanel2.add("dropdownlist",undefined,app.activeDocument.characterStyles.everyItem().name),
                    b = w.add("group");
                  
                    mCoList.items[0].selected = mStyList.items[0].selected = true;
                    w.alignChildren= "left";
                    b.add ("button", undefined, "OK", {name: "ok"});
                    b.add ("button", undefined, "Anuluj", {name: "cancel"});
                  
                    if (w.show()) {
                    var mColor = mCoList.selection.text;
                    var mCharStyleName = mStyList.selection.text;
                    }
                    else exit();
                    
                    var myFillTint = 20;
                   // var ...
                   // ...
                    }
                  

                   

                  Jarek

                  • 6. Re: How can I show the swatches.everyItem in the dropdown list
                    Johnwhite Level 1

                    Hi Jarek,

                     

                    I changed it into this:

                     

                    but I got an error.

                     

                    //==================================================

                    if ( app.selection.length > 0 && ( app.selection[0].constructor.name == "Cell" || app.selection[0].constructor.name == "Table" ) ) {  

                        if ( app.scriptPreferences.version >= 6 ) { 

                            app.doScript( main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "style every second column in selection" );     // the error source is this line     

                        }  

                        else { 

                            main(); 

                        } 

                    } else { 

                        alert ( "Nothing or wrong selection!" ); 

                    function main() { 

                    var 

                      w = new Window("dialog","What is your choice", undefined, {closeButton: false}), 

                      mPanel1 = w.add ("panel",undefined, "choose a color"), 

                      mPanel2 = w.add ("panel",undefined, "choose a charStyle"), 

                      mCoList = mPanel1.add("dropdownlist",undefined,app.activeDocument.swatches.everyItem().name), 

                      mStyList = mPanel2.add("dropdownlist",undefined,app.activeDocument.characterStyles.everyItem().name) , 

                      b = w.add("group"); 

                     

                      mCoList.items[0].selected = mStyList.items[0].selected = true; 

                      w.alignChildren= "left"; 

                      b.add ("button", undefined, "OK", {name: "ok"}); 

                      b.add ("button", undefined, "Anuluj", {name: "cancel"}); 

                     

                      if (w.show()) { 

                      var mColor = mCoList.selection.text; 

                      var mCharStyleName = mStyList.selection.text; 

                      } 

                      else exit(); 

                       

                        var myFillTint = 20; 

                        var myCharStyleName = "Bold"; 

                        var myStrokeWeight = 0.5; 

                     

                        var curSel = app.selection[0]; 

                        var allCells = curSel.cells; 

                        var startCol = curSel.cells[0].name.split(":")[0]*1;    

                        var endCol = curSel.cells[-1].name.split(":")[0]*1; 

                        var startRow = curSel.cells[0].name.split(":")[1]*1;    

                        var endRow = curSel.cells[-1].name.split(":")[1]*1;   

                        var counter = startCol + 1; 

                     

                        for ( var i = 0 ; i < allCells.length; i++ ) { 

                            var curCell = allCells[i]; 

                            var curCol = curCell.name.split(":")[0]*1; 

                            var curRow = curCell.name.split(":")[1]*1; 

                     

                            if ( curCol == counter ) { 

                                with ( curCell ) { 

                                    fillColor = myColor; 

                                    fillTint = myFillTint; 

                                    texts[0].appliedCharacterStyle = myCharStyleName; 

                                    rightEdgeStrokeWeight = myStrokeWeight; 

                                    rightEdgeStrokeColor= myColor;  

                                    leftEdgeStrokeWeight = myStrokeWeight; 

                                    leftEdgeStrokeColor= myColor;                  

                                } 

                                if ( curRow == startRow ) { 

                                    curCell.topEdgeStrokeWeight = myStrokeWeight; 

                                    curCell.topEdgeStrokeColor= myColor; 

                                } 

                                else if (curRow == endRow ) { 

                                    curCell.bottomEdgeStrokeWeight = myStrokeWeight; 

                                    curCell.bottomEdgeStrokeColor= myColor; 

                                } 

                                 

                                counter = counter + 2; 

                            } 

                             

                            if ( counter > endCol ) { 

                                counter = startCol + 1; 

                            } // end if 

                        } // end for 

                    } // end main

                     

                    //==================================================

                     

                    how can fix it?

                     

                    John

                    • 7. Re: How can I show the swatches.everyItem in the dropdown list
                      Jump_Over Level 5

                      ... what error? paste the message

                      Jarek

                      • 8. Re: How can I show the swatches.everyItem in the dropdown list
                        Johnwhite Level 1

                        Hi Jarek

                         

                        myColor undefined

                         

                        but the error source is:

                         

                                app.doScript( main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "style every second column in selection" );         

                         

                        I don't konw why

                         

                        John

                        • 9. Re: How can I show the swatches.everyItem in the dropdown list
                          Jump_Over Level 5

                          check var names and share the same version:

                               mColor vs myColor

                               mCharStyleName vs myCharStyleName

                           

                          (are you going to use a 2nd dropDownList as well?)

                           

                          Notice that I am just showing the way. I am not an end_product_supplier.

                           

                          Jarek

                          • 10. Re: How can I show the swatches.everyItem in the dropdown list
                            Johnwhite Level 1

                            sorry Jarek

                             

                            I made two typos

                             

                            now it works

                             

                            thank you so much,

                             

                            and very very sory

                             

                            John

                            • 11. Re: How can I show the swatches.everyItem in the dropdown list
                              Johnwhite Level 1

                              Hi jarek

                               

                              inspire by your idea

                               

                              I want and need to add the dropdown list for FillTint percentage

                               

                              and point of the StrokeWeight

                               

                              and stroke color

                               

                              can you please give me an idea

                               

                              Regard

                               

                              John

                              • 12. Re: How can I show the swatches.everyItem in the dropdown list
                                Jump_Over Level 5

                                Hi,

                                 

                                Idea is given already

                                All you need is to add next object to a dialog instead of "hard-defining" variable.

                                 

                                Try yourself!    ... and enjoy scripting

                                 

                                Jarek