11 Replies Latest reply on Aug 19, 2016 6:16 AM by Kai Rübsamen

    Create an UI for a script! …

    Obi-wan Kenobi Adobe Community Professional

      Hi all,

       

      I think I had to split this topic part!

      [ see:  How to quickly change cell stroke weight  ]

       

      Here's 2 variants of a tool thought by Uwe Laubender, Skemicle and your servant!

       

      This 1rst script allows the op to change to 0.5 pt the cell stroke weights in all the tables in a document if they are not equal to zero.

       

       

      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;      
      app.doScript      
          (      
          main,      
          ScriptLanguage.JAVASCRIPT,      
          [],      
          UndoModes.ENTIRE_SCRIPT,      
          "Change Tables Stroke Weights!"      
          );      
        
      function main()      
      {      
          var newWeight = 0.5;    
        
        
          if(app.documents.length == 0){return};  
          if(app.documents[0].stories.length == 0){return};  
          if(app.documents[0].stories.everyItem().tables.length == 0){return};  
              
          var doc = app.documents[0];      
          var allStories = doc.stories.everyItem();      
          var allCells = allStories.tables.everyItem().cells.everyItem().getElements();      
            
          for(var n=0;n<allCells.length;n++)      
              {      
                  if(allCells[n].topEdgeStrokeWeight != 0){   
                      if(allCells[n].topEdgeStrokeWeight != newWeight){      
                         allCells[n].topEdgeStrokeWeight = newWeight  
                        }  
                    }  
              };    
          
          for(var n=0;n<allCells.length;n++)      
              {      
                  if(allCells[n].bottomEdgeStrokeWeight != 0){   
                      if(allCells[n].bottomEdgeStrokeWeight != newWeight){      
                         allCells[n].bottomEdgeStrokeWeight = newWeight  
                        }  
                    }  
              };    
          
          for(var n=0;n<allCells.length;n++)      
              {      
                  if(allCells[n].leftEdgeStrokeWeight != 0){   
                      if(allCells[n].leftEdgeStrokeWeight != newWeight){      
                         allCells[n].leftEdgeStrokeWeight = newWeight  
                        }  
                    }  
              };    
          
          for(var n=0;n<allCells.length;n++)      
              {      
                  if(allCells[n].rightEdgeStrokeWeight != 0){   
                      if(allCells[n].rightEdgeStrokeWeight != newWeight){      
                         allCells[n].rightEdgeStrokeWeight = newWeight  
                        }  
                    }  
              };    
                
      };     
      

       

       

      This second script allows the op to choose what cell stroke weights in all the tables in a document he wants to change (to 0.5 pt):

       

      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;      
      app.doScript      
          (      
               
          main,      
          ScriptLanguage.JAVASCRIPT,      
          [],      
          UndoModes.ENTIRE_SCRIPT,      
          "Change Stroke Weight"      
               
          );      
            
      function main()      
      {      
              
      var weight = Number(prompt("What is your starting weight?", 1,"Current Stroke Weight"));    
      var newWeight = 0.5;      
            
      if(app.documents.length == 0){return};      
      if(app.documents[0].stories.length == 0){return};      
      if(app.documents[0].stories.everyItem().tables.length == 0){return};      
              
      var doc = app.documents[0];      
      var allStories = doc.stories.everyItem();      
      var allCells = allStories.tables.everyItem().cells.everyItem().getElements();      
          
            
            
      for(var n=0;n<allCells.length;n++)      
      {      
          if(allCells[n].bottomEdgeStrokeWeight != weight){continue};      
          allCells[n].bottomEdgeStrokeWeight = newWeight;      
      };      
                  
      for(var n=0;n<allCells.length;n++)      
      {      
          if(allCells[n].leftEdgeStrokeWeight != weight){continue};      
          allCells[n].leftEdgeStrokeWeight = newWeight;      
      };      
              
      for(var n=0;n<allCells.length;n++)      
      {      
          if(allCells[n].rightEdgeStrokeWeight != weight){continue};      
          allCells[n].rightEdgeStrokeWeight = newWeight;      
      };      
              
      for(var n=0;n<allCells.length;n++)      
      {      
          if(allCells[n].topEdgeStrokeWeight != weight){continue};      
          allCells[n].topEdgeStrokeWeight = newWeight;      
      };    
          
      };     
      
      
      
      

       

      I think it would be cooler to have this UI and this game:

       

      • type * if the op wants to treat all the strokes (except 0);

      • type a number if he wants to treat a specific stroke weight.

       

       

      Capture d’écran 2016-08-17 à 16.49.54.png

       

      I get this UI like this:

       

      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;    
      app.doScript    
          (    
          main,    
          ScriptLanguage.JAVASCRIPT,    
          [],    
          UndoModes.ENTIRE_SCRIPT,    
          "Change Tables Stroke Weights!"    
          );    
      
      
      // Here's the UI
      
      
      var w = new Window ('dialog {alignChildren: "right"}', 'Change Tables Stroke Weights!');
        var group1 = w.add ('group');
        group1.add ('statictext {text: "Cells stroke to be replaced:"}');
        var UIWeight = group1.add ("edittext", undefined, 1);
         UIWeight.minimumSize.width = 50;
          UIWeight.maximumSize.width = 50;
        UIWeight.active = true;
      
        var group2 = w.add ('group');
        group2.add ('statictext {text: "… by:"}');
        var UINewWeight = group2.add ("edittext", undefined, 0.5);
         UINewWeight.minimumSize.width = 50;
        UINewWeight.maximumSize.width = 50;
      
      
        var buttons = w.add ('group {alignment: "center"}');
        buttons.add ('button {text: "OK"}');
        buttons.add ('button {text: "Cancel"}');
      
      
      if (w.show() == 2) {  // Cancel pressed
        exit();
      }
      
      
      // Now the script:
      
      
      function main()    
      {    
      

       

       

      … But, at this step, I'm totally unable to continue! …

       

      Thanks for all your help and your nice advices to not to sink in the dark side of The Force! 

       

      MTFBWY!

        • 1. Re: Create an UI for a script! …
          Skemicle Level 3

          The script will now enable the op to select a starting stroke weight and choose to either change all stroke weights of that value or keep that weight and change all other stroke weights to a specified value. Hopefully this works.

           

           

          /*

          Scripted and idealised by:

              Obi-wan Kenobi

              Laubender

              Skemicle

          */

          var myDialog = app.dialogs.add({name:"Change Stroke Weight"});

          with(myDialog.dialogColumns.add()){

              with(dialogRows.add()){

                  staticTexts.add({staticLabel:"Stroke Weight to:"});

                  var myVerticalAlignmentButtons = radiobuttonGroups.add();

                  with(myVerticalAlignmentButtons){

                      var changeButton = radiobuttonControls.add({staticLabel:"Change", checkedState: true});

                      var keepButton = radiobuttonControls.add({staticLabel:"Keep"});

                  }

                  var strokeWeightField1 = textEditboxes.add({editContents:"1"});

                  with(dialogRows.add()){

                      staticTexts.add({staticLabel:"Stroke Weight to change to:"});

                      var strokeWeightField2 = textEditboxes.add({editContents:"0.5"});

                  }

              }

              if(myDialog.show() == 1){

                  if(changeButton.checkedState == true){

                      app.doScript(changeSpecifiedWeight,ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT," Change Tables Stroke Weights!");

                  }

                  if(keepButton.checkedState == true){

                      app.doScript(keepSpecifiedWeight,ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,"Ke ep One Table Stroke Weight!");

                  }

              }

          }

          ////////////////////////////////////////////////////////////////////////////////////////// ///////////////////

          function keepSpecifiedWeight(){

              var Weight = strokeWeightField1.editContents;

              var newWeight = strokeWeightField2.editContents; 

              if(app.documents.length == 0){return};  

              if(app.documents[0].stories.length == 0){return};  

              if(app.documents[0].stories.everyItem().tables.length == 0){return};  

              var doc = app.documents[0];      

              var allStories = doc.stories.everyItem();      

              var allCells = allStories.tables.everyItem().cells.everyItem().getElements();      

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

                  if(allCells[n].topEdgeStrokeWeight != Weight){   

                      allCells[n].topEdgeStrokeWeight = newWeight;  

                  }  

              }    

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

                  if(allCells[n].bottomEdgeStrokeWeight != Weight){

                      allCells[n].bottomEdgeStrokeWeight = newWeight;

                  }  

              }

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

                  if(allCells[n].leftEdgeStrokeWeight != Weight){     

                      allCells[n].leftEdgeStrokeWeight = newWeight;

                  }  

              }

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

                  if(allCells[n].rightEdgeStrokeWeight != Weight){

                      allCells[n].rightEdgeStrokeWeight = newWeight;

                  }

              }

          }

          ////////////////////////////////////////////////////////////////////////////////////////// ///////////////////

          function changeSpecifiedWeight(){

              var Weight = strokeWeightField1.editContents;

              var newWeight = strokeWeightField2.editContents;

              if(app.documents.length == 0){return};

              if(app.documents[0].stories.length == 0){return};

              if(app.documents[0].stories.everyItem().tables.length == 0){return};

              var doc = app.documents[0];

              var allStories = doc.stories.everyItem();

              var allCells = allStories.tables.everyItem().cells.everyItem().getElements();

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

                  if(allCells[n].topEdgeStrokeWeight != Weight){

                      continue

                  }

                  allCells[n].topEdgeStrokeWeight = newWeight;

              }

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

                  if(allCells[n].bottomEdgeStrokeWeight != Weight){

                      continue

                  }

                  allCells[n].bottomEdgeStrokeWeight = newWeight;

              }

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

                  if(allCells[n].leftEdgeStrokeWeight != Weight){

                      continue

                  }

                  allCells[n].leftEdgeStrokeWeight = newWeight;

              }

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

                  if(allCells[n].rightEdgeStrokeWeight != Weight){

                      continue

                  }

              allCells[n].rightEdgeStrokeWeight = newWeight;

              }

          }

          • 2. Re: Create an UI for a script! …
            Obi-wan Kenobi Adobe Community Professional

            Cool!  It works very fine! Thanks a lot! 

             

            The "keepSpecifiedWeight()" function is a very clever! 

             

            Skemicle, I let you finish the adding of Uwe's script? 

             

            I think about something: It could interesting to "isolate" the "0 pt" stroke (using a checkbox).

            Same thing with Uwe's part (checkbox).

            … and why not: color + stroke style!?

             

             

            • 3. Re: Create an UI for a script! …
              Skemicle Level 3

              Obi-wan Kenobi wrote:

               

              It could interesting to "isolate" the "0 pt" stroke (using a checkbox).

               

              What do you mean?

               

              and when i get some time i will definetely look into adding uwe's "only if all four sides are the same" and possibly the stroke styles/colors

              • 4. Re: Create an UI for a script! …
                Obi-wan Kenobi Adobe Community Professional

                About 0 pt stroke, it's a particular case because if 0, we have no stroke (of course, aha!).

                 

                If, like in the first screenshot, we have 5 pts, 3 pts, 1 pt, 0,5 pt (by default) and 0 pt, and want 2 pts for the three first, we can't use: "Keep 0" because all we have in 0,5 pt will disappear! Actually, we need to launch the script 3 times to "change" 5 pts, 3 pts and 1 pt.

                It's not really a problem!

                About the last point, see the last screenshot, the script can't do this! … 

                 

                Capture d’écran 2016-08-17 à 23.15.02.png

                Capture d’écran 2016-08-17 à 23.15.23.png

                Capture d’écran 2016-08-17 à 23.16.39.png

                Capture d’écran 2016-08-17 à 23.18.34.png

                 

                (^/)   Good night!

                • 5. Re: Create an UI for a script! …
                  Skemicle Level 3

                  Oh i think i understand now. You are wanting to keep both '0' and '0.5' weights and change everything except those two?

                  • 6. Re: Create an UI for a script! …
                    Obi-wan Kenobi Adobe Community Professional

                    Yep! 

                     

                    "0 pt" is a very particular case because we can use it to build a table [as "no color" stroke].

                    • 7. Re: Create an UI for a script! …
                      Skemicle Level 3

                      Here is the UI I have..

                       

                      /*

                      Scripted and idealised by:

                      Obi-wan Kenobi

                      Laubender

                      Skemicle

                      */

                      var userStrokeStyles = [];

                      var userSwatches = [];

                      var myDialog = app.dialogs.add({name:"Change Stroke Weight"});

                      with(myDialog.dialogColumns.add()){

                          with(dialogRows.add()){

                              staticTexts.add({staticLabel:"Stroke Weight to:"});

                              var myVerticalAlignmentButtons = radiobuttonGroups.add();

                              with(myVerticalAlignmentButtons){

                                  var changeButton = radiobuttonControls.add({staticLabel:"Change", checkedState: true});

                                  var keepButton = radiobuttonControls.add({staticLabel:"Keep"});

                              }

                              var strokeWeightField1 = textEditboxes.add({editContents:"1"});

                              with(dialogRows.add()){

                                  staticTexts.add({staticLabel:"Stroke Weight to change to:"});

                                  var strokeWeightField2 = textEditboxes.add({editContents:"0.5"});

                              }

                              with(dialogRows.add()){

                                  checkboxControls.add({staticLabel:"Only if all four strokes are the same", checkedState:false});

                                  checkboxControls.add({staticLabel:"Keep \"0pt\" Stroke", checkedState:true});

                              }

                              with(dialogRows.add()){

                                  staticTexts.add({staticLabel:"Apply new stroke styles"});

                              }

                              with(dialogRows.add()){

                              staticTexts.add({staticLabel:"Select stroke style to apply:"});

                              dropdowns.add();

                          }

                              with(dialogRows.add()){

                              staticTexts.add({staticLabel:"Select stroke color to apply:"});

                              dropdowns.add();

                          }

                          }

                          if(myDialog.show() == 1){

                              if(changeButton.checkedState == true){

                                  app.doScript(changeSpecifiedWeight,ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT," Change Tables Stroke Weights!");

                              }

                              if(keepButton.checkedState == true){

                                  app.doScript(keepSpecifiedWeight,ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,"Ke ep One Table Stroke Weight!");

                              }

                          }

                      }

                       

                       

                      I'm not entirely sure how to save the user's swatches/stroke styles to an array to use in the dropdowns, but i will get it eventually I'm sure =P. Anything else that should be added?

                      • 8. Re: Create an UI for a script! …
                        Obi-wan Kenobi Adobe Community Professional

                        Cool! The UI truly becomes to be … Pro! Thanks for the work!

                         

                        The last features I think it would be interesting to add:

                        the capacity for the script to apply (or not // via a checkbox) "Cell styles"! 

                        the capacity to play with: all tables (doc), all tables (story), selected table and tables defined by a table style (drop list)!

                         

                        I know it's a big deal and nobody will pay us for this (but a lot will play with the script if it's finished one day)! 

                        • 9. Re: Create an UI for a script! …
                          Kai Rübsamen Level 4

                          Guys,

                           

                          Obi used Script-Ui, Skemicle changed this to a normal dialog. So, the result of 'dialog.show()' is either true or false, not 1 or 2!

                           

                          Beside this: I’m astonished, that there is a need of the same for-loop 4 times?

                          • 10. Re: Create an UI for a script! …
                            Skemicle Level 3

                            Kai Rübsamen wrote:

                             

                            Obi used Script-Ui, Skemicle changed this to a normal dialog. So, the result of 'dialog.show()' is either true or false, not 1 or 2!

                            Is true/false not equivalent to 1/0?

                            • 11. Re: Create an UI for a script! …
                              Kai Rübsamen Level 4

                              Skemicle, you’re right. I did not know that before. So thanks for clarification and sorry for confusion …