Skip to main content
Participant
December 27, 2024
Answered

Create shape-bounded text in script

  • December 27, 2024
  • 8 replies
  • 604 views

Hi! I was trying to replicate in a jsx script a specific object. It's the kind of shape-bounded text that you can do by creating a shape (e.g. cirle) and using the text tool to put text inside. I'm trying to replicate that object in code, but I'm unable to find the dependencies between TextFrame and Oval objects it involves. Any idea?

Thank you!

This topic has been closed for replies.
Correct answer Robert at ID-Tasker

After you create Oval - change its contentType:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Oval.html

 

8 replies

Participant
December 28, 2024

Hello,

 

o replicate shape-bounded text in a JSX (JavaScript for Adobe Illustrator) script, you need to work with both TextFrame and shape objects, such as Ellipse (for circles). In Illustrator scripting, these objects interact as follows:

Create the Shape: First, you need to create the shape, such as a circle or any other closed path (for example, using the PathItems.ellipse() method to create an ellipse).
Create Text and Fit It to the Shape: After creating the shape, you need to place text inside it. The TextFrame object can be used to create text. You can then fit the text to the boundaries of the shape (ellipse in this case).

example of how you can create shape-bounded text inside an ellipse in JSX:

// Create a new document
var doc = app.documents.add();

// Define ellipse dimensions (position, width, height)
var x = 200;
var y = 200;
var width = 400;
var height = 200;

// Create an ellipse shape (path item)
var ellipse = doc.pathItems.ellipse(y + height / 2, x - width / 2, width, height);

// Create a text frame inside the ellipse
var textFrame = doc.textFrames.add();
textFrame.contents = "Hello, Illustrator!";

// Set the text frame's position
textFrame.position = [x - width / 2, y + height / 2];

// Fit text inside the shape (ellipse)
textFrame.textPath = ellipse; // Make text follow the path of the ellipse

// Optionally adjust text properties (font, size, etc.)
var textRange = textFrame.textRange;
textRange.characterAttributes.size = 24;
textRange.characterAttributes.textFont = app.textFonts.getByName("Arial");

// Adjust the text to be vertically aligned in the center of the shape
textFrame.textPathJustification = Justification.CENTER;

Ellipse Creation: The ellipse is created using the doc.pathItems.ellipse() method. This takes the y and x positions, followed by the width and height.
TextFrame Creation: The textFrame is added to the document and its contents is set to the text you want to display.
Positioning: The text frame is positioned at the center of the ellipse.
Text Fit to Path: The key part here is textFrame.textPath = ellipse;, which makes the text follow the path of the ellipse (or any shape you create).
Text Adjustments: You can adjust the font, size, and other properties of the text as needed.

The textPath property links the text frame to the path of the shape (ellipse), making the text conform to its outline. dgme paystub
Depending on the text length, you might need to adjust font size or the shape's size to make the text fit properly within the shape.
The textFrame.textPathJustification property is used to align the text inside the path (center, left, or right).

Robert at ID-Tasker
Legend
December 28, 2024

@nima_3446

 

This is InDesign forum. 

 

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
December 28, 2024

After you create Oval - change its contentType:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Oval.html

 

Participant
December 28, 2024

This is the correct answer. Thank you! I'll provide a very short snippet for those interested:

    var doc = app.activeDocument;
    var new_oval = doc.ovals.add()
    new_oval.geometricBounds = [0, 0, 50, 50];
    new_oval.contentType = ContentType.TEXT_TYPE;