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

Text frames close together: get content by script

Explorer ,
Jan 13, 2021 Jan 13, 2021

Copy link to clipboard

Copied

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.

 

Bildschirmfoto 2021-01-13 um 13.10.12.png

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.

 

TOPICS
Print , Scripting , Type

Views

249

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
Community Expert ,
Jan 13, 2021 Jan 13, 2021

Copy link to clipboard

Copied

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.

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 ,
Jan 13, 2021 Jan 13, 2021

Copy link to clipboard

Copied

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 🙂

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
Community Expert ,
Jan 13, 2021 Jan 13, 2021

Copy link to clipboard

Copied

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

 

Quite right!

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 ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

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

 

Is that something that only happens with the tables in the document or can you just write the converted content into a variable without changing the 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
Community Expert ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

The function returns a Text object, so

 

myText = myTable.convertToText ('\t', '\r').contents;

 

returns the table's content as a string. But the table is gone. If you want to keep the table you'll have to duplicate it first.

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 ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

Okay, than I have to avoid to save the document after converting.

It´s weird but once I converted the tables in a document, and after I undo my action to recover the tables, the script doesn´t works again (in that document): "The requested action could not be carried out because the object no longer exists" (or something like this. It´s a translation).

Why?

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 ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

update:

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){
            var thisTable = allTextFrames[i].tables[0].convertToText('\t', '\r').contents;
            myTabs.push(thisTable);
            }
        }
alert(myTabs); exit();
    }

If there is more than one table, all but the first become an object, even though I'll undo! With only one table in the document, I can run the script an infinite number of times. 

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
Community Expert ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

LATEST

> "The requested action could not be carried out because the object no longer exists".

> Why?

 

I noticed that. Looks like a bug.

 

> If there is more than one table, all but the first become an object, even though I'll undo! With only one table in the document, I can run the script an infinite number of times. 

 

When you convert the first table, all the references to the remaining tables are messed up. To avoid that, start processing the frames from the end and work your way to the beginning of the array:

for(var i=numberOfTextFrames-1; i >= 0; i--) {

P.

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