Skip navigation
Currently Being Moderated

adding a placed item to a button state

Apr 8, 2012 1:24 PM

Tags: #indesign_cs5 #scripting

Hello everyone--

 

First off, I'm using InDesign CS5 on a MacBook Pro.

 

I'm having trouble with targeting a placed item that I would like to add to a button.  It was my understanding that, when you place a file, it could be referenced as master.pageItems[0]. This is the snippet I tried.

 

 

var doc = app.activeDocument;
var master = doc.masterSpreads.itemByName("A-Body_Page");
var lay = doc.layers.item("navigation");
var imgFile = File( "~/desktop/eGuide/NavBtnNormal.png");
master.place(imgFile,["131.25px", "637px"], lay);
var img = master.pageItems[0];
 
var btnHide = master.buttons.add();
btnHide.geometricBounds = ["637px","131.25px", "670px","164.25px"];
btnHide.states[0].addItemsToState(img);

 

On some documents, this will work. On others, it seems to randomly add a different page item to the Button State. Since I added a button to the page, I thought the issue was that master.pageItems[0], changed. Consequently, I tried to approach it differently, before adding the button I tried targeting the id of img or adding a name to the img variable. I tried to access the graphic that way. I also tried to store the "master.place" statement within the img variable itself. As I said, sometimes it works; sometimes it doesn't.

 

I guess I really just want to know—when you place an item, how do you target that item later in the script? I'm pretty new to JavaScript but I really enjoy learning it. As such, the more descriptive you could be, the better. 

 

Thank you in advance for all your help.  

 
Replies
  • Currently Being Moderated
    Apr 8, 2012 2:47 PM   in reply to cfranklin8

    The problem is that "pageItems[0]" does point to your just-placed item, but only if it's the single one item on that page! Otherwise, it does what it usually (actually, always) does, that is, it points to the very first item on your page. Note that this does not contradict my first line :-)

     

    If you place an item with a script and immediately want to do something with it, you need to save the return value of the "place" command. That is an array of "pageItem", and usually you only place one item, so it would be

     

    placedContentArray = master.place(imgFile,["131.25px", "637px"], lay);

    var img = placedContentArray[0];

     

    or, simply (safe because you always place one image at a time)

     

    var img = master.place(imgFile,["131.25px", "637px"], lay)[0];

     

    (Note the [0] at the very end.)

     

    >It was my understanding that, when you place a file, it could be referenced as master.pageItems[0].

     

    I guess you picked up your initial example from a sample script that didn't expect any other items to be on that page.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2012 3:29 AM   in reply to cfranklin8

    ... I missed something in my instructions! Congrats on finding that out, and on finding a solution to it as well. This touches upon one of the many subtleties of scripting InDesign, so you deserve a pat on the back for that.

    Allow me to clarify what goes on under the hood; you can decide if your code does what it needs to, or you may change it according to this little expansion.

     

    Yes, the command "img = master.place(..)[0]" *does* return the image, but you are correct -- this returns the *image* component of the required "image is always inside a frame" that gets placed.

    Your solution works, but alternatively you could use this:

     

    img = master.place(..)[0];

    imgFrame = img.parent;

    // now imgFrame will point to its frame instead!

     

    The reason this (finally) works is because you cannot place an image without it *automatically* having a frame as its parent as well. Your solution works because you are placing inside an existing frame, which is also a good solution -- except you (probably) don't know what size the placed image is. Placing the image itself and then using its own default frame will ensure the size matches.

     
    |
    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