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

script for creating a text outline and hence find the geometric bound of text

New Here ,
May 30, 2020 May 30, 2020

Copy link to clipboard

Copied

i want a help in js codde in extendscript to crate a text outline and hence find the geometric bound of text within a text frame

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
Guru ,
May 30, 2020 May 30, 2020

Copy link to clipboard

Copied

Here's an example: a snippet from this script:

sourceText = source.sourceText;  
page = sourceText.parentTextFrames[0].parentPage;  
arr = sourceText.createOutlines(false);  
outlinedText = arr[0];  
gb = outlinedText.geometricBounds;  
outlinedText.remove();              

The text is temporarily converted to outlines to get geometric bounds to build a rectangle of the same size and location as the text.

Hope it helps!

— Kas

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
New Here ,
May 30, 2020 May 30, 2020

Copy link to clipboard

Copied

thanks a lot for your answer.but how do i give source .i want to create outline of a text in textframe so how should i initialise my source

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 ,
May 30, 2020 May 30, 2020

Copy link to clipboard

Copied

Also, if there were some reason you don’t want to convert to outlines, you can use the baseline+ascent or descent to get a word’s Y1 and Y2 coordinate, and the horizontal offset and end horizontal offset to get its X1 & X2 coordinates.

 

This would get the bounds of the selection if it is a word or single line. For multiple lines you could test for the longest line and get the Y2 from the end of that line, and the X2 from the lastline baseline + descent.

 

 

 

var sel = app.selection[0];
$.writeln("Selected Text Bounds: " + getTextBounds(sel));


function getTextBounds(t){
    var y1, x1, y2, x2
    y1 = t.baseline - sel.ascent;
    x1 = t.horizontalOffset
    y2 = t.baseline + sel.descent;
    x2 = t.endHorizontalOffset
    return [y1,x1,y2,x2]
}

Also, 

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
New Here ,
May 30, 2020 May 30, 2020

Copy link to clipboard

Copied

 

var docRef = app.documents.add();
   with (docRef.documentPreferences) {
      pageHeight = "3 in";
      pageWidth = "2 in";
      pagesPerDocument = 1;
      facingPages=false;
   }
   var pageRef = docRef.pages.item(0);   
      var frameRef = pageRef.textFrames.add();
      frameRef.geometricBounds = myGetBounds(pageRef,docRef);
      frameRef.contents = "hey";
      alert("hello");
      frameRef.contents="vaishuu";
      var sel = app.selection[0];
$.writeln("Selected Text Bounds: " + getTextBounds(sel));

      function getTextBounds(t){
        var y1, x1, y2, x2
        y1 = t.baseline - sel.ascent;
        x1 = t.horizontalOffset
        y2 = t.baseline + sel.descent;
        x2 = t.endHorizontalOffset
        return [y1,x1,y2,x2]
    }
this is giving me error in line y1 = t.baseline - sel.ascent;
Runtime Error:Object does not support the property or method 'baseline'

 

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
New Here ,
May 30, 2020 May 30, 2020

Copy link to clipboard

Copied

thanks a lot for helping but have one more doubt as i have posted below

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 ,
May 31, 2020 May 31, 2020

Copy link to clipboard

Copied

LATEST

The code you posted doesn’t include a selection, so my sel variable is not defined. Also there’s no myGetBounds() function.

 

Try this:

var docRef = app.documents.add();
   with (docRef.documentPreferences) {
      pageHeight = "6 in";
      pageWidth = "6 in";
      pagesPerDocument = 1;
      facingPages=false;
   }
   var pageRef = docRef.pages.item(0);   
      var frameRef = pageRef.textFrames.add();
      frameRef.geometricBounds = [1,1,4,4];
      frameRef.contents = "hey";
      //alert("hello");
      frameRef.contents="vaishuu";
      frameRef.parentStory.texts[0].select();
      var sel = app.selection[0];
       $.writeln("Selected Text Bounds: " + getTextBounds(sel));

      function getTextBounds(t){
        var y1, x1, y2, x2
        y1 = t.baseline - sel.ascent;
        x1 = t.horizontalOffset
        y2 = t.baseline + sel.descent;
        x2 = t.endHorizontalOffset
        return [y1,x1,y2,x2]
    }

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 ,
May 30, 2020 May 30, 2020

Copy link to clipboard

Copied

Using the ascent and descent gets the entire font bounds, so outlining would be better if you needed the visible bounds. My script does this when the selection isn’t mixed case:

 

Screen Shot 10.png

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