• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

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.

 

1.png2.png4.png3.png 

TOPICS
Scripting

Views

305

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 3 Correct answers

Advisor , Nov 08, 2022 Nov 08, 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

Votes

Translate

Translate
People's Champ , Nov 08, 2022 Nov 08, 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.allGraphic
...

Votes

Translate

Translate
People's Champ , Nov 09, 2022 Nov 09, 2022

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 sho

...

Votes

Translate

Translate
People's Champ ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

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?

  

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

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-th...

 

Regards,

Mike

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

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.

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 09, 2022 Nov 09, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 09, 2022 Nov 09, 2022

Copy link to clipboard

Copied

LATEST

Thank you, Loic! You have been super helpful (as always)!

I'll certainly check out the links that you provided. I didn't know that Peter Karhel wrote that ScriptUI guide! Good to know.

Take care and I appreciate your help!

Jim

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

@mikesorry our posts crossed each other. If I saw your first, I wouldn't have posted an answer 😉

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines