0

In my indesign document i have around 250 pages, each of which contain 1 table (1 per page). What i am trying to do is:

  1. Get the value of the top row of each table
  2. Retrieve the first letter of the cell contents
  3. create a textframe on each page to output that letter

Here is my code so far:

var doc = app.activeDocument;  

var myTable = app.activeDocument.stories.everyItem().tables.everyItem().getElements();  
alert(myTable.length)//no. of tables in a document  

for(var i=0; i<myTable.length; i++)  {  
var textFrame = doc.pages[0].textFrames.add();  
var string = myTable[i].contents;
textFrame.properties =  
{  

    geometricBounds : [ 0,0,100,100 ],  
    strokeWidth : 0,  
    fillColor : "None",  
    contents : string

};  
}  

As this is a glossary, the letters will be A,B,C,D..

For example, table 1 the first row contains the string "Another", so on this page a textframe on the top left corner will say the letter A

UPDATE: Get the first letter from each table

var myTable = app.activeDocument.stories.everyItem().tables.everyItem().getElements();  
alert(myTable.length)//no. of tables in a document  

for(var i=0; i<myTable.length; i++)  
{  
        var string = myTable[i].contents[0][0];
        alert(string);
}  
3
  • How does your code-so-far not work the way you envision?
    – Jongware
    Apr 17, 2018 at 14:07
  • Without the textframe properties, i managed to get the value from each table, but im stuck at getting the first letter, and then creating the text frame
    – lanes123
    Apr 17, 2018 at 14:11
  • @usr2564301 : in the update in the question, i have managed to get the first letter of each one, any idea on creating a new textframe on each page with that letter? in the top left corner
    – lanes123
    Apr 17, 2018 at 14:43

1 Answer 1

1

Almost had it there!

I used your code as the basis, and this is what I came up with:

if (parseFloat(app.version) < 6)
    getTableInitials();
else
    app.doScript(getTableInitials, ScriptLanguage.javascript, undefined, UndoModes.fastEntireScript, "Table Initials to Text Frames");


function getTableInitials() {

    var myTable = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
    var myTableCount = myTable.length;

    for (var i = 0; i < myTableCount; i++) {
        var initialLetter = myTable[i].contents[0][0];
        var pageName = myTable[i].parent.parentPage.name;
        var textFrame = app.activeDocument.pages.itemByName(pageName).textFrames.add();
        textFrame.properties = {
            geometricBounds: [0,0,100,100],
            strokeWeight: 0,
            fillColor: "None",
            contents: initialLetter
        };
    }

    alert("Finished! Added " + myTableCount + " text frames.");

}

This script will create a text frame with initial letter for every table on a single page. It shouldn't matter how your pages are named (i.e. Arabic or Roman numerals, letters, etc), it should catch them all. I also wrapped the script into a function so that fastEntireScript can be called on it. It should at least give you the courtesy of being just one step away from undoing the script, rather than number-of-tables × 2.

Since you've stated that your document contains one table per page, there should be no problem. However, if you have more than one table, you will end up with multiple text frames overlayed one on top of another, each containing the initial letter from the respective table.

Be sure to change text frame properties to your liking. I've included parameters from your code as default values (also corrected strokeWidth to strokeWeight).

I hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.