Skip to main content
_AWID_
Inspiring
January 13, 2021
Question

Text frames close together: get content by script

  • January 13, 2021
  • 1 reply
  • 704 views

First of all: the hardest part is done. 🙂

Hello!

From a document with a variable number of pages, in which (article-describing) texts with a table belong together, I need the content of both in succession: Text1 + Table1, Text2 + Table2, Text3 + Table3, etc.

 

There is no "physical" connection between the two. They are just close together. Can I do something with it?

What I´ve done, yet is the script grabbing the content of the tables.

 

var myDoc = app.activeDocument;
var myPages = myDoc.pages.everyItem();

var allTextFrames = myPages.textFrames.everyItem().getElements();
var numberOfTextFrames = myPages.textFrames.length;
var myTabs = [];

app.doScript(funktionAusführen, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "DoIt");
function funktionAusführen(){
    
    for(var i=0; i<numberOfTextFrames; i++) {
                
        if(allTextFrames[i].tables.length > 0){
            myTabs.push(allTextFrames[i].tables[0]);
            }
        }

    var numberOfTables = myTabs.length;
    var myTableContent = [];
    var myTexts = [];
    
    for(var t=0; t<numberOfTables; t++) {
            var thisTab = myTabs[t];
            var allRows = thisTab.rows.everyItem().getElements();

            for(var n=0; n<allRows.length; n++) {
                var myRows = thisTab.rows[n] ;
                var thisRawContent = thisTab.rows[n].cells.everyItem("\t").contents.join(';');
                myTableContent.push(thisRawContent, "\r").toString();

                }
            myTabs.push(myTableContent);
    }
            
    var res = myTabs.pop('').join('').replace(/;/g, '\t');

    var textKasten = myDoc.textFrames.add({geometricBounds: [75, 106, 250, 198]});
    textKasten.insertionPoints[0].contents  = res;

    app.findGrepPreferences = changeGrepPreferences = null;  
    app.findGrepPreferences.findWhat = "(~b(?=Menge))"; 
    var found = app.findGrep();  
    app.changeGrepPreferences.changeTo = "\\r\\r";      
    app.changeGrep();  
    }

 

I just need plain text with no styles. It is used in an external text editing program.

 

This topic has been closed for replies.

1 reply

Peter Kahrel
Community Expert
Community Expert
January 13, 2021

It's not entirely clear to me what's going on there, but your approach looks unnecessarilty complicated. Text and tables are in separate frames I suppose, so that you have twelve frames on your sample spread. Correct? How does your script determine which tables should go to which text frames?

 

Anyway, to get the content of a table after the corresponding text, simply move the table after the text and use 

 

myTable.convertToText ('\t', '\r');

 

(You can't move a table, but you can move the character or paragraph that the table sits in.)

 

P.

_AWID_
_AWID_Author
Inspiring
January 13, 2021

Hallo Peter.

"It's not entirely clear to me what's going on there..."

Simple answer: I only need text and table contents from a document (a catalog with many articles). I will insert this (unformatted!) Into another InDesign document. The background is that we are getting a completely new catalog design and I want to avoid importing any old style or just part of a style (or a color) into this new and neat document.

Once you've tried, you know that the time wasting job is adding the text elements. Unless you have an automated content management system ... 

 

"myTable.convertToText ('\t', '\r');"

If only I had known that before! 😞

But as a beginner, I'm also happy with my result 🙂

Peter Kahrel
Community Expert
Community Expert
January 13, 2021

But as a beginner, I'm also happy with my result

 

Quite right!