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

Writing equations in illustrator using javascript.

Explorer ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

Hi,
I am trying to write equations using javascript in illustrator.

Equations might be having superscript and subscript. When I run the script, I need a prompt where, I will write some inputs like

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

And after hitting enter, I need the equation to be like

a2+b2=c22

I had tried something, but its not working properly.

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

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

        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 graphValues = (prompt("Enter something", "", "")); //getting the values to write

        var newPowerValue="";

        var ii=1;

        var items=[];

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

            PoweredValues=graphValues.split("^");

            BaseValue=PoweredValues[0]; //getting base value from the array

            PowerValue=PoweredValues[1]; //getting power value from the array  

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

                if(i>0 && i+1!=PowerValue.length){

                    newPowerValue=newPowerValue+PowerValue;

                }

            }            

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

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

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

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

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

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

            finalContent = BaseValue+""+(Number(newPowerValue));         

            finalContent=finalContent.toString();  

            var finalContentcheck=Number(finalContent.indexOf("-"));

            if(finalContentcheck>=0){

                finalContent=finalContent.replace(/-/g,"–"); 

            }

            ii.contents=(finalContent);

            var t = doc.textFrames[0];  // adding text

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

            supStop=newPowerValue.length+BaseValue.length;

            for(var j=BaseValue.length;j<=supStop-1;j++){

                thisChar = t.characters;  //writing base value           

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

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

            }

       

       

       

       

        }

    }

}

I am new in this scripting. Thanks in advance for the help.

TOPICS
Scripting

Views

1.2K

Translate

Translate

Report

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

Community Expert , Apr 26, 2018 Apr 26, 2018

Try this

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

...

Votes

Translate

Translate
Adobe
LEGEND ,
Apr 19, 2018 Apr 19, 2018

Copy link to clipboard

Copied

Your code doesn't make any sense. You are not manipulating arrays, you are merely incrementing values. Perhaps you should start by reading up on how arrays in JavaScript actually work and which methods can be used to manipulate them?

Mylenium

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Here I just made the equation

a2

from a^{2}

I couldn't get any idea to make the equation like I said in the question.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Use the property of characterAttributes

FontBaselineOption.SUBSCRIPT and

FontBaselineOption.SUPERSCRIPT

Have fun

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

What happens?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

Try this

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

Votes

Translate

Translate

Report

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