Skip navigation
Currently Being Moderated

[JS-CS5.5] How to place an image in a rectangle by name ?

Jul 11, 2012 7:49 AM



I try to place an image in a rectangle by name ...


var myDoc =  app.activeDocument;

var myGraphicFile = File("/data/image.jpg");


// this code work



var img = myDoc.rectangles.item(0).place(new File(myGraphicFile));


//this code doesn't work



var img = myDoc.rectangles.item("Rec1").place(new File(myGraphicFile));


Have you an idea ?


  • Currently Being Moderated
    Jul 11, 2012 8:41 AM   in reply to rhanot63


    Is "Rec1" a name which you set or InDesign set it when created?

    If InDesign set it - your rectangle name is 'undefined', even you can see <Rec1> written in layer panel.

    Name your rectangle by yourself to refer to it by its name.



    Mark as:
  • Currently Being Moderated
    Jul 11, 2012 2:56 PM   in reply to rhanot63

    How did you name it?


    Rec1 = myDoc.rectangles.add()

    In which case you need to use


    without the "quotes"


    If you used "name"


    Then your code works


    If you used "label"


    then that's not good

    If you want to refer to it, AFAIK you have to loop through all the rectangles and see if there label is =="Rec1"


    Let us know if this solves your problem



    Mark as:
  • Currently Being Moderated
    Jul 12, 2012 1:43 AM   in reply to rhanot63

    Here is the function I use in CS5+:

    var myRec = GetItemFromCollection("Rec1", myDoc.rectangles);
    function GetItemFromCollection(label, collection) {
        for (var i = 0; i < collection.length; i++) {
            if (label == collection[i].label) return collection[i];
        return null;
    Mark as:
  • Currently Being Moderated
    Jul 12, 2012 4:19 AM   in reply to rhanot63

    Hi Ronald


    I'm pretty sure it's because labels as apposed to names are not intrinsically unique, so they can't be considered an identifier (which have to be unique).


    Haven't put that so well but you should be able to understand.



    Don't forget to mark your question as answered





    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points