Search code examples
javascriptadobe-indesign

InDesign javascript: How to rotate an image within a text frame?


This is my first question here and I hope my problem is well explained below. Being new to javascript I'm writing a script to place (hundreds of) images in my InDesign document.

Now I'm stuck because of the following problem. After placing the images into a text frame I'd like to rotate some of them by 180 and others only by 90 degrees (depending on an external variable). While the 180 degrees turn works well via flipItem(), the 90 degrees rotation via transform() does not.

Applied on the text frame, transform works well but not when applied on the image object (Error: myFig.transform is no function). I have been using snippets from this very nice answer on the topic but it seems like I missed something crucial.

Can someone point out why transform() is not working in the code below and what might solve the problem?

var myDocument = app.documents.add ();

// create a random text frame
var myTF = myDocument.textFrames.add ();
myTF.geometricBounds = [20, 100, 100, 20];

const cs = CoordinateSpaces.pasteboardCoordinates;
const ap = AnchorPoint.centerAnchor;    
var mx90 = app.transformationMatrices.add ({counterclockwiseRotationAngle:90});

// place an image into the text frame 
var myPath = ""; // enter absolute path to an image (mine are JPEG) 
var myFig = myTF.place (File (myPath), false);
myFig.locked = false;

// works fine
myTF.flipItem (Flip.BOTH,  AnchorPoint.CENTER_ANCHOR);

// works fine
myTF.transform (cs, ap, mx90); 

// error
myFig.transform (cs, ap, mx90);         

Solution

  • According to http://jongware.mit.edu/idcs6js/pc_PageItem.html#place the return value of PageItem.place (fileName: File[, showingOptions: bool=false][, withProperties: Object])
    is an Array

    So if you rather try

    var myFig = myTF.place (File (myPath), false)[0];
    

    the last instruction

    myFig.transform (cs, ap, mx90);
    

    will work. But you will have to disable the line

    //myFig.locked = false;
    

    otherwise it will throw an error since the locked property of an Image within a frame appears to be “not applicable”.