Skip to main content
Participant
February 2, 2014
Question

[AS] How to test the presence of at least one table?

  • February 2, 2014
  • 2 replies
  • 4115 views

Hello everyone,

I would like to test for the presence of at least one table in a document before starting a process (on edge strokes). 

I found this, but I do not know if this is really effective:

                              set CountOfTables1 to count of tables of every story

                              set CountOfTables2 to every table of every story

The first gives me a list of the number of table in each story; the second gives me the objects reference of every table.

Is there another way?

TIA.

Oli.

This topic has been closed for replies.

2 replies

zeRafio
Inspiring
February 8, 2014

Hi all,

Don't have enough time to read the entire discussin,.

But if the goal is still to know if a doc contains at least one table, try this:

tell application id "InDn"

 

          tell active document

                    set a1 to tables of stories

                    set a2 to (count of a1) > 1

          end tell

 

end tell

It's the fastest way I know.

Takes less than 2 seconds in a 274 pages document containing 1918 stories.

But I can be beside the point…

Community Expert
February 8, 2014

@zeRafio – sorry to say, but with some of the solutions presented above we are more in a time frame of 0.1 to 0.01 seconds. Of course depending on the exact amount of stories and "table density" (and the hardware specifications of our equipment).

Uwe

Legend
February 8, 2014

Can anybody with a big document give the following script a try?

I've added an unfair advantage for AppleScript - using the optional whose clause you can narrow down the examined objects by additional criteria. I just chose a table style to illustrate the principle.

Dirk

tell application "Adobe InDesign CS6"

          tell active document

                    set tstyle to table style 1

                    get exists (table of every story whose applied table style is not tstyle)

          end tell

end tell


Community Expert
February 3, 2014

@Olivier – don't know much about AppleScript, but have some experience in ExtendScript.

From that I can say that the Story object is absolutely the right target if you are looking for tables.

Also Story objects inside nested objects are detected: Story objects in groups, MSOs, buttons, hidden objects etc.pp.

And if your goal is just to detect if at least one table is present in the document, I'll go for the second method and read out its length. Very efficient.

Uwe

Participant
February 3, 2014

Thank you.

I see no alternative but to "store" each table.

It's pitty ... I have a document (an annual report) that contains nearly 200 tables. It takes time to store them.

Oli.

Community Expert
February 4, 2014

@Olivier – on the other hand, it could be faster to loop through all storys of that document reading out the length of the tables. And stop, if you hit the first table.

ExtendScript:

//EDIT: A bug creeped in the original version of my ExtendScript.

Here the tested script:

var s=app.documents[0].stories;

var myAlert = 0;

for(var n=0;n<s.length;n++){

   

    var currentStory = s;

   

   

    if(currentStory.tables.length > 0){

        myAlert = 1;

        break;

        };

    };

end_time = Date.now();

elapsed_time = ((end_time - start_time)/1000)/60;

if(myAlert){

    alert(elapsed_time+" minutes"+"\r"+"At least ONE table found.");

    };

else{

    alert(elapsed_time+" minutes"+"\r"+"NO table found.");

    };

In my case with the 1,300 tables in about 1,500 story objects it took that script a split second to detect a table.

But this could vary, depending on your count of story objects. In my case nearly all story objects contain at least one table.

Uwe

Message was edited by: Laubender

Message was edited by: Laubender


@Olivier – or, if you assume, that nearly every story contains a table, you could split the task in two sections:

1. Count on stories.anyItem() and check for tables (let InDesign decide which arbitrary one single story anyItem() will reach)

2. If that will fail, loop through all stories

In comparison with a single loop through all the story objects, this could be even more efficient. Or not, adding a small penalty for taking its time, if the ratio "number of story objects containing a table" vs. "number of story objects" is very low.

Here an example for an ExtendScrpt using anyItem():

start_time = Date.now();

var s=app.documents[0].stories;

var myAlert = 0;

var myResult = detectATable();

function detectATable(){

    //Use anyItem()

    //to single out an arbitrary story in the document:

    if(s.anyItem().tables.length > 0){

        myAlert = 1;

        };

    //If by chance a story with a table is found

    //end this function and return a result:

    if(myAlert){return myAlert};

    //If not (sigh!) do the walk through a loop with all story objects:

    for(var n=0;n<s.length;n++){

        var currentStory = s;

        if(currentStory.tables.length > 0){

            myAlert = 1;

            break;

            };

        };

    return myAlert;

};

end_time = Date.now();

elapsed_time = ((end_time - start_time)/1000)/60;

if(myResult === 1){

    alert(elapsed_time+" minutes"+"\r"+"At least ONE table found.");

    };

else{

    alert(elapsed_time+" minutes"+"\r"+"NO table found.");

    };

Uwe