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

Need to remove some characters in the text in active document

Explorer ,
Apr 20, 2018 Apr 20, 2018

Hi all,

Here I had made a script to write equations with superscript.

The input maybe

a^{2}+b^{2}=c^{2}

The Required output is

a2+b2=c2

The script I made is.

if(documents.length >0){  //if document is open     

    if(app.activeDocument.selection[0]){  //if a line is selected 

        var supBPosition=[];

        var subBPosition=[];

        var doc = app.activeDocument;  //getting the active document

        var line = doc.pathItems[0];  //getting path items from the active document 

        var firstPoint = line.pathPoints[0].anchor; // [x, y]

        var lastPoint = line.pathPoints[line.pathPoints.length - 1].anchor; // [x, y]       

        var eqValues = (prompt("Enter something ", "", "")); //getting the values to write

        var eqValuesCopy=eqValues;

        var startCharacterSize=0;

        var ys=[];

        var ns=[];

        var nsDummy=[];

        var yFlag=0;

        var nFlag=0;

        var plotGraph=1;

        getPositions();  

        var finalContent=eqValues;

        if(eqValues.indexOf("^")>0){                        

            plotGraph= doc.textFrames.add();  //adding text

            plotGraph.textRange.characterAttributes.size = 8;  //character size for text

            plotGraph.textRange.characterAttributes.textFont = app.textFonts.getByName("Times-Roman"); //for changing the font to times-roman

            plotGraph.textRange.justification = Justification.CENTER;  //for aligning the content to center

            plotGraph.top = firstPoint[1];  //x coordinate for the new value

            plotGraph.left = firstPoint[0];  //y coordinate for the new value           

            plotGraph.contents=(finalContent);

            var t = doc.textFrames[0]; 

            for(var m=0;m<=ns.length;m++){

                for(var plotGraphSup=ys;plotGraphSup<=ns;plotGraphSup++){ 

                    thisChar = t.characters[plotGraphSup];  //writing base value              

                    thisChar.characterAttributes.baselineShift = +3;  //making superscript 

                    thisChar.characterAttributes.size = 6;  //adjusting size of superscript

                }                

            }

        }

    }

    function getPositions(){   

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

            if(eqValuesCopy=="{"){

                if(i!=0){

                    validBCheck=eqValuesCopy[i-1]+eqValuesCopy;

                    if(validBCheck!="/{"){

                        ys.push(i);

                    }

                }

            }

            ysLength=ys.length;

        }

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

            nsLength=ns.length;

            if(nsLength<ysLength){

                if(eqValuesCopy=="{"){

                    var j=i;

                    while(j<eqValuesCopy.length){

                        j++;

                        if(eqValuesCopy=="}"){

                            ns[nsLength]=j;

                            break;

                        }else if(eqValuesCopy=="{"){

                            nsLength++;

                        }else{

                           

                        }

                    } 

                    i=j;

                }

            }

        }

        for(var k=0;k<eqValuesCopy.length;k++){

            if(eqValuesCopy=="}"){

                var h=k;

                while(h<eqValuesCopy.length){

                    h++;

                    if(eqValuesCopy=="{"){

                        break;

                    }else if(eqValuesCopy=="}"){

                        nsDummy.push(h);

                        break;

                    }else{

                       

                    }

                }

            }

        }

        nsDummy.reverse();

        for(var x=0;x<nsLength;x++){

            if(ns===undefined){

                ns=nsDummy;

            }

        }

    }

}

Also I am getting an error in line number "32".

Moved from Illustrator to Illustrator Scripting by moderator

.

TOPICS
Scripting
1.8K
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

Advocate , Apr 25, 2018 Apr 25, 2018

Bonjour,

Je ne vois pas l'utilité des accolades { }

// JavaScript document for Illustrator

// elleere Wed, 25 April 2018 17:39:04 GMT

// INIT---------

var c1 = "^";

var c2 = "_";

var text = "2^2.5 + b^2 = 5 C_12^2 sin(20^3)";  // cas 1

//var text = "2^2.5 + b^2 = 5 C^2_12 sin(20^3)"; // cas 2 sauf si toujours 1 chiffre

var corps = 16;

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

var docRef = activeDocument;

var pointText = docRef.textFrames.add();

    pointText.contents = text;

    pointText.position  = [20,-20];

    pointText.textRange.

...
Translate
Adobe
Community Expert ,
Apr 20, 2018 Apr 20, 2018

for(var plotGraphSup=ys;plotGraphSup<=ns;plotGraphSup++){

should be:

for(var plotGraphSup=ys;plotGraphSup<ns;plotGraphSup++){

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
Advocate ,
Apr 25, 2018 Apr 25, 2018

Bonjour,

Je ne vois pas l'utilité des accolades { }

// JavaScript document for Illustrator

// elleere Wed, 25 April 2018 17:39:04 GMT

// INIT---------

var c1 = "^";

var c2 = "_";

var text = "2^2.5 + b^2 = 5 C_12^2 sin(20^3)";  // cas 1

//var text = "2^2.5 + b^2 = 5 C^2_12 sin(20^3)"; // cas 2 sauf si toujours 1 chiffre

var corps = 16;

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

var docRef = activeDocument;

var pointText = docRef.textFrames.add();

    pointText.contents = text;

    pointText.position  = [20,-20];

    pointText.textRange.characterAttributes.size = corps;

    //expo_indice(pointText,c1,1); // si cas 2

    expo_indice(pointText,c2,0);

    expo_indice(pointText,c1,1); // si cas 1

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

function expo_indice(textRf,rx,drap) {

  var index, pos, i, r, curentChar, suit, baseline;

      baseline = drap ? FontBaselineOption.SUPERSCRIPT : FontBaselineOption.SUBSCRIPT;

      pos = index = textRf.contents.indexOf(rx);

      suit = textRf.contents;

        while (pos != -1) {

          suit = suit.substring(pos+1);

          r = parseFloat(suit)+"";

          //r = drap ? parseFloat(suit)+"" : "1";  // si toujours 1 chiffre

            for (i = 1; i <= r.length; i++) {

              curentChar = textRf.textRanges[index+i];

              curentChar.baselinePosition = baseline;

            }

          textRf.textRanges[index].remove();

          pos = suit.indexOf(rx);

          index += pos;

        }

}

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

de LR

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 Expert ,
Apr 26, 2018 Apr 26, 2018

renél80416020  schrieb

Bonjour,

Je ne vois pas l'utilité des accolades { } …

Me too,

but anyway - try this (this is the same code as in your other thread)

var aSample = "a^{2}+b^{2}=c^{2}_{2}";

var aDoc = app.activeDocument;

var aTF = aDoc.textFrames.add();

aTF.position = [10, -2];

aTF.contents = aSample;

aTF.textRange.size = 8;

aTF.textRange.characterAttributes.textFont = app.textFonts.getByName("TimesNewRomanPSMT"); //depends on your Illu version and installed font

var res, idx;

var reg = /\^\{/g;

while (res = reg.exec(aTF.contents)) {

    idx = res.index;

    aTF.textRange.characters[idx].remove();

    aTF.textRange.characters[idx].remove();

    aTF.textRange.characters[idx].baselinePosition = FontBaselineOption.SUPERSCRIPT;

    aTF.textRange.characters[idx+1].remove();

}

var reg = /_\{/g;

while (res = reg.exec(aTF.contents)) {

    idx = res.index;

    aTF.textRange.characters[idx].remove();

    aTF.textRange.characters[idx].remove();

    aTF.textRange.characters[idx].baselinePosition = FontBaselineOption.SUBSCRIPT;

    aTF.textRange.characters[idx-1].tracking = -480; // TimesNewRomanPSMT

    aTF.textRange.characters[idx+1].remove();

}

Have fun

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 ,
Apr 26, 2018 Apr 26, 2018

Hi pixxxel,

This works. But the problem comes when, the input is

a^{21}+b^{2}=c^{2}_{2}


Something like this. This is why I had used the loop to get the starting and ending position on superscript and subscript values.

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 Expert ,
Apr 26, 2018 Apr 26, 2018

There is one rule for scripting and Grep (Regex):

The better/detailed and more the examples - the better the script/grep

How about the solution by in post #3 ? Should work with more than one sperscript/subscript digits.

Regards

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 Expert ,
Apr 24, 2018 Apr 24, 2018
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 ,
Apr 27, 2018 Apr 27, 2018
LATEST

Hi,
had done just some changes.

// JavaScript document for Illustrator

// elleere Wed, 25 April 2018 17:39:04 GMT

// INIT---------

var c1 = "^";

var c2 = "_";

var text = (prompt("Enter something like 2^2.35 + b^2 + c^2= 5 C_12^2 sin(20^3)", "", "")); //getting the values to write

//var text = "2^2.35 + b^2 + c^2= 5 C_12^2 sin(20^3)";  // cas 1

//var text = "2^2.5 + b^2 = 5 C^2_12 sin(20^3)"; // cas 2 sauf si toujours 1 chiffre

var corps = 8;

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

var docRef = app.activeDocument;  

var line = docRef.pathItems[0];

var firstPoint = line.pathPoints[0].anchor; // [x, y]

var pointText = docRef.textFrames.add();

    pointText.contents = text;

    pointText.position  = [firstPoint[0],firstPoint[1]];

    pointText.textRange.characterAttributes.size = corps;

    pointText.textRange.characterAttributes.textFont = app.textFonts.getByName("RVTimes-Italic");

    expo_indice(pointText,c2,0);

    expo_indice(pointText,c1,1); // si cas 1    

    app.cut();

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

function expo_indice(textRf,rx,drap) {

  var index, pos, i, r, curentChar, suit, baseline;

      baseline = drap ? 1 : 2;

    // baseline = drap ? FontBaselineOption.SUPERSCRIPT : FontBaselineOption.SUBSCRIPT;

      pos = index = textRf.contents.indexOf(rx);

      suit = textRf.contents;

        while (pos != -1) {

          suit = suit.substring(pos+1);

          r = parseFloat(suit)+"";

            for (i = 1; i <= r.length; i++) {

              curentChar = textRf.textRanges[index+i];

              if(baseline==1){

                  curentChar.characterAttributes.baselineShift = +3;

                  curentChar.characterAttributes.size = 5.33;

              }else{

                  curentChar.characterAttributes.baselineShift = -1.2;

                  curentChar.characterAttributes.size = 5.33;                

              }

              //curentChar.baselinePosition = baseline;

            }

          textRf.textRanges[index].remove();

          pos = suit.indexOf(rx);

          index += pos;

        }

}


Thank-you

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