Search code examples
javascriptadobe-indesign

Indesign : in a table cell, how to get and move any paragraph content


I need help for complete my code.

I created a table and inserted 4 paragraphs texts and a image into the same cell. I want to modify the content of the cell like this :

1: move 'text3' before 'text4'

2: move the image at the first place

I understood the principe of the insertionpoints, however after many tests i didn't succeed the moves...

Please help me to fix, Thank you !

var myDocument = app.documents.add();
var myPage = myDocument.pages;

var tf = myPage[0].textFrames.add({ geometricBounds: [20, 20, 100, 150] });

insertionpoint = tf.insertionPoints[0];
app.select(insertionpoint); 
mySelection=app.selection[0];

mySelection.tables.add({
                                    label : "theTable", 
                                    bodyRowCount : 7, 
                                    columnCount : 8, 
                                });                                             

tableList = myDocument.stories.everyItem().tables.everyItem().getElements();

myTable = tableList[0];

myTable.columns[4].cells[5].contents = "test2\n"; 
myTable.columns[4].cells[5].insertionPoints[-1].contents = "test4\n";
myTable.columns[4].cells[5].insertionPoints[0].contents = "test1\n";
myTable.columns[4].cells[5].insertionPoints[-1].contents = "test3\n";

var myFrame = myTable.columns[4].cells[5].insertionPoints[-1].rectangles.add({
                    name : "theFrame"
                });
myFrame.horizontalScale *= 2;
myFrame.verticalScale *= 3;     

try 
{ 
    myFrame.place(File("Put here a valid local image path"));               
}
catch(err)
{
    myFrame.fillColor = "Black";
};

myFrame.fit(FitOptions.proportionally); 
myFrame.fit(FitOptions.frameToContent); 

/* 1: Code for moving "test3\n" paragraph before "test4\n" in the cell (4,5) */


/* 2: Code for moving the rectangle in first place in the cell (4,5) */

Solution

  • Here is the rather clumsy solution but it works:

    /* 1: Code for moving "test3\n" paragraph before "test4\n" in the cell (4,5) */
    app.findGrepPreferences = NothingEnum.nothing;
    app.findGrepPreferences.findWhat  = '^(.+?)\\n(.+?)\\n(.+?)\\n(.+?)\\n';
    app.changeGrepPreferences.changeTo = '\\n$1\\n$2\\n$4\\n$3';
    myTable.columns[4].cells[5].changeGrep();
    
    /* 2: Code for moving the rectangle in first place in the cell (4,5) */
    myFrame.select();
    app.cut();
    myTable.columns[4].cells[5].insertionPoints[0].select();
    app.paste();