Skip navigation
rhanot63
Currently Being Moderated

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

Jul 11, 2012 7:49 AM

Hi,

 

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

 

var myDoc =  app.activeDocument;

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

 

// this code work

myDoc.rectangles.item(0).place(myGraphicFile);

//or

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

 

//this code doesn't work

myDoc.rectangles.item("Rec1").place(myGraphicFile);

//or

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

 

Have you an idea ?

Thanks

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

    Hi,

    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.

     

    rgds

     
    |
    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

    myDoc.rectangles.item(Rec1).place(myGraphicFile);

    without the "quotes"

     

    If you used "name"

    myDoc.rectangles.add({name:"Rec1"})

    Then your code works

     

    If you used "label"

    myDoc.rectangles.add({label:"Rec1"})

    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

     

    Trevor

     
    |
    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

     

    Regards

     

    Trevor

     
    |
    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