Skip to main content
levi23
Known Participant
November 5, 2016
Answered

draw rectangle arround a textRange or textSelection?

  • November 5, 2016
  • 3 replies
  • 1875 views

Hello such.

My question is simple. it is possible to get bounds of a textRange or TextSelection?

Draw the rectangle already know how to do it, but do not know how I can get the coordinates to draw it. I think it's not possible.

I'm wrong?

I would greatly appreciate your help.

a greeting

This topic has been closed for replies.
Correct answer o-marat

The only thing that came to my mind:

  • find the numbers of the needed characters
  • copy the text frame
  • make it curves
  • find the desired paths by numbers
  • edit (e.g., add a frame around)
  • remove the copy

3 replies

Inspiring
November 21, 2016

Hello, also another way(working with multiline & hyphenation). Code based on Carlos

function main() {

    var idoc = app.activeDocument;

    var sel = idoc.selection;

    if (sel.typename == "TextRange") {

        var iframe = sel.parent.textFrames[0];

        var dup = iframe.duplicate();

        dup.selected = false;

        var range = sel;

       

        // log color

        var col = range.characterAttributes.fillColor;

        //set fill color to blank

        range.story.textRange.characterAttributes.fillColor = new NoColor();

        //set fill color again

        range.characterAttributes.fillColor = col;

        //apply outline effect

        var effectStr = '<LiveEffect name="Adobe Outline Type">' + '<Dict data=""/>' + '</LiveEffect>';

        iframe.applyEffect(effectStr);

        iframe.selected = true;

        //expand appearance

        app.executeMenuCommand('expandStyle');

        var igroup = app.selection[0];

        var margins = 1.5;

        var ipath = idoc.pathItems.rectangle(igroup.top + margins, igroup.left - margins, igroup.width + margins * 2, igroup.height + margins * 2);

        ipath.filled = false;

        ipath.stroked = true;

        ipath.move(dup, ElementPlacement.PLACEBEFORE);

        // remove duplicates 

        igroup.remove();

    } else {

        alert('Select a Text Range and try again');

    }

}

main();

o-marat
Inspiring
November 8, 2016

As another way - the code is written according to the algorithm, which I quoted earlier:

/**

* ai.jsx (c)MaratShagiev m_js@bk.ru 08.11.2016

* makes frame around TextRange.textSelection

* */

//@target illustrator

(function addRectByTxtSel () {

  var d        = activeDocument,

      fr       = d.textFrames[0],

      re       = /\s/gmi,

      marg     = 2,

      col      = new CMYKColor (),

      selStart = fr.textSelection[0].characterOffset - 1, // first selected char

      selLen   = fr.textSelection[0].length, // text selection lenght

      selEnd   = selLen + selStart - 1, // last selected char

      fixSelLen, selSpaces,

      past, fixPastLen, pastSpaces,

      frCurv, gr, rect,

      selItems = [];

  col.magenta = 100;

  col.yellow  = 100;

  fixSelLen = selLen;

  selSpaces = fr.textSelection[0].contents.match (re);

  if (selSpaces) {

    fixSelLen = selLen - selSpaces.length;

  }

  past        = fr.textRange.characters[selEnd + 1];

  past.length = fr.contents.length - selEnd - 1;

  fixPastLen = past.length;

  pastSpaces = past.contents.match (re);

  if (pastSpaces) {

    fixPastLen = past.length - pastSpaces.length;

  }

  frCurv          = (fr.duplicate ()).createOutline ();

  for (var i = fixPastLen; i < fixPastLen + fixSelLen; i++) {

    var obj1 = frCurv.pageItems;

    selItems.push (obj1);

  }

  gr = d.activeLayer.groupItems.add ();

  for (var j = 0; j < selItems.length; j++) {

    var obj2 = selItems;

    obj2.move (gr, ElementPlacement.INSIDE);

  }

  rect = d.activeLayer.pathItems.rectangle (

    gr.position[1] + marg, gr.position[0] - marg, gr.width + marg * 2, gr.height + marg * 2);

  rect.filled      = false;

  rect.strokeColor = col;

  gr.remove ();

  frCurv.remove ();

} ());

If hyphenation  in Illustrator palette is enabled, it is not working correctly (cause the hyphens are not taken in the calculation). But I think it can be fix.

o-marat
Inspiring
November 5, 2016

Hello, levi23​!

You want to get this bounds?

Most likely you're right – through script no possible...

levi23
levi23Author
Known Participant
November 5, 2016

feared. And a part of the text (not the entire text) even if not selected?

Thank you.

a greeting

o-marat
o-maratCorrect answer
Inspiring
November 5, 2016

The only thing that came to my mind:

  • find the numbers of the needed characters
  • copy the text frame
  • make it curves
  • find the desired paths by numbers
  • edit (e.g., add a frame around)
  • remove the copy