Search code examples
javascriptadobe-indesignextendscript

indesign script to add a polygon (irregular shape textframe) and add text to it


I need an Indesign script to add a irregular textframe (polygon) to page and add text content to it. I have so far this code. but it is a regular text frame.

       var doc = app.documents.add();

       var textFrame = doc.textFrames.add();

       textFrame.properties =

       {

        geometricBounds : [ 0,0,100,100 ],

        strokeWidth : 0,

        fillColor : "None",

        contents : "Hello World!"
       }

I can also add this polygon but I can't add text to it.

       var doc = app.documents.add();

       var polygon = doc.polygons.add();
       var points = [
       [100,100],
       [150,100],
       [200,200],
       [120,150],
       [100,100],

]
       
polygon.paths.item(0) = points;
        

thanks for your help!


Solution

  • Not sure if this is an optimal way for your workflow but it can be done this clumsy way:

    var doc = app.documents.add();
    
    var polygon = doc.polygons.add();
    var points = [
        [100,100],
        [150,100],
        [200,200],
        [120,150],
        // [100,100], // this point doesn't need, since a polygon is a closed path
    ];
    polygon.paths[0].entirePath = points;
    
    polygon.contentType = ContentType.TEXT_TYPE;    // convert the polygon into a text frame
    var poly_textFrame = doc.textFrames.lastItem(); // get the last text frame
    poly_textFrame.contents = 'Hello World!';       // change contents of the frame
    

    The problem of this approach is that I'm not sure if the new text frame is always a last item within of the text frames collection. It needs to be tested in vivo.