Applescript for Separating Pages of PDFs

Two weeks ago I started attempting to learn applescript as a way to make things simpler in my department. After much reading of these forums, lots of trial and error, and enough expletives to hurt someone I have a script that will extract individual pages from multi-page PDF files and rename them sequentially for imposition.

There are several things in the script that, I feel, are less efficient than they could be, but my extreme noobescence(?) has its limits. (I don’t know what I’m doing or talking about, but this script is long and clunky and requires too many mouse clicks for my taste.) Please, look it over and let me know what changes I could make to optimize this nasty, little jewel.

Thank you for your time and input.
Pat (omitted)


    tell application "Finder"
    --This bothers me because it assumes that there is an alias called "Acrobatlaunch" on disk "Samson"
    --Is there a way that I could make it work on any machine with Acrobat 7 running OSX?
       open file "Samson:Applications:Acrobatlaunch"
    end tell
    --BEGIN REPEATED BLOCK
    tell application "Adobe Acrobat 7.0 Professional"
       activate
       choose file with prompt "Select PDF to Covert to Single Page"
       copy the result to Orgdoc
       open Orgdoc
    end tell
    tell application "Finder"
       activate
       set Orgdocname to name of Orgdoc as string
    end tell
    tell application "Adobe Acrobat 7.0 Professional"
       activate
       set PageCount to ""
       set Orgdoc to the front document
       set PageCount to page count
       
       tell application "System Events"
           tell process "Acrobat"
               click menu item "Number Pages..." of menu "Advanced" of menu bar item "Advanced" of menu bar 1
               delay 1
               tell group 1 of group 2 of group 1 of window "Page Numbering"
                   click radio button "All"
               end tell
               delay 0.5
               tell group 1 of window "Page Numbering"
                   click button 1
               end tell
           end tell
       end tell
       delay 0.5
       tell application "System Events"
           tell process "Acrobat"
               click menu item "Extract Pages..." of menu "Document" of menu bar item "Document" of menu bar 1
               delay 1
               keystroke tab
               keystroke (PageCount as string)
               delay 0.5
               click checkbox "Extract Pages As Separate Files" of group 1 of window "Extract Pages"
               click button "OK" of group 1 of window "Extract Pages"
           end tell
           set goahead to display dialog "Select Destination for output and Press OK once document is finished extracting." buttons {"OK", "Not OK"} default button 1
           set goahead2 to button returned of goahead
           if goahead2 is "OK" then
               close Orgdoc saving no
           end if
       end tell
    end tell
    tell me
       activate
    end tell
    set temp to display dialog "Would you like to process another PDF?" buttons {"No", "Yes"} default button 2
    set pressed to button returned of temp
    --END REPEATED BLOCK
    repeat until pressed is not "Yes"
       tell application "Adobe Acrobat 7.0 Professional"
           activate
           choose file with prompt "Select PDF to Covert to Single Page"
           copy the result to Orgdoc
           open Orgdoc
       end tell
       
       tell application "Finder"
           activate
           set Orgdocname to name of Orgdoc as string
       end tell
       
       tell application "Adobe Acrobat 7.0 Professional"
           activate
           set PageCount to ""
           set Orgdoc to the front document
           set PageCount to page count
           
           tell application "System Events"
               tell process "Acrobat"
                   click menu item "Number Pages..." of menu "Advanced" of menu bar item "Advanced" of menu bar 1
                   delay 1
                   tell group 1 of group 2 of group 1 of window "Page Numbering"
                       click radio button "All"
                   end tell
                   delay 0.5
                   tell group 1 of window "Page Numbering"
                       click button 1
                   end tell
               end tell
           end tell
           delay 0.5
           tell application "System Events"
               tell process "Acrobat"
                   click menu item "Extract Pages..." of menu "Document" of menu bar item "Document" of menu bar 1
                   delay 1
                   keystroke tab
                   keystroke (PageCount as string)
                   delay 0.5
                   click checkbox "Extract Pages As Separate Files" of group 1 of window "Extract Pages"
                   click button "OK" of group 1 of window "Extract Pages"
               end tell
               set goahead to display dialog "Select Destination for output and Press OK once document is finished extracting." buttons {"OK", "Not OK"} default button 1
               set goahead2 to button returned of goahead
               if goahead2 is "OK" then
                   close Orgdoc saving no
               end if
           end tell
       end tell
       
       
       tell me
           activate
       end tell
       
       set temp to display dialog "Would you like to process another PDF?" buttons {"No", "Yes"} default button 2
       set pressed to button returned of temp
       
    end repeat
    set rename to display dialog "Would you like to sequentially rename files?" buttons {"OK", "Not OK"} default button 1
    set rename2 to button returned of rename
    if rename2 is "OK" then
       
       tell application "Finder"
           --choose folder
           choose folder with prompt "Select Folder for sequential content renaming"
           copy the result to Orgfold
       --How could I add a leading zero to files numbered less than 10?    
           -- get the current folder listing
           set sourceFiles to every file of Orgfold
           -- start off with file 1
           set fileCounter to 1
           --loop through the files
           repeat with aFile in sourceFiles
               --renaming each one in turn
               set name of aFile to fileCounter as string
               -- and incrementing the counter after each file
               set fileCounter to fileCounter + 1
           end repeat
       end tell
       
    end if

This is a long script, so I’ll nibble at it. If you’re worried about your users having the right file, something like this works. The idea is that if the Finder can’t find the file in /Applications/ it gets the customer to find it.

set appsPath to path to applications folder from local domain as text
set appName to "ABC"
tell application "Finder"
	try
		open alias (appsPath & appName)
	on error
		set resp to button returned of (display dialog appName & " not found, please find it")
		if resp = "OK" then set f to choose file -- you can choose application too, but it's sloooow
		open f
	end try
end tell

Thanks for “nibbling” at it, Adam. I really appreciate it. Like I said, I’m very new to this.

I think that I understand what the snippet that you posted is going to do for the script, but some of it is a little confusing to me. I will comment your block…

Please excuse my ignorance… Not only is applescript new to me, but I don’t think that I’ve tried to learn anything new in three years or better (excluding the past couple of weeks)…

What you’re shooting for is a general purpose pointer to someone’s Adobe Acrobat Pro.


--I'm not on a domain; is this a problem? Also, is it an issue that the application is in a subdirectory of /Applications/?

(* local domain is to distinguish the Applications Folder at the top level (just below the volume name) from the default Users Applications Folder under your user name. You don't need it, "path to applications folder as text" will do. If you know the folder that will hold the app you're targetting, "Adobe Acrobat Pro" say, then:*)

set appsPath to (path to applications folder as text) & "Adobe Acrobat Pro:" -- note the colon

(* This resolves to yourHDname:Applications:Adobe Acrobat Pro: so when you concatinate "Acrobatlaunch" you'll have HDname:Applications: Adobe Acrobat Pro:Acrobatlaunch as the path the the app you want to open.*)

set appName to "the full name of the app as you would double click it"
-- right
tell application "Finder"
	try
		open alias (appsPath & appName) -- this 
	on error
		set resp to button returned of (display dialog appName & " not found, please find it")
		if resp = "OK" then set f to choose file -- you can choose application too, but it's sloooow
		open f
	end try
end tell

Since I don’t have Adobe Acrobat Pro, I can’t really comment much on that portion, except to say that instead of clicking on menu bars you should find out if AAP’s AppleScript dictionary contains commands for doing those things, and failing those, see if AAP has key equivalents for some or all of those menu picks. If so, then, supposing the menu pick you wanted was “command-shift-k” you’d do this inside your AAP tell block:

tell application "System Events" to keystroke "k" using {command down, shift down}

You can also experiment with leaving some of those “activate” calls - you only have to do that if you want the application in front. You can still tell it lots of stuff if it’s not.

Hey, you did a great job for a first script! Open the automater application and check out the split odd/even under PDF. There’s a drop-down menu there to change it so it will separate individual pages.

Applescript lets you send directions directly to scriptable apps like Acrobat. Open Acrobat’s dictionary in ScriptEditor and look at the Distill command. It’s usually easier than using UI scripting.

You can make your scripts function as applications where you drop a file or folder on the app and the script acts on the dropped files. That way you don’t need the open dialog. You’ll find examples of this by searching for “on open”.

There

Adam Bell–
Thank you soooo much! I just finished testing the new call for Acrobat that you suggested, and it works flawlessly on every machine on which I’ve run it. I haven’t had any luck replicating the results of the script using commands from the AA7P dictionary. There appears to be very limited support for applescript in there, and even less support for keyboard shortcuts iin the application itself. Nothing in Acrobat suggests that Adobe wanted to make things easier for anyone who uses it, but it’s what we had to have to keep up with the rest of the print production world, I guess. I’ll still be looking for ways to speed this script up and make it more autonomous, but you’ve been a great help. Thank you, again!

digest4d–
Thanks for the pat on the back. It’s always nice to know that effort put forth is appreciated. I toyed around with automator for a while before moving to applescript for this function, but the split odd/even never seemed to work right for me. It would process the files, but it would output some empty files and other files would have more than one even or odd files in them. Like I told Adam, the applescript dictionary for Acrobat doesn’t look like it supports what I’m wanting to accomplish, but I will definately play around with the “Distill” command and “on open” thingy. The more I play with this stuff, the more fun it becomes. Crazy, eh?

Anyways… I think that this script is in a decent enough shape that I’m going to go ahead and pass it out around here. If anyone else would like it in its present form, let me know. Without these forums it wouldn’t have made it this far.