Application Preview to print as pdf

Hi everyone,
I am trying to write an applescript droplet that will allow me to print from system events as a pdf.

Sounds stupid. But here is my problem. The weekly delivery for me, comes in as a 3 page pdf document. I only need page one so I have found myself going over this over and over. I usually bring the pdf in Preview and select Print page 1 to 1 and save as a pdf in a certian folder.

The names of the pdf has to be
ED-05-today-2004.pdf

Any ideas how to do the printing from applescript as a pdf page 1 to 1 that has to be scaled down by preview?

Any help would be appreciated.

Steve

Hi,

If you’re running Jaguar, then you need ui scripting beta (System Events beta) for the following script example. With Panther it’s built in but you need to change the keystroke command’s ‘with’ to ‘using’ something, I forget. Anyway you’re scripting the Print window and there are several things to note. You may need to change items in the pop up buttons, you may have made changes that changes items in the print window (Apple extra stuff) like making the “Save as pdf” button a pop up, and the destination for your saves. The destination for your saves should be remembered by Preview (or system) depending on where you last saved something in Preview. Default for the Print window pop up button 1 should be “Copies and pages”. If this needs to be scripted then I remember doing it before, but can’t remember how. Anyway, here’s an example of simple scripting of the print window. Note that it’s a drawer and a ui element of the front window.

set this_item to (choose file)
set item_info to (info for this_item)
set item_ext to (name extension of item_info)
if (name extension of item_info is “pdf”) then
set item_name to (name of item_info)
set ext_offset to (offset of “.” in item_name)
set display_name to (text 1 thru (ext_offset - 1) of item_name)
set new_name to (display_name & " p1.pdf")
tell application “Preview”
activate
open this_item
end tell
tell application “System Events”
tell process “Preview”
keystroke “p” with command down
tell front window
tell window “Print”
tell UI element 1
tell UI element 3
tell UI element 1
tell UI element 6
click radio button “From:”
end tell
end tell
end tell
tell UI element 4
click button “Save As PDF…”
end tell
end tell
end tell
end tell
keystroke new_name
keystroke return
end tell
end tell
end if

You can easily change this to a droplet.

gl,

Kel,
Thanks so much for all your help. However, I am stuck. I keep on getting an error

I have tried to experiment in my free time, and never get anywhere.
Gets to the point of click radio button “From:” and errors

Is there a place I can go to to find our more about UI element numbers and how to tell what element is which number. :rolleyes:


tell UI element 1 
  tell UI element 3 
    tell UI element 1 
      tell UI element 6 
        click radio button "From:" 
     end tell 
    end tell 
end tell 
tell UI element 4 
  click button "Save As PDF…" 
end tell 

Thanks again!
Krazay

PS. I love this site!

Hi Krazay,

There are things that help you get the ui elements outline, but I don’t use them. So, maybe somebody else can help you there. I like to see things for myself, so I just use the Script Editor and the result window and go down the line through the ui elements until I get to the goal. For instance, I can start with this in the Script Editor:

set the_file to (choose file)
tell application “Preview”
activate
open the_file
end tell
tell application “System Events”
tell process “Preview”
keystroke “p” with command down
tell front window
UI elements
end tell
end tell
end tell

The result window in the Script Editor shows the ui elements of Preview’s front window. Here, the results show that window “Print” is an element of the front window. So, I add this to the script statements, comment out the unnecessaryparts, and get the ui elements of window “Print”:

–set the_file to (choose file)
tell application “Preview”
activate
–open the_file
end tell
tell application “System Events”
tell process “Preview”
–keystroke “p” with command down
tell front window
tell window “Print”
UI elements
end tell
end tell
end tell
end tell

Here, the result show:

{UI element 1 of window “Print” of window “birds.jpg” of application process “Preview” of application “System Events”}

so I add ui element 1 and get the ui elements of that. etc.

–set the_file to (choose file)
tell application “Preview”
activate
–open the_file
end tell
tell application “System Events”
tell process “Preview”
–keystroke “p” with command down
tell front window
tell window “Print”
tell UI element 1
UI elements
end tell
end tell
end tell
end tell
end tell

Here, I get a big list of elements of tthe print window. Then I just sift through all the elements by trial and error. Most of the list items can be eliminated and from previously playing with this I knew to jump to certain items. Fun, fun, fun.

gl,

Kel,
Thanks alot! Took a bit of testing but I see all the results come up after asking the front window to display the UI Elements. Another great thing about mac!

Have a great day!
Krazay :o

Kel,
One more thing… My next problem is when I am about to save the pdf file. How do I save the file into a specific folder. It always comes up with my last used default folder.
Any ideas? :slight_smile:

Krazay

Hi Krazay,

I don’t think this can be done in the save dialog. You probably need to set the folder before printing. If you didn’t need to print just the first page, then this could be done with PDF Workflow but this just saves or gives references to the whole pdf file.

gl,