Copy link to clipboard
Copied
Hi All,
i have three text frames Frame1, Frame 2 and Frame3 using itemByName. Each frame has a table and one cell, each cell has two paragraphs formatted one head and another paragraph text. I intend to replace text from each cell with "Head 1" for heading and "xxx" for paragraph. Below is the script.
var myDoc = app.activeDocument;
var myFrame1 = myDoc.textFrames.itemByName("Frame1");
var myTable1 = myFrame1.tables[0];
var myCell1 = myTable1.cells.firstItem();
myCell1.texts[0].paragraphs[0].contents = "Head1\r";
myCell1.texts[0].paragraphs[1].contents = "xxx";
var myFrame2 = myDoc.textFrames.itemByName("Frame2");
var myTable2 = myFrame2.tables[0];
var myCell2 = myTable2.cells.firstItem();
myCell2.texts[0].paragraphs[0].contents = "Head1"; // without \r
myCell2.texts[0].paragraphs[1].contents = "xxx";
After the script, the formatting of second paragraph which is normal text get changed. Any suggestion to retain it?
Hi @virender_CTS ,
perhaps it will help you to see into the value of length with the characters of your paragraphs and also the length that the string as value of contents will return. Hint: an end of paragraph sign is part of the paragraph.
If you exchange all the contents of the first paragraph without using an end of paragraph sign what will happen?
The text of the next paragraph will be merged with the text of the first paragraph and the result is the formatting of the first paragraph.
N
...Copy link to clipboard
Copied
Hi @virender_CTS ,
perhaps it will help you to see into the value of length with the characters of your paragraphs and also the length that the string as value of contents will return. Hint: an end of paragraph sign is part of the paragraph.
If you exchange all the contents of the first paragraph without using an end of paragraph sign what will happen?
The text of the next paragraph will be merged with the text of the first paragraph and the result is the formatting of the first paragraph.
Not so when you try this in the GUI. There the formatting will change to the formatting of the second paragraph so the result seems to be different. But in fact InDesign is doing the following equivalent in the GUI, I think:
// Text frame selected!
app.select( app.selection[0].parentStory.paragraphs[0] );
app.selection[0].contents = "";
app.selection[0].contents = "Head";
If you want both kinds of formatting in one paragraph ( no idea if that's what you are after! ) you have to change your strategy. First change the contents of the paragraph without touching the end of paragraph sign, then remove the paragraph sign to merge both paragraphs into one:
// Text frame selected:
app.selection[0].parentStory.paragraphs[0].characters.itemByRange(0,-2).contents = "Head";
app.selection[0].parentStory.paragraphs[0].characters[-1].remove();
The applied paragraph style, if there is one applied, would be still the one that is applied.
The different formatting of the text of the formerly second paragraph is now a format override.
Regards,
Uwe Laubender
( Adobe Community Expert )
Copy link to clipboard
Copied
Ah. My bad. I did not assume a table in the text frame.
But I think you could still follow along with the paragraphs inside a table cell.
Regards,
Uwe Laubender
( Adobe Community Expert )
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi Uwe,
This line worked as this code do not conisder paragraph symbol.
app.selection[0].parentStory.paragraphs[0].characters.itemByRange(0,-2).contents = "Head";
Below is the before and after result screenshot as I intent to keep paragraph styles intact.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more