Skip to main content
Inspiring
November 8, 2022
Answered

Script to find image box within a table (data merged)

  • November 8, 2022
  • 2 replies
  • 883 views

Hi All,

 

I have a multi-page document that has been in-line data merged. Each frame contains a table with 2 rows -- an image on top and text on the bottom (Image 1). I wrote a script that will find/select the text and apply an object style to that box. However, I can't seem to figure out how to script finding and selecting the larger image frame on the top to apply an object style to that box (Image 2). The 3rd and 4th image areas won't work with my object style, so those areas can't be selected. Any suggestions? Each page has 5-9 boxes, like the sample below, and I don't want to manually select each image frame to apply the object style.

 

 

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

Hi Loic,

Thanks again for your help! It is really appreciated!

I could only get the script to work if I commented out this line:

//if(cl.constructor.name=="Cell"){
Also, is there a way to prompt the user after the script finds each image to use 1 of 3 different object styles? I know this is beyond the scope of my original request. As a beginner, I'm just wondering if it's possible.
Attached is my sample InDesign file for reference.

Hello,

I modified the script. There was obviously a flaw with graphic cells and just in case, if tables are 1 cell only. The file you  provided helped fixing the issue. This happens when you script blindly 😉

 

Apart from that, you can certainly have a prompt but think wisely, you may have many graphics and the user could be annoyed by repeating requests. At this point, you can either go with the dialogs object:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Dialogs.html#d1e590161

You should get the InDesign Scripting guide which introduces dialogs in a more friendly way.

Or use ScriptUI, see Peter Karhel reference book :

https://creativepro.com/files/kahrel/indesign/scriptui.html

HTH

Loic

2 replies

Legend
November 8, 2022

Hello @jimDcleveland,

 

Take a look at the link below...

"Script to apply an Object Style to the frames those has same specific link image"

https://community.adobe.com/t5/indesign-discussions/script-to-apply-an-object-style-to-the-frames-those-has-same-specific-link-image/td-p/10028774

 

Regards,

Mike

Loic.Aigon
Legend
November 8, 2022

This should help.

function main(){
    //-----VARS-------//
    var doc = app.documents, gfxs = [], n = 0, p, c, gfx, os, cl;

    //Exit if no doc open
    if(!doc.length) return;
    doc = doc[0];

    //Exit if object style not found
    os = doc.objectStyles.itemByName("shadow");
    if(!os.isValid){
        alert("An object style named \"shadow\" is needed to run teh script.");
        return;
    }

    //Let's loop through the array of graphics objects inside docs
    gfxs = doc.allGraphics;
    n =gfxs.length;
    while(n--){

        //let's climb up to what should be a cell.
        gfx = gfxs[n];
        //The container of the image
        p = gfx.parent;
        // a character containing a anchored fralme, a cell or a table object or something else
        c = p.parent; 
        //a cell or a table object or something else
        cl = c.parent;
        
        //If the graphics hierarchy returns a cell or table object, let's apply object style to the graphic
        if(
            /^(Cell|Table)$/.test(c.constructor.name) 
            || 
            /^(Cell|Table)$/.test(cl.constructor.name)
        ){
            p.applyObjectStyle(os)
        }
    }


}

//run
main();

 

[EDIT] Fix case with graphic cells and possible 1 cell only tables.

 

 

Inspiring
November 8, 2022

Thank you so much, Loic. I'll test the script tonight and let you know!

Loic.Aigon
Legend
November 8, 2022

There would be different ways to tackle this, the smarter one depends on your context.

- Do you want any images to be styled?

  Then you could either loop through the doc.allGraphics array and climb up to the container.

- Only those mounted by the merge?

 

In other words, what are the key characteristics for being sure only the appropriate images are processed?

  

 

 

Inspiring
November 8, 2022

Hi Loic! Thank you for helping! I'm looking for only those mounted by the merge. For example, only the images that appear in that top frame with the text underneath.