Combine PDFs using Acrobat Pro

Hi

I have the below script to take the Finder selection and combine in Acrobat Pro, making bookmarks to mark where each individual document appears in the combined PDF. But I cannot for the life of me get the insert pages syntax and file save syntax to work in Acrobat (right at the end).

The rest works fine, creates a new PDF, renames it etc. But Can someone point me in the right direction for the Acrobat scripting on insert pages (and the Save command!). They are both throwing errors and night is falling over here…

Many thanks

tell application "Finder"
	set pdfList to every item of (get selection)
	set noerror to false
	set errormess to ""
	repeat until noerror
		set combined_file to item 1 of pdfList
		set combined_file to (duplicate combined_file with replacing) as alias
		set {name:Nm, name extension:Ex} to combined_file
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set noerror to false
		set errormess to ""
		try
			set newname to text returned of (display dialog "New Document Name" & errormess default answer "Combined Documents")
			set name of combined_file to newname & "." & Ex
			set noerror to true
		on error e number n
			delete combined_file
			if n = -128 then return
			set errormess to return & "ERROR - " & "'" & newname & "'" & " ALREADY EXISTS" & return & "SELECT NEW NAME"
			beep
		end try
	end repeat
	set appPath to path to application "Adobe Acrobat Pro"
	open combined_file using appPath
end tell

tell application "Adobe Acrobat Pro"
	activate
	set activeDoc to the active doc
	repeat with n from 2 to number of items in pdfList
		set pageCount to count of pages of activeDoc
		set thisName to item n of pdfList
		insert pages activeDoc after pageCount from thisName starting with 1 number of pages 1
	end repeat
	save activeDoc
end tell


I have managed to almost get this working. Last hurdle is to get bookmarking working. I have got the script to add the first bookmark for the first added PDF (at page 1), but am not managing to get it to add a bookmark for each additional PDF. The greyed/commented out part of the script below is in the exact same form as for the first “add bookmark” part of the script, but throws up errors.

Any help gratefully received on this last issue.

Cheers

tell application "Finder"
	set pdfList to every item of (get selection)
	set noerror to false
	set errormess to ""
	repeat until noerror
		set combined_file to item 1 of pdfList
		set combined_file to (duplicate combined_file with replacing) as alias
		set {name:Nm, name extension:Ex} to combined_file
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set noerror to false
		set errormess to ""
		try
			set newname to text returned of (display dialog "New Document Name" & errormess default answer "Combined Documents")
			set name of combined_file to newname & "." & Ex
			set noerror to true
		on error e number n
			delete combined_file
			if n = -128 then return
			set errormess to return & "ERROR - " & "'" & newname & "'" & " ALREADY EXISTS" & return & "SELECT NEW NAME"
			beep
		end try
	end repeat
	set appPath to path to application "Adobe Acrobat Pro"
	open combined_file using appPath
end tell

tell application "Adobe Acrobat Pro"
	activate
	set activeDoc to the active doc
	set Nm to name of item 1 of pdfList
	set Ex to name extension of item 1 of pdfList
	if Ex is missing value then set Ex to ""
	if Ex is not "" then set activeDocName to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
	tell document activeDoc
		make bookmark with properties ¬
			{destination page number:{1}, fit type:fit page, name:activeDocName}
	end tell
	repeat with n from 2 to number of items in pdfList
		
		set Nm to name of item n of pdfList
		set Ex to name extension of item n of pdfList
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set activeDocName to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		
		set pageCount to count of pages of activeDoc
		open (item n of pdfList) as alias
		set addDoc to the active doc
		set addPages to count of pages of addDoc
		insert pages activeDoc after pageCount from addDoc starting with 1 number of pages addPages with insert bookmarks
		(*
		tell document activeDoc
			make bookmark with properties ¬
				{destination page number:{(pageCount + 1)}, fit type:fit page, name:activeDocName}
		end tell
		*)
		close addDoc saving no
	end repeat
	create thumbs activeDoc
	save activeDoc
end tell


Last plea for help on this one…

I have found a piece of javascript that seems to set bookmarks:

      do script "this.bookmarkRoot.createChild('Bookmarkname', this.pageNum = 10, 1);"

But I cannot get it to do what I want - ie add name of added PDF as a bookmark! Javascript is even more unknown to me than applescript…

Any suggestions? Help would be much appreciated.

Thanks

Last post on this…

I managed to get it all to work (See below, complete with Growl notifications). Would no doubt be a bit speedier if I knew how to do the inserting pages with javascript (over to more gifted scripters than myself).

But functionally should assist those who have to bundle up several PDFs into a single PDF, with automatic bookmarking for each individual PDF. I use Acrobat Pro X, but would be good if others can report that it works with other versions of Acrobat.

Just select your PDFs in Finder, invoke the script (I use Quicksilver for this) and the script will create a custom named combined PDF in the same folder as the original PDFs, complete with bookmarks.

Enjoy!

Cheers

tell application "Finder"
	set pdfList to every item of (get selection)
	set totalItems to number of items in pdfList
	repeat with n from 1 to totalItems
		if name extension of item n of pdfList is not "pdf" then
			my notify(1)
			beep
			return
		end if
	end repeat
	set noerror to false
	set errormess to ""
	repeat until noerror
		set combined_file to item 1 of pdfList
		set tempOff to alert volume of (get volume settings)
		set volume alert volume 0
		set combined_file to (duplicate combined_file with replacing) as alias
		set volume alert volume tempOff
		set Nm to name of combined_file
		set Nm to text 1 thru ((count Nm) - 4) of Nm
		set noerror to false
		set errormess to ""
		try
			set newname to text returned of (display dialog "New Document Name" & errormess default answer Nm)
			set name of combined_file to newname & ".pdf"
			set noerror to true
		on error e number n
			set tempOff to alert volume of (get volume settings)
			set volume alert volume 0
			delete combined_file
			set volume alert volume tempOff
			if n = -128 then return
			my notify(2)
			set errormess to return & "ERROR - " & "'" & newname & "'" & " ALREADY EXISTS" & return & "SELECT NEW NAME"
			beep
		end try
	end repeat
	my notify(3)
	set appPath to path to application "Adobe Acrobat Pro"
	open combined_file using appPath
end tell

tell application "Adobe Acrobat Pro"
	activate
	set activeDoc to the active doc
	set Nm to name of item 1 of pdfList
	set activeDocName to text 1 thru ((count Nm) - 4) of Nm
	tell activeDoc
		make bookmark with properties ¬
			{destination page number:{1}, fit type:fit page, name:activeDocName}
	end tell
	repeat with n from 2 to totalItems
		set Nm to name of item n of pdfList
		set activeDocName to text 1 thru ((count Nm) - 4) of Nm
		set pageCount to count of pages of activeDoc
		open (item n of pdfList) as alias
		set addDoc to the active doc
		set addPages to count of pages of addDoc
		insert pages activeDoc after pageCount from addDoc starting with 1 number of pages addPages with insert bookmarks
		tell activeDoc
			make new bookmark at end with properties ¬
				{destination page number:{(pageCount + 1)}, fit type:fit page, name:activeDocName}
		end tell
		close addDoc saving no
	end repeat
	my notify(4)
	create thumbs activeDoc
	set view mode of activeDoc to pages and bookmarks
	save activeDoc
end tell


on notify(MessNum)
	tell application "GrowlHelperApp"
		-- Make a list of all the notification types 
		-- that this script will ever send:
		set the allNotificationsList to ¬
			{"PDFONLY", "DUPLICATEFILE", "STARTING", "FINISHED"}
		
		-- Make a list of the notifications 
		-- that will be enabled by default.      
		-- Those not enabled by default can be enabled later 
		-- in the 'Applications' tab of the growl prefpane.
		set the enabledNotificationsList to ¬
			{"PDFONLY", "DUPLICATEFILE", "STARTING", "FINISHED"}
		
		-- Register our script with growl.
		-- You can optionally (as here) set a default icon 
		-- for this script's notifications.
		register as application ¬
			"CombinePDFs" all notifications allNotificationsList ¬
			default notifications enabledNotificationsList ¬
			icon of application "Adobe Acrobat Pro"
		
		--       Send a Notification...
		if MessNum is 1 then
			notify with name ¬
				"PDFONLY" title ¬
				"ONLY PDF FILES PERMITTED" description ¬
				"Reselect ONLY PDF files" application name "CombinePDFs"
		else if MessNum is 2 then
			notify with name ¬
				"DUPLICATEFILE" title ¬
				"FILE ALREADY EXISTS" description ¬
				"Enter NEW filename" application name "CombinePDFs"
		else if MessNum is 3 then
			notify with name ¬
				"STARTING" title ¬
				"COMBINING FILES" description ¬
				"This may take a while" application name "CombinePDFs"
		else if MessNum is 4 then
			notify with name ¬
				"FINISHED" title ¬
				"FILES SUCCESSFULLY COMBINED" description ¬
				"Check bookmarks are OK" application name "CombinePDFs"
		end if
	end tell
end notify