Home > InDesign Scripts > Round Selected text with Polygon

Round Selected text with Polygon

Round Selected text with PolygonWell, this one is really awesome! I was playing with finding positions of selected text, baseline and stuff, and I came to great idea! What if I want to draw polygon around selected text? Can it be done through scripting?!😕 Well, I tried, and I think I was successful!😀 It’s little bit complicated, but I learned a lot! So, let’s get started!

First, let’s grab active document, selection, also parent text frame, we will need frame geometric bounds later. Also, I added offset setting for increase and decrease size of final polygon. Let’s go through retrieving position of selected text. There few great text properties that allow us to retrieve bounds of selected text for example ‘horizontalOffset’ (The horizontal offset of the text), ‘endHorizontalOffset’ (Horizontal offset of the end of the text), ‘baseline’ (The vertical offset of the text), ‘endBaseline’ (Vertical offset of the end of the text), ‘ascent’ (The maximum ascent of any character in the text) and ‘descent’ (The maximum descent of any character in the text). So, with that properties retrieved form selected text, we calculate positions and create polygon. Script is not perfect, but it’s working😀 and of course there is lot of space for improvements, so if you like this script, feel free to suggest😉

var doc = app.activeDocument;
var sel = doc.selection[0];
var selOffset = 0.2;
var myFrame = sel.parentTextFrames[0];
var myFrameBounds = myFrame.geometricBounds;

var X_first = sel.horizontalOffset - selOffset;
var X_last = sel.endHorizontalOffset + selOffset;

var Y_top = sel.baseline - sel.ascent - selOffset;
var Y_bottom = sel.endBaseline + sel.descent + selOffset;

var roundSelectionPoints = Array();

  if(sel.lines.length == 1){
      roundSelectionPoints.push([X_first,Y_top]);
      roundSelectionPoints.push([X_last,Y_top]);
      roundSelectionPoints.push([X_last,Y_bottom]);
      roundSelectionPoints.push([X_first,Y_bottom]);
  }else{
      var Y_first = sel.lines[1].baseline - sel.ascent - selOffset;
      var Y_last = sel.lines[sel.lines.length-2].baseline + sel.descent + selOffset;

      roundSelectionPoints.push([X_first,Y_top]);
      roundSelectionPoints.push([myFrameBounds[3],Y_top]);
      roundSelectionPoints.push([myFrameBounds[3],Y_last]);
      roundSelectionPoints.push([X_last,Y_last]);
      roundSelectionPoints.push([X_last,Y_bottom]);
      roundSelectionPoints.push([myFrameBounds[1],Y_bottom]);
      roundSelectionPoints.push([myFrameBounds[1],Y_first]);
      roundSelectionPoints.push([X_first,Y_first]);
  }

var roundSelection = app.activeWindow.activePage.polygons.add();

roundSelection.sendBackward();
roundSelection.fillColor = app.activeDocument.swatches.item("Black");
roundSelection.fillTint = 25;
roundSelection.strokeWeight = 0;
roundSelection.paths.item(0).entirePath = roundSelectionPoints;

Well, that’s it, little messy😀 Maybe, we can put polygon on separate layer, or anchor it to text or whatever we want to achieve!

Have fun!😀

  1. SARAVANAN
    January 20, 2011 at 09:31

    Hai,

    I need a similar script to find the geometricBounds of a In-Line Table

    Actually what i need is, find all Inline Tables in a InDesign Document, Cut & Paste it into a Graphic-Frame at the Same Position.

    any help??

  2. Jay
    January 28, 2011 at 18:10

    You’re the man!

  3. Linus Wang
    July 2, 2011 at 16:49

    Good one, but it does not work on vertical text, if you have a CCJK version of InDesign.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: