Skip to main content
Participant
August 28, 2017
Answered

Correct use of dataMergeImagePlaceholders.add()

  • August 28, 2017
  • 4 replies
  • 960 views

Hi All

I'm very new to InDesign and ExtendScript and also this forum so apologies in advance for any indiscretions.

I'm having trouble with one particular aspect of a script I've written to duplicate pages and add Data Merge text and image placeholders to page items in the new pages.

I have a template page with 6 rollover buttons each to display one of 6 photos.


The image frames are in buttons so I can use the 'show/hide buttons' action on rollover.
This works well.

The frames in the photo buttons are empty in the template and the buttons are named such that I can uniquely identify each button on each page.
There are also 3 empty uniquely named text frames on each page.

The code duplicates the template page a number of times according to the user's requirements.

After the pages are duplicated I go back to each page to find the empty text frames adding a data merge placeholder to each using dataMergeTextPlaceholders.add()
This works perfectly; so I know I'm iterating the pages correctly and applying the correct methodology in programmatically adding DataMergeFields (at least for text fields).

However when I try to do the same for the photos with image placeholders using dataMergeImagePlaceholders.add() it crashes InDesign (literally).

I suspect that the problem might be the image frame being part of a button, not a simple page item like the text frames.
So when I search for and find the named page item I'm getting the button not the image frame.

Here is the code I'm currently using (and failing) to find the button and add an image placeholder:

// image place holders
var imageFieldPrefix = ["Photo1 Ext", "Photo2 Ext", "Photo3 Ext", "Photo4 Ext", "Photo5 Ext", "Photo6 Ext"];

// suffix of this sheet
thisSuffix = ("0" + (cnt1+1)).slice (-2)


for (cnt2 = 0; cnt2 < imageFieldPrefix.length; cnt2++)

{

// construct the field name
fieldName = imageFieldPrefix[cnt2] + thisSuffix;

// find the graphic frame
newImageItem = currentPage.pageItems.itemByName(fieldName);  // this returns button item

// add the data merge placeholder.
myDataMergeField = get_field(fieldName, myDocument);   // this returns DataMergeField


var myFieldHolder = myDocument.dataMergeImagePlaceholders.add(newImageItem, myDataMergeField); // this brings down the lot
}

So my question is: Can anyone tell me what is the right argument to pass to dataMergeImagePlaceholders.add() and how to get it from my button?
(Or am I on the wrong track?)


Thanks in advance.

Michael

Adobe InDesign CC
MS-Windows (XP would you believe?)

This topic has been closed for replies.
Correct answer Loic.Aigon

The crash isn't really surprising because as you presumed you can't attach datamerge tags to items other than regular frames.

Anyway, if you go down in the button pageItems hierarchy to the frame you are interested in, there is as it seems to be on ID CC 2017/Mac OS no issue reagrding to adding datamerge tags to a frame belonging to a button.

In this snippet, I have a single button in a dummy document. My button is made of one frame, and my image tag is the second one in the dataMergeFields array:

var doc = app.activeDocument;

var rect = app.selection[0].pageItems[0].pageItems[0];

var field = doc.dataMergeProperties.dataMergeFields[1];

doc.dataMergeImagePlaceholders.add(rect, field);

Just works.

FWIW

Loic

Ozalto | Productivity Oriented - Loïc Aigon

4 replies

Loic.Aigon
Legend
August 28, 2017

Laubender

FWIW: InDesign shouldn't crash if you are doing something wrong with addressing the right object.

It just should throw an error.

Most of the times indeed. But i think here this is a space/time continuum being broken. InDesign datamerge was there since CS1 maybe and interactive came with CS5 (4?) So I think engineers could never imagine one would tag something else than a frame. It's probably handled in the UI but not monitored at the scripting level (unless i am wrong).
Once that said, I can count a few similar issues leading to crashes, or should I say "Illegal Operations"

Community Expert
August 28, 2017

Hi Michael,

FWIW: InDesign shouldn't crash if you are doing something wrong with addressing the right object.
It just should throw an error.

Now your problem is perhaps to determine the right state of the button and the right pageItem in that state where you can add the data merge image placeholder.

Can you tell us more about your buttons?

Maybe you could look into:

Adobe InDesign CS6 (8.0) Object Model JS: State

Adobe InDesign CS6 (8.0) Object Model JS: Button

Select a button and do:

app.select( app.selection[0].states[0].pageItems[0] );

Is the page item selected you want to deal with?

Or is it this one:

app.select( app.selection[0].states[0].pageItems[0].pageItems[0] );

Check the selected item in the Layers Panel.

Regards,
Uwe

PS: Too late :-) You already found the right one…

Loic.Aigon
Legend
August 28, 2017

So, in the code I posted all I need to do is change the first arg in dataMergeImagePlaceholder.add() to   newImageItem.pageItems[0] ?

Might not be sufficient.

Remember that the first pageItems instance inside the button is a group (even if you only have one object). So you need to go down once more hence my :

var rect = app.selection[0].pageItems[0].pageItems[0];

Participant
August 28, 2017

Of course... So

app.selection[0]   is your button (the one and only item in your document)

app.selection[0].pageItems[0]   is some sort of group 'header'

app.selection[0].pageItems[0].pageItems[0]   is the frame

Got it.

And thanks again.

Loic.Aigon
Loic.AigonCorrect answer
Legend
August 28, 2017

The crash isn't really surprising because as you presumed you can't attach datamerge tags to items other than regular frames.

Anyway, if you go down in the button pageItems hierarchy to the frame you are interested in, there is as it seems to be on ID CC 2017/Mac OS no issue reagrding to adding datamerge tags to a frame belonging to a button.

In this snippet, I have a single button in a dummy document. My button is made of one frame, and my image tag is the second one in the dataMergeFields array:

var doc = app.activeDocument;

var rect = app.selection[0].pageItems[0].pageItems[0];

var field = doc.dataMergeProperties.dataMergeFields[1];

doc.dataMergeImagePlaceholders.add(rect, field);

Just works.

FWIW

Loic

Ozalto | Productivity Oriented - Loïc Aigon

Participant
August 28, 2017

Thanks so much for the reply.

One line of your snippet was exactly what I was after; that is how to get to the frame in the button.

var rect = app.selection[0].pageItems[0].pageItems[0];

So, in the code I posted all I need to do is change the first arg in dataMergeImagePlaceholder.add() to   newImageItem.pageItems[0] ?

Can't wait to try it.

Regards

Michael

fer-47286597
Participant
February 29, 2020

Hey Michael! I have been looking for a way to make a new page and add datamerge placeholders programatically. Is there any chance you can share the full code? Thank you in advance!