Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Remove / Delete pages with javascript (or vbs)

New Here ,
Jul 23, 2009 Jul 23, 2009

There is a MyPage.Add()... There is no MyPage.remove()?

Can I remove a given page number with a script?

The problem:

I have a facing pages problem. I essentially need to delete two pages, leave two pages, delete two and so on for about 2000 pages. Sure I could click and delete, but what would be the fun in that?

The pages are created from a facing-pages datamerge. Why CS4 can't handle facing pages datamerge is beyond me... Either way, I need a work around.

Visually in the PAGES tab, what I have is this

Data Merge Entry Data Merge Entry

Facing Page AFacing Page B
Data Merge Entry AData Merge Entry A
Data Merge Entry BData Merge Entry B
Data Merge Entry CData Merge Entry C
Data Merge Entry DData Merge Entry D

Resulting in A,A,B,B,C,C,D,D, etc.

What I need to end with is

AB
A
B
C
D

Resulting in A,B,C,D, etc.

The rules: The database is untouchable. Facing Pages Side A and Side B are formated differently.

Any and all help or information leading to a working work-around willl be handsomly rewarded the moment I get my check from a rich African uncle who, I sadly found out through an email today, had recently passed away.

Many thanks!

Marc

TOPICS
Scripting
4.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 23, 2009 Jul 23, 2009

But there is a myPage.remove()

Are there threaded text frames on the pages you want to remove? In a script, this behaves just like in the UI: threaded text is shifted to the next frame in the thread (usually, on the next page). So if that's the case, this script will work but not the way you intended it.

Anyway, you want to remove one page, then skip two pages -- remove two pages -- skip 2 -- etc., until the end of the document.

This Javascript should do it:

var curpage = 0;

app.activeDocument.pages

...
Translate
Community Expert ,
Jul 23, 2009 Jul 23, 2009

>There is no MyPage.remove()?

There is! When you add a page, you add it to the page collection, so your starting point is the page collection:

myDoc.pages.add (...);

To delete a page, your starting point is not the page collection, but the page itself. For instance, to delete the sixth page, you would use this:

myDoc.pages[5].remove ();

(JavaScript starts counting at zero, so page 6 is the fifth page.) Note the difference between pages and page[5]: the former is the collection of pages, the latter is one page.

Peter

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 23, 2017 Aug 23, 2017
LATEST

Hi Peter,

I saw your response here and was hoping you could assist me, I am trying to use your code to delete a page in my document (page 3 to be specific or javascript page 2). I have used the method you noted above ( myDoc.pages[5].remove (); ) however it does not seem to work. I have entered the javascript as a mouse down action on a radio button. I would really appreciate your assistance here?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 23, 2009 Jul 23, 2009

But there is a myPage.remove()

Are there threaded text frames on the pages you want to remove? In a script, this behaves just like in the UI: threaded text is shifted to the next frame in the thread (usually, on the next page). So if that's the case, this script will work but not the way you intended it.

Anyway, you want to remove one page, then skip two pages -- remove two pages -- skip 2 -- etc., until the end of the document.

This Javascript should do it:

var curpage = 0;

app.activeDocument.pages[curpage].remove();
while (curpage < app.activeDocument.pages.length)
{
curpage += 2;
app.activeDocument.pages[curpage].remove();
app.activeDocument.pages[curpage].remove();
}

It boings out with an error at the end if your document contains an odd number of pages (?) because it might try to remove a page after the last one, but that's not a critical error, methinks.

I've tested it on a 30+ document, and it correctly deleted #1, then #4 and #5, then #8 and #9, etc. Still, paranoia is a healthy lifestyle (when applied to computers), so run it on a copy, will ya.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 24, 2009 Jul 24, 2009

Thank you both very much for the help! That code worked like a charm!

-Cheers!

Marc

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines