Highlighted

Script to remove all textFrames from a specific page

New Here ,
May 14, 2020

Copy link to clipboard

Copied

i've been digging around but thought i would post here as i'm guessing someone can quickly save my brain from exploding.

 

i simply want to remove all text frames on a specific page. i have a written a script that adds one textframe with text to a specific page number. but i want to modify my script so that it always removes every existing text frame on the page first.

 

i found this code in the forum (which i have modified but it is not working for me. not sure why.

 

original code (to remove all text frames on all pages):

 

 

app.activeDocument.textFrames.everyItem().remove();

 

 

my code (to only affect specific page number), where pageN is the page number:

 

 

app.activeDocument.pages[pageN].textFrames.everyItem().remove();

 

 

thoughts? 

 

Adobe Community Professional
Correct answer by Peter_Kahrel | Adobe Community Professional

In that scenario, do this: select the image, then run this script:

app.selection[0].parentPage.textFrames[0].remove();

 It removes the text frame that's on the page that the selected image sits on.

Topics

Scripting

Views

1.2K

Likes

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

Script to remove all textFrames from a specific page

New Here ,
May 14, 2020

Copy link to clipboard

Copied

i've been digging around but thought i would post here as i'm guessing someone can quickly save my brain from exploding.

 

i simply want to remove all text frames on a specific page. i have a written a script that adds one textframe with text to a specific page number. but i want to modify my script so that it always removes every existing text frame on the page first.

 

i found this code in the forum (which i have modified but it is not working for me. not sure why.

 

original code (to remove all text frames on all pages):

 

 

app.activeDocument.textFrames.everyItem().remove();

 

 

my code (to only affect specific page number), where pageN is the page number:

 

 

app.activeDocument.pages[pageN].textFrames.everyItem().remove();

 

 

thoughts? 

 

Adobe Community Professional
Correct answer by Peter_Kahrel | Adobe Community Professional

In that scenario, do this: select the image, then run this script:

app.selection[0].parentPage.textFrames[0].remove();

 It removes the text frame that's on the page that the selected image sits on.

Topics

Scripting

Views

1.2K

Likes

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
May 14, 2020 0
Adobe Community Professional ,
May 14, 2020

Copy link to clipboard

Copied

What happens when you run the code:

  • Do you get an error? Make sure you don't have the code in a try catch block to be alterted if there is an error
  • No textframes are deleted?
  • Some textframes are deleted and some not?

The code seems to be fine, one thing i could see was pageN should denote the pageIndex and not pagenumber. So to access the second page of the document the value should be 1 irrespective of the pagenumber allocated to it. Could you try the following and tell what you get

 

app.activeDocment.pages[pageN].isValid

 

If the above alerts true then we have a valid page. Then run the following code to see if you get the correct count of textframes

 

app.activeDocment.pages[pageN].textFrames.length

 

If you don't see the correct count i.e. if not all frames are being removed, it could be because some of these frames are placed on the masterpage applied to this page.

 

-Manan

 

Likes

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
Reply
Loading...
May 14, 2020 1
Adobe Community Professional ,
May 15, 2020

Copy link to clipboard

Copied

Pages can be addressed by index (or document offset), as Manan mentioned, but also by name. When you use an index, as in

 

app.activeDocument.pages[3]

 

you reference the fourth page in the document.

Another way to refer to a page is by its folio, that is, the page number printed on the page. To refer to page 4, use

 

app.activeDocument.pages.item('4')

 

P.

Likes

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
Reply
Loading...
May 15, 2020 1
New Here ,
May 15, 2020

Copy link to clipboard

Copied

i appreciate everyone's help!

 

a few things to clarify.

my indesign document has about 450 pages. each page has only two objects. an image (photograph) and a text frame. the text frame is the caption for the image.

 

my script takes a mouse selected image and generates a caption (textframe) below it based on the page number and file name (link) of the selected image.

 

anyhooos, i am able to create the textframe just fine on the page using this code, etc (this is why i am assuming i have a textframe since it says "textFrames"):

 

 

var textFrame = app.activeDocument.pages[pageN].textFrames.add();

 

everything is perfect. but if I replace the image with a different image, it then of course needs a new caption. so i want to be able to run the script and have it remove all the textframes if needed (theoretically only just the one), and then rebuild the image caption (textframe) from scratch.

 

i was able to remove the textframe with this code, but only after having created the variable "textFrame" with the above code.

 

 

textFrame.remove();

 

 

i know that that bit of code is not going in the right direction. i just need code that, when the script starts, removes everything on the page except the image.

Likes

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
Reply
Loading...
May 15, 2020 0
Adobe Community Professional ,
May 15, 2020

Copy link to clipboard

Copied

In that scenario, do this: select the image, then run this script:

app.selection[0].parentPage.textFrames[0].remove();

 It removes the text frame that's on the page that the selected image sits on.

Likes

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
Reply
Loading...
May 15, 2020 0
New Here ,
May 15, 2020

Copy link to clipboard

Copied

that worked! i knew it had to be simple and short.

 

i then modifed it to include everyItem.

 

app.selection[0].parentPage.textFrames.everyItem().remove();

 

beautiful! 

Likes

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
Reply
Loading...
May 15, 2020 0
rob day LATEST
Adobe Community Professional ,
May 15, 2020

Copy link to clipboard

Copied

Just keep in mind if there are text frames that are part of a group they would not get removed.

Likes

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
Reply
Loading...
May 15, 2020 0
Adobe Community Professional ,
May 15, 2020

Copy link to clipboard

Copied

Hi pjcrush,

could be that some of your text frames are part of a nested structure like groups or were pasted inside graphic frames.

If you want to get rid of them you could loop the page.allPageItems array and test every item if it is a textFrame or not. Then decide if you want to remove it.

 

var page = app.documents[0].pages[0];
var allItemsOnPage = page.allPageItems;

for( var n=0; n<allItemsOnPage.length; n++ )
{
	if( allItemsOnPage[n].constructor.name == "TextFrame" )
	{
		allItemsOnPage[n].remove();
	};
};

 

But even then this could fail for text frames that are visibly positioned on a page, but that are anchored to text of a text frame that is on a different page or the pasteboard. Just like that:

 

page-textFrames-length-is-ZERO-1.PNG

 

In the situation above page.textFrames.length is 0 !

But if you check property parentPage of the selected text frame its value is actually that page. And not null which would indicate the pasteboard. You could catch such a frame with a loop through spread.allPageItems and ask for parentPage like that:

 

var doc = app.documents[0];

var targetPage = doc.pages[0];
var spreadOfPage = targetPage.parent;

var allItemsOnSpread = spreadOfPage.allPageItems;

for( var n=0; n<allItemsOnSpread.length; n++ )
{
	if( allItemsOnSpread[n].constructor.name == "TextFrame" && allItemsOnSpread[n].parentPage == targetPage )
	{
		allItemsOnSpread[n].remove();
	};
};

 

The result would be this:

page-textFrames-length-is-ZERO-2.PNG

 

Other case with a facing pages document and a two-pages spread:

You want to target one of the pages in a spread and a text frame is crossing from one page to another.

Then usually the horizontal center of the text frame dictates where the text frame is:

 

textFrames-on-Spread-crossing-spine.PNG

 

Regards,
Uwe Laubender

( ACP )

Likes

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
Reply
Loading...
May 15, 2020 2
Adobe Community Professional ,
May 15, 2020

Copy link to clipboard

Copied

Hi pjcrush,

yet another thing:

 

If you address a page by its index number with doc.pages[n] the numbering start is 0 and not 1.

The first page in your document is doc.pages[0] and not doc.pages[1] . Perhaps you addressed the wrong page?

 

Regards,
Uwe Laubender

( ACP )

Likes

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
Reply
Loading...
May 15, 2020 0
Adobe Community Professional ,
May 15, 2020

Copy link to clipboard

Copied

where pageN is the page number

 

Not sure if this is the problem, but pages is a list so page 1 would be pages[0].

 

Also, the script wouldn’t necessarily get all of the text frames—it wouldn’t get a frame inside of a group. for that something like this should work for page 1:

 

var api = app.activeDocument.pages[0].allPageItems;
    
for (var i = 0; i < api.length; i++){
    if (api[i].constructor.name == "TextFrame") {
            api[i].remove();
    }
};   

Likes

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
Reply
Loading...
May 15, 2020 0
Adobe Community Professional ,
May 15, 2020

Copy link to clipboard

Copied

Sorry I didn’t see the other posts before I posted

Likes

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
Reply
Loading...
May 15, 2020 0