Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Combine 2 PDF files into 1 using applescript

Hi all,


I am just starting out with applescript and could do with a pointer please. I have written a script which saves two pdf files from an active Illustrator document in the source folder of that document. The issue I am have now is that I cannot get the two files to combine into a single PDF file.


The whole script is quite long and is works apart from the combine PDF part, I will paste the part of the code I am having problems with and hopefully someone can help me out please:


------------------- start of PDF combine script


tell application "Finder"

try

open (every document file of container of file_path whose name contains ".pdf")

on error

display dialog "no PDF file located"

end try

end tell

tell application "Adobe Acrobat Pro" to activate

tell application "Adobe Acrobat Pro"

set docRefA to the active doc

tell application "Finder"

try

open (every document file of container of container of file_path whose name contains ".pdf")

on error

display dialog "no PDF file located"

end try

end tell

tell application "Adobe Acrobat Pro" to activate

tell application "Adobe Acrobat Pro"

set docRefb to the active doc

bring to front docRefA

insert pages docRefA after 1 from docRefb starting with 1

end tell

end tell

------------------------------------- script end


Both PDF documents open and are set as the variables docRefA and docRefb, but the acrobat command 'bring to front' and 'insert page' do not seem to be working. I get an error come up in an illustrator dialog box saying "Adobe Acrobat Pro got an error: document "84056_R1.pdf" doesn't understand the bring to front message".

Mac OS X (10.6.8)

Posted on Jun 22, 2012 6:26 AM

Reply
26 replies

Jun 23, 2012 2:13 PM in response to twtwtw

The section of the script I have placed on here is the end of a larger script.

The script starts by saving two PDF files from an adobe illustrator file in the source folder of the illustrator file.

The script is used in a production environment with hundreds of job folders each with a different number and location. This means I can never reference a single static source folder. The beginning of the script uses the current document as the reference for saving the file and is placed in a variable, which is where I can call the two PDFs it makes from in the last section of the script.


The illustrator files are packaging artwork files. The first PDF is a PDF of the whole job and all it's layers. The second PDF is a PDF of the varnish plate of that job with all the layer bar 2 deleted out. I want to open the whole job PDF and insert the second varnish plate PDF after it, saving it and them closing it. I can open both PDF files after creation but cannot get one to insert after the other.

Jun 24, 2012 11:56 AM in response to big_murph

Ok, that's good, but you didn't answer most of my questions. so once more:


  1. Which version of Acrobat Pro are you using?
  2. Do you have any third-party scripting additions installed?
  3. Is this block of code embedded in an application tell block, a script handler, a script object, or etc.


This isn't idle curiosity. Any of these things can change script dynamics and generate errors.


However, reading over the description of your process flow I think you'd get better results downloading and installing the pdftk command line utility. Follow the install pdftk link on that page and look for the OS X dmg download (it says Snow Leopard, but they meant SL and later), put the utility in /usr/local/bin/ as per instructions. Pdftk will give you more control over the process, faster processing, and a less flakey scripting environment than Acrobat Pro.


The way this would work using pdftk (after you've installed the package) is as follows. First, keep references from the beginning of the script to the two PDF files you saved. I haven't looked at Illustrator's dictionary, but generally save commands return an alias to the saved file. so you'd want to modify those lines to say something like:


set firstFilePath to POSIX path of (save ...)


the script segment you'd use to combine the files is this:


-- assuming the saved posix paths are in the variables firstFilePath and secondFilePath

set {oldTID, my text item delimiters} to {my text item delimiters, "/"}

set outputFilePath to (text items 1 thru -2 of firstFilePath & "combined_output.pdf") as text

set my text item delimiters to oldTID


(*

build the pdftk command: sets the references A and B to the file paths, then

cat A1 B A2-end

combines the first page of file A, all of file B, then the remainder of file A

*)

set cmd to "/usr/local/bin/pdftk A=" & quoted form of firstFilePath & " B=" & quoted form of secondFilePath & " cat A1 B A2-end output " & quoted form of outputFilePath

do shell scriptcmd

Jun 25, 2012 1:04 AM in response to twtwtw

1. I am using Adobe Acrobat pro X (10.0.0)

2. No scripting additions are installed.

3. The end part of the script is this:


------------------- start of PDF combine script


tell application "Finder"

try

open (every document file of container of file_path whose name contains ".pdf")

on error

display dialog "no PDF file located"

end try

end tell

tell application "Adobe Acrobat Pro" to activate

tell application "Adobe Acrobat Pro"

set docRefA to the active doc

tell application "Finder"

try

open (every document file of container of container of file_path whose name contains ".pdf")

on error

display dialog "no PDF file located"

end try

end tell

tell application "Adobe Acrobat Pro" to activate

tell application "Adobe Acrobat Pro"

set docRefb to the active doc

bring to front docRefA

insert pages docRefA after 1 from docRefb starting with 1

end tell

end tell

------------------------------------- script end


So it is only in the tell blocks you can see here.


The save line is this:



tell application "Adobe Illustrator"

-- create the COMPOSITE High res PDF

tell me to set save_name to SavedPDFName(file_path, "")

save docRef as pdf in file (dest_folder & save_name) with options {class:PDF save options, PDF preset:"•Alternate_PDF_v1"}

end tell


I am not sure if it returns an alias.



As this script has to be installed on 50+ Macs I was trying to keep it to a single script install without any third party installs.

Would this 'pdfx command line utility' need to be installed on all Macs using the script?


I am very new to applescript and am learning as I go, thank you for your patience.

Jun 25, 2012 3:39 AM in response to big_murph

Hello


Does something simple like the following script work?

Assuming there are two documents named "1.pdf" and "2.pdf" already open.


--TEST SCRIPT

tell application "Adobe Acrobat Pro"

insert pages document "1.pdf" after 1 from document "2.pdf" starting with 1 number of pages 1

end tell

--END OF TEST SCRIPT


If it fails, I'd think something basic is wrong in your environment.


H

Jun 25, 2012 5:52 AM in response to twtwtw

I was also thinking would I be able to use System Event to use the insert page shortcut?


Something like this:


-----------------------------

tell application "System Events" to tell process "Adobe Acrobat Pro"

keystroke "i" using {command down, shift down} -- insert page

end tell

-----------------------------


How would I then navigate to the second PDF? Is there a way of pointing the window to a definded source folder?

Jun 25, 2012 6:50 AM in response to big_murph

Hello


Well, then you may try javascript instead.

Something like script below.


(If the second document always has the same number of pages, you don't have to open it at all.

Just specify its last page number (0-based) as the nEnd value.)


Good luck,

H


--SCRIPT (part)

tell application "Finder"

try

set a1 to (document file 1 of container of file_path whose name ends with ".pdf") as alias

set a2 to (document file 1 of container of container of file_path whose name ends with ".pdf") as alias

on error

display dialog "no PDF file located"

end try

end tell


set js to "// combine pdfs

var path1 = \"" & a1's POSIX path & "\";

var path2 = \"" & a2's POSIX path & "\";

var doc1 = app.openDoc(path1);

var doc2 = app.openDoc(path2);

doc1.bringToFront();

doc1.insertPages ({

nPage: 0,

cPath: path2,

nStart: 0,

nEnd: doc2.numPages - 1

});

doc2.closeDoc();"


tell application "Adobe Acrobat Pro"

activate

do script js

end tell

--END OF SCRIPT

Jun 25, 2012 9:39 AM in response to big_murph

1. ok, you're using a version of AP one later than mine. That shouldn't be a problem, but it might account for differences in execution if nothing else makes sense. Let's keep that in mind for later.


2. Illustrator (for some reason) returns a reference to the illustrator document from the save command, not to the saved file. But documents have a file path property, so you can retain the file path for later use this way:


set doc1 to save docRef as pdf in file (dest_folder & save_name) with options {class:PDF save options, PDF preset:"•Alternate_PDF_v1"}

set firstFilePath to POSIX path of ((file path of doc1) as alias)


3. You can save the script as a script bundle and package the pdftk utility within it - that would keep it self-contained. To do that, do the following:


  1. open your script in the AppleScript editor, and save it as a script bundle.
  2. extract the files from the installer on the dmg. you can do that in two ways: install the utility on one machine and copy the installed folder, or use Pacifist to extract it to the desktop. If you install it, it will be the folder pdftk in /opt/pdflabs. Pacifist is shareware, but allows free usage with a nag screen. It's well worth buying it, though - comes in handy more than you'd think.
  3. in the applescript editor, click the Bundle Contents toolbar button to open the bundle drawer, then drag the entire pdftk folder you extracted into the drawer. That saves it in the bundle. Make sure it's named "pdftk" (Pacifist may add 'Folder' to the name; right click in the drawer to rename)


Now you can set up a variable with the utility path and use it like so:


set pdftkUtilityPath to quoted form of POSIX path of (path to resource "pdftk" in directory "pdftk/bin")


-- …


set cmd to pdftkUtilityPath & " A=" & quoted form of firstFilePath & " B=" & quoted form of secondFilePath & " cat A1 B A2-end output " & quoted form of outputFilePath

do shell scriptcmd

you can use the script bundle exactly the way you would use a normal script. The only difference is that the utility is contained within it for portability.

Jun 25, 2012 10:57 AM in response to big_murph

Hi,



big_murph wrote:


1. I am using Adobe Acrobat pro X (10.0.0)

2. No scripting additions are installed.

3. The end part of the script is this:


------------------- start of PDF combine script

tell application "Adobe Acrobat Pro"

set docRefb to the active doc

bring to front docRefA

insert pages docRefA after 1 from docRefb starting with 1

end tell


Hi,


For me, if I don't use the property "number of pages", I get an error.

Did you try using "number of pages" in your command?

Like this


tell application "Adobe AcrobatPro"


setdocRefbtotheactive doc

--bring to front docRefA


insert pagesdocRefAafter 1 fromdocRefbstarting with 1 number of pages 1

endtell

Combine 2 PDF files into 1 using applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.