Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Different color of text between square brackets

Community Beginner ,
Jun 20, 2018 Jun 20, 2018

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);

        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);

}

TOPICS
Scripting
743
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Jun 21, 2018 Jun 21, 2018

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("edit

...
Translate
Explorer ,
Jun 21, 2018 Jun 21, 2018

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 = lines.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.indexOf("[", curIndex + 1);

                        curIndex = indexStart;

                        var indexEnd = lines.indexOf("]", curIndex + 1);

                        curIndex = indexEnd;

                       

                        if (indexStart >= 0 && indexEnd >= 0) {

                            //if you find [ & ] set

                            indexStarts.push(indexStart);

                            indexEnds.push(indexEnd);

                        } else {

                            break;

                        }

                    }

                    lines = lines.replace(/\[|\]/g, " ");    //Enter[your] text [here!] -> Enter your text here!

                   

                    lineObjs.push({

                        "text": lines,

                        "indexStarts": indexStarts,

                        "indexEnds": indexEnds

                    });

                }

                createText(lineObjs);

            }

            function createText(lineObjs) {

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

                    var thisLine = lineObjs;

                    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);

                        txtProp.property(1).property(propNumber).property("ADBE Text Index End").setValue(thisLine.indexEnds);

                        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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 21, 2018 Jun 21, 2018

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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 21, 2018 Jun 21, 2018
LATEST

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

please use below...

                    while (true) { 

                        var indexStart = lines.indexOf("[", curIndex); 

                        curIndex = indexStart + 1; 

 

                        var indexEnd = lines.indexOf("]", curIndex); 

                        curIndex = indexEnd + 1; 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines