Duplicate a page in a PDF via Applescript

Hi,

I try to duplicate a page within a PDF document. For example I have a document with 8 pages and I want to duplicate page number 2.

So I have tried to insert page 2 from document 1 into document 1 on position 2 with the number of pages of 1 (see example). But it doesn’t work of course. :slight_smile:

How can I do it?

tell application "Adobe Acrobat"
	activate
	tell active doc
		insert pages document 1 after 2 from document 1 starting with 2 number of pages 1
	end tell
end tell

You may use :

-- Modified by YK 2017/01/21
-- Modified 2016-07-18 by Takaaki Naganoya
--Original By Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz"
use framework "QuartzCore"

set inFile to (choose file of type {"pdf"} with prompt "Choose your PDF files:")
set targPage to 2
set maxPage to pdfPageCount(inFile) of me
if 0 < targPage and targPage ≤ maxPage then
	--Skip
else
	display dialog "Page Number Range Error"
	return
end if

duplicateSpecificPageInPDF(inFile, targPage) of me


on duplicateSpecificPageInPDF(inFile, targPageNum)
	-- make URL of the original PDF
	set inNSURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of inFile)
	set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
	
	set oldDocCount to ((theDoc's pageCount()) - 1)
	
	--Make a Blank PDF
	set newPDFdoc to current application's PDFDocument's alloc()'s init()
	
	set newDocCount to 0
	
	repeat with i from 0 to oldDocCount
		if i = (targPageNum - 1) then
			set thePDFPage to (theDoc's pageAtIndex:i) -- zero-based indexes
			(newPDFdoc's insertPage:thePDFPage atIndex:newDocCount)
			set newDocCount to newDocCount + 1
		end if
		set thePDFPage to (theDoc's pageAtIndex:i) -- zero-based indexes
		(newPDFdoc's insertPage:thePDFPage atIndex:newDocCount)
		set newDocCount to newDocCount + 1
		
	end repeat
	
	--Delete the original file and give its name to the one with the duplicate page
	set aRes to my deleteFile(inFile)
	if aRes then
		set aRes to (newPDFdoc's writeToURL:inNSURL)
	end if
	
	return aRes
	
end duplicateSpecificPageInPDF


on pdfPageCount(aFile)
	set theURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of aFile)
	set aPDFdoc to current application's PDFDocument's alloc()'s initWithURL:theURL
	set aRes to aPDFdoc's pageCount()
	return aRes as integer
end pdfPageCount


on deleteFile(aFile)
	set filePath to current application's NSString's stringWithString:(POSIX path of aFile)
	set fileManager to current application's NSFileManager's defaultManager()
	set aRes to fileManager's removeItemAtPath:filePath |error|:(reference)
	-->	{‹‹‹‹‹true, ‹‹‹‹‹missing value‹‹‹}
	-->	{‹‹‹‹‹false, ‹‹‹‹‹(NSError) Error}
	copy aRes to {aFlag, aReason}
	return aFlag
end deleteFile

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) samedi 21 janvier 2017 11:11:00

Uh, I hoped that duplicating a page is one instruction, or maximum three or four. But this is a whole software suite! :slight_smile:

Thank You for this, I will take a closer look to it.

Maybe it may be done with few instructions when we own Acrobat but I don’t so I posted a script doing the job without Adobe product.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) samedi 21 janvier 2017 17:53:11

Ah, okay. But I want to script Acrobat.

The background for this:
I have documents which contain facing pages as one page. Fore reasons of later workflow I have to cut the single facing pages to two separate facing pages.

When I have duplicated all pages except the first and the last page, I have a script what is cropping all inner pages to their left or right sides. Only the page duplicating is missing, because I do not know how.

This is my source:
┌───┬───┐
│═══………═══│
│═══………═══│
└───┴───┘

This is my destination:
┌───╥───┐
│═══║═══│
│═══║═══│
└───╨───┘

So, I have to

  1. duplicate the page,
  2. to crop the first page to the left 50 percent,
  3. crop the second page to the right 50 percent.

I got the dictionary of version 15.

Insert page is described as :
insert pages‚v : Insert page(s) from another document.
insert pages [specifier] : The document to receive the inserted page(s).
after integer : The page after which page(s) will be inserted.
from specifier : The source document, where the inserted page(s) are from.
starting with integer : The first page to insert.
number of pages integer : The number of pages, including the first page, to insert.
[insert bookmarks boolean] : Whether to copy bookmarks referencing the inserted pages over, or not (default is yes).

So my understanding is that you need to make a new document with the content of page 2 then insert page 1 of the new document after page 2 of the original one.

Or, maybe easier, duplicate the document then
insert in original document after page 2 the page 2 from duplicate.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) samedi 21 janvier 2017 20:02:33

I do not understand how to read the directory. There are many instructions how to code in Applescript, but not one instruction how to read the directory. :frowning:

Someone should make a video or an instruction how to use the dictionary. :wink:

I have coding experience in Basic and Assembler, some in Applescript, Bash scripts, PHP and HTML. I am understanding the man pages, but I don not understand the Applescript directory.

When the editor is open

menubar > File > Open the dictionary ⇧⌘O
It open a window in which you may scroll to select the application whose dictionary you need.

You may also drag the application’s icon onto the Script editor’s one.

The Help available from the Script Editor describe a third way to reach dictionaries : menubar > Window > Library.
By default it offer a “short” list of applications but there is a + button allowing us to navigate to applications to add.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) dimanche 22 janvier 2017 11:02:47

I know how to open the directory, but I do not understand the content.

For example:
I can move a page in Acrobat by dragging it with the mouse.
If I press the alt key while dragging, acrobat makes a duplicate.

So, it should be possible to do so with Applescript.

I open the directory and I find in žRequired Suite" (whatever this means):
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
movev : Move object (only Page class objects can be moved).
ƒƒmove specifier : The Page object to move.
ƒƒƒto location specifier : The new location for the object.
ƒƒƒâ†’ specifier
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈

But I do not understand how to use. I do not understand the meaning.

Yess! :slight_smile: I have the solution!

tell application "Adobe Acrobat"
	activate
	set DocName to name of active doc
	insert pages document DocName after 2 from document DocName starting with 2 number of pages 1
end tell

This duplicates the Page number 2 in a PDF document. This is the simple solution I was looking for! :slight_smile:

And here is the complete solution:

If you have facing pages as single pages, than you have to cut the pages in the middle. Here is an Applescript what is doing it for you.

What is the script doing?
First the script is duplicating all inner pages beginning from the end of the document (because the page counter would be much more complicated in other direction)
Then all inner pages are cropped to 50 percent of page width. The even pages are cropped to the left half page, the odd pages are cropped to their right half.

Perhaps someone has some use of the script. :slight_smile:
I didn’t find a solution in the internet. So I made it.

tell application "Adobe Acrobat"
	activate
	set DocName to name of active doc --determine document name of the PDF
	set HighPage to count of page --determine Number of pages in the PDF
	
	--duplicating pages from last page minus one to second page (backwards)
	repeat with i from HighPage - 1 to 2 by -1
		insert pages document DocName after i from document DocName starting with i number of pages 1
	end repeat
	
	tell active doc
		set HighPage to count of page --determine Number of pages in the PDF again
		
		
		repeat with i from 2 to HighPage - 1 by 2
			
			set j to i + 1
			
			tell page i -- crop the odd page to the left half of the page
				set {Links, Oben, Rechts, Unten} to media box
				set crop box to {Links, Oben, Rechts / 2, Unten}
			end tell
			
			tell page j -- crop the even page to the right half of the page
				set {Links, Oben, Rechts, Unten} to media box
				set crop box to {Rechts / 2, Oben, Rechts, Unten}
			end tell
			
		end repeat
	end tell
end tell

After installing the last security update and restarting my Mac the script will not longer work. :frowning:
The insert pages is not longer working. The reason is, that the documents name is empty.

Does anyone have an idea?

tell application "Adobe Acrobat"
	activate
	set DocName to name of active doc --determine document name of the PDF
end tell

. produces this output:

tell application "Adobe Acrobat"
	activate
	get name of active doc
		--> ""
end tell
Ergebnis:
""

This used to work for me through Yosemite. In Sierra it appears to be broken.