Skip to main content
Inspiring
August 17, 2022
Answered

Delete paragraphs from a text frame except first para

  • August 17, 2022
  • 2 replies
  • 461 views

From a text frame which is linked to two or three frames, I want to delete all paragraphs excpet first one. I have already done using below code but looking for shorter code (without using for loop) if possible.

var i;

var myDoc = app.activeDocument;

var myFrame = myDoc.textFrames.itemsByName("virender");

var my_count = myFrame.parentStory.paragraphs.length;

for (i = my_count; i > 1; i--) {

myFrame.parentStoryparagraphs.item(i-1).contents = "";

}

This topic has been closed for replies.
Correct answer Peter Kahrel

And without a for-loop (but why?):

app.activeDocument.textFrames.item('virender')
  .paragraphs
  .itemByRange (1, -1)
  .remove();

2 replies

Community Expert
August 17, 2022

Hi @virender_CTS ,

let's assume you want to remove all paragraphs in red of this situation:

So that the result is this:

Then you could use this code:

app.documents[0].textFrames.itemByName("Virender").
texts[0].paragraphs.itemByRange( 1,-1).remove();

 

Also note that in a situation like that, when the said text frame has overset text and is the last one in the story:

The result could be like that:

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Inspiring
August 17, 2022

Hi Uwe, many thanks for replying.

Regards

Virender

Peter Kahrel
Community Expert
Community Expert
August 17, 2022

If you want shorter code, use this:

par = app.activeDocument.textFrames.item('virender').paragraphs.everyItem().getElements();
for (i = par.length-1; i > 0; i--) {
  par[i].contents = '';
}

P.

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
August 17, 2022

And without a for-loop (but why?):

app.activeDocument.textFrames.item('virender')
  .paragraphs
  .itemByRange (1, -1)
  .remove();
Community Expert
August 17, 2022

Hi Peter,

did not see your latest reply with the itemByRange() code.

 

Best,

Uwe Laubender
( Adobe Community Professional )