1 2 Previous Next 26 Replies Latest reply: Jun 25, 2012 10:57 AM by Jacques Rioux Go to original post
  • 15. Re: Combine 2 PDF files into 1 using applescript
    Frank Caggiano Level 7 Level 7 (23,840 points)

    except for the one weird factor that he wants to insert document 2 afterpage 1 of document 1.

    I missed that bit. Just went back and reread the whole thread ans still don't see that. Time for ne glasses.

  • 16. Re: Combine 2 PDF files into 1 using applescript
    twtwtw Level 5 Level 5 (4,690 points)

    glasses wont help - you need a microscope for this one:

      insert pages docRefA after 1 from docRefb starting with 1 number of pages (countof pages of docRefb)

     

    'after 1' means after page 1.  I assume he's trying to keep a title page and insert a block of pages (table of contents, or something like that) immediately after.

  • 17. Re: Combine 2 PDF files into 1 using applescript
    big_murph Level 1 Level 1 (0 points)

    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.

  • 18. Re: Combine 2 PDF files into 1 using applescript
    twtwtw Level 5 Level 5 (4,690 points)

    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 script cmd


  • 19. Re: Combine 2 PDF files into 1 using applescript
    big_murph Level 1 Level 1 (0 points)

    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.

  • 20. Re: Combine 2 PDF files into 1 using applescript
    Hiroto Level 5 Level 5 (5,025 points)

    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

  • 21. Re: Combine 2 PDF files into 1 using applescript
    big_murph Level 1 Level 1 (0 points)

    Some of the commands in the adobe acrobat script dictionary are not recognised, 'insert pages' being one of them. On running the script it throws up this error:

     

    "Adobe Acrobat Pro got an error: document "1.pdf" doesn’t understand the insert pages message."

  • 22. Re: Combine 2 PDF files into 1 using applescript
    big_murph Level 1 Level 1 (0 points)

    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?

  • 23. Re: Combine 2 PDF files into 1 using applescript
    Hiroto Level 5 Level 5 (5,025 points)

    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

  • 24. Re: Combine 2 PDF files into 1 using applescript
    twtwtw Level 5 Level 5 (4,690 points)

    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 script cmd


    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.

  • 25. Re: Combine 2 PDF files into 1 using applescript
    twtwtw Level 5 Level 5 (4,690 points)

    Acrobat Pro has an insert pages command in its applescript dictionary.  GUI scripting is last resort - it's difficult to get right and prone to breakage with updates. Always use applescript commands if they are available.

  • 26. Re: Combine 2 PDF files into 1 using applescript
    Jacques Rioux Level 4 Level 4 (1,960 points)

    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 Acrobat Pro"

      set docRefb to the active doc

              --bring to front docRefA

      insert pages docRefA after 1 from docRefb starting with 1 number of pages 1

    end tell


1 2 Previous Next