3 Replies Latest reply on Jun 21, 2018 2:46 AM by takeyamaatsushi

    Different color of text between square brackets

    ubusam Level 1

      Hi,

      I'm working on script where user can put the square brackets around some text and that text will be different color.

      The script is working when user enter the one pair of brackets and in only in one line. For example "Enter [your] text here".

      But it doesn't work in multi line and when you try to add more brackets, for example "Enter [your] text [here]".

      This is the script:

      {
      function myScript(thisObj){
           function myScript_buildUI(thisObj){
               var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", undefined, {resizeable:true});
      //================================================================================================================
      var textBox = myPanel.add("edittext", undefined, "Enter [your] text here!", {multiline:true});     
          textBox.minimumSize = [284,100];
      var myButton = myPanel.add("button",undefined,"CREATE TEXT");
      myButton.onClick = createMain;
      //================================================================================================================
      function createMain(){
      var textBoxContents = textBox.text;
      
      var lines = textBoxContents.replace(/ {1,}/g,"[").replace(/\[{1,}/g," ").replace(/ {1,}/g,"]").replace(/\]{1,}/g," ").split(/\n|\r/);
      
      var indexStart = textBoxContents.indexOf("[");
      var indexEnd = textBoxContents.indexOf("]");
      
      createText(lines,indexStart,indexEnd);
      
      }
      function createText(line,start,end){
          for (var i = 0; i < line.length; i++) {
              var myLayer = app.project.activeItem.layers.addText(line[i]);
      
              indexStartEnd(1);
      
              function indexStartEnd(prop){
                  for (var i = 0; i < line.length; i++) {
                  myLayer.property("ADBE Text Properties").property(4).addProperty("ADBE Text Animator");
                  var txtProp = myLayer.property("ADBE Text Properties").property(4).property(1);
                  txtProp.property("ADBE Text Animator Properties").addProperty("ADBE Text Fill Color");
                  txtProp.property(1).addProperty("ADBE Text Selector");
                  txtProp.property(1).property(prop).property(7).property("ADBE Text Range Units").setValue(2);
                  txtProp.property(1).property(prop).property("ADBE Text Index Start").setValue(start);
                  txtProp.property(1).property(prop).property("ADBE Text Index End").setValue(end);
                  txtProp.property(2).property("ADBE Text Fill Color").setValue([0,0.6,0,1]);
                  }
              }    
          }
      }
      //================================================================================================================
              myPanel.layout.layout(true);
              myPanel.minimumSize = myPanel.size;        
              myPanel.layout.resize();
              myPanel.onResizing = myPanel.onResize = function(){this.layout.resize()};                 
               return myPanel;
               }
              var myScriptPal = myScript_buildUI (thisObj);
              
              if((myScriptPal != null) && (myScriptPal instanceof Window)){
                  myScriptPal.center();
                  myScriptPal.show();
              }
      }
      myScript(this);
      }
      
        • 1. Re: Different color of text between square brackets
          takeyamaatsushi Level 2

          Hi

           

          If indexStart & indexEnd is one variable number, then can only find first [ ] set.

           

          Check below example

          Sorry my poor English

           

          {
              function myScript(thisObj) {
                  function myScript_buildUI(thisObj) {
                      var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "test", undefined, { resizeable: true });
                      //================================================================================================================  
                      var textBox = myPanel.add("edittext", undefined, "Enter [your] text here!", { multiline: true });
                      textBox.minimumSize = [284, 100];
                      var myButton = myPanel.add("button", undefined, "CREATE TEXT");
                      myButton.onClick = createMain;
                      //================================================================================================================  
                      function createMain() {
                          var textBoxContents = textBox.text;
          
                          var lines = textBoxContents.split(/\n|\r/);
          
                          var lineObjs = [];
          
                          for (var i = 0; i < lines.length; i++) {
                              lines[i] = lines[i].replace(/ *\[ */g, "[").replace(/ *\] */g, "]").replace(/ +/g, " ");   //Enter [your] text [here!] -> Enter[your]text[here!]
          
                              //get indexes
                              var indexStarts = [];
                              var indexEnds = [];
                              var curIndex = 0;
                              while (true) {
                                  var indexStart = lines[i].indexOf("[", curIndex + 1);
                                  curIndex = indexStart;
          
                                  var indexEnd = lines[i].indexOf("]", curIndex + 1);
                                  curIndex = indexEnd;
                                  
                                  if (indexStart >= 0 && indexEnd >= 0) {
                                      //if you find [ & ] set
                                      indexStarts.push(indexStart);
                                      indexEnds.push(indexEnd);
                                  } else {
                                      break;
                                  }
                              }
          
                              lines[i] = lines[i].replace(/\[|\]/g, " ");    //Enter[your] text [here!] -> Enter your text here!
                              
                              lineObjs.push({
                                  "text": lines[i],
                                  "indexStarts": indexStarts,
                                  "indexEnds": indexEnds
                              });
                          }
          
                          createText(lineObjs);
                      }
          
                      function createText(lineObjs) {
                          for (var i = 0; i < lineObjs.length; i++) {
                              var thisLine = lineObjs[i];
          
                              var myLayer = app.project.activeItem.layers.addText(thisLine.text);
          
                              for (var j = 0; j < thisLine.indexStarts.length; j++) {
                                  var propNumber = j + 1  //property index start at 1
          
                                  myLayer.property("ADBE Text Properties").property(4).addProperty("ADBE Text Animator");
                                  var txtProp = myLayer.property("ADBE Text Properties").property(4).property(1);
                                  txtProp.property("ADBE Text Animator Properties").addProperty("ADBE Text Fill Color");
                                  txtProp.property(1).addProperty("ADBE Text Selector");
                                  txtProp.property(1).property(propNumber).property(7).property("ADBE Text Range Units").setValue(2);
                                  txtProp.property(1).property(propNumber).property("ADBE Text Index Start").setValue(thisLine.indexStarts[j]);
                                  txtProp.property(1).property(propNumber).property("ADBE Text Index End").setValue(thisLine.indexEnds[j]);
                                  txtProp.property(2).property("ADBE Text Fill Color").setValue([0, 0.6, 0, 1]);
                              }
                          }
                      }
                      //================================================================================================================  
                      myPanel.layout.layout(true);
                      myPanel.minimumSize = myPanel.size;
                      myPanel.layout.resize();
                      myPanel.onResizing = myPanel.onResize = function () { this.layout.resize() };
                      return myPanel;
                  }
                  var myScriptPal = myScript_buildUI(thisObj);
          
                  if ((myScriptPal != null) && (myScriptPal instanceof Window)) {
                      myScriptPal.center();
                      myScriptPal.show();
                  }
              }
              myScript(this);
          }
          
          1 person found this helpful
          • 2. Re: Different color of text between square brackets
            ubusam Level 1

            Thank you so much takeyamaatsushi,

            I was on the wrong way.

            Thank you for putting the comments in the code so I can understand it better.

            This just made my day

            You're my hero...

            • 3. Re: Different color of text between square brackets
              takeyamaatsushi Level 2

              Sorry, my code cannot check line whitch starts with "["

               

              please use below...

               

                                  while (true) {  
                                      var indexStart = lines[i].indexOf("[", curIndex);  
                                      curIndex = indexStart + 1;  
                
                                      var indexEnd = lines[i].indexOf("]", curIndex);  
                                      curIndex = indexEnd + 1;  
              
              1 person found this helpful