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?
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.
Copy link to clipboard
Copied
What happens when you run the code:
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
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.
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.
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.
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!
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.
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:
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:
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:
Regards,
Uwe Laubender
( ACP )
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 )
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();
}
};
Copy link to clipboard
Copied
Sorry I didn’t see the other posts before I posted