views:

1331

answers:

4

I'm working on a project for OSX where the user can pick a collection of documents (from any application) which I need to generate PDF's from. The standard Macintosh Print dialog has a PDF button which has a number of PDF-related commands including "Save as PDF...". However, I need to generate the PDF file without requiring user interactions. I ideally want this to work with any type of document.

Here's the options I've explored so far:

  • Automator actions. There's a PDF library for Automator but it provides actions for working with PDF files, not generating them. There's a Finder action for printing any file but only to a real printer.
  • AppleScript. Some applications have the ability to generate PDF files (for instance, if you send 'save doc in "test.pdf"' to Pages it will generate a PDF (but this only works for Pages - I need support for any type of document).
  • Custom Printer. I could create a virtual printer driver and then use the automator action but I don't like the idea of confusing the user with an extra printer in the print list.

My hope is that there's some way to interact with the active application as if the user was carrying out the following steps:

  1. Do Cmd-P (opens the print dialog)
  2. Click the "PDF" button
  3. Select "Save as PDF..." (second item in menu)
  4. Type in filename in save dialog
  5. Click "Save"

If that's the best approach (is it?) then the real problem is: how do I send UI Events to an external application (keystrokes, mouse events, menu selections) ?

Update: Just to clarify one point: the documents I need to convert to PDF are documents that are created by other applications. For example, the user might pick a Word document or a Numbers spreadsheet or an OmniGraffle drawing or a Web Page. The common denominator is that each of these documents has an associated application and that application knows how to print it (and OSX knows how to render print output to a PDF file).

So, the samples at Cocoa Dev Central don't help because they're about generating a PDF from my application.

+2  A: 

The samples available at Cocoa Dev Central should get you started relatively quickly. The implementation provided there is geared more towards an "Export as PDF"-ish option than an actual "printer".

wisequark
+5  A: 

I think you could use applescript to open a document and then use applescript UI scripting to invoke print menu.

For example :

tell application "System Events"
     tell window of process "Safari"
      set foremost to true
      keystroke "p" using {command down}
      delay 3
      click menu button "PDF" of sheet 2
      click menu item "Save as PDF…" of menu 1 of menu button "PDF" of sheet 2
      keystroke "my_test.file"
      keystroke return
      delay 10
     end tell

    end tell
mtim
Thanks! That was exactly the pointer I was looking for.
Denis Hennessy
A: 

Funny, that doesn't work at all for me from Script Editor. This script just seems to print the Script Editor foremost window. It doesn't activate Safari at all and doesn't click the "Save as PDF..." button either.

A: 

Couldn't get the code by mtim to work.

I typed up the code below with the assistance of Automator (recording an action, and then dragging the specific action out of the "Watch Me Do" window in order to get the Applescript). If you want to print a PDF from an application other than Safari, you might have to run through the same process and tweak this Applescript around the Print dialogue, since each program might have a different Print GUI.

Hope this helps!

# Convert the current Safari window to a PDF
# by Sebastain Gallese

# props to the following for helping me get frontmost window
# http://stackoverflow.com/questions/480866/get-the-title-of-the-current-active-window-            document-in-mac-os-x

global window_name

# This script works with Safari, you might have
# to tweak it to work with other applications 
set myApplication to "Safari"

# You can name the PDF whatever you want
# Just make sure to delete it or move it or rename it
# Before running the script again
set myPDFName to "mynewpdfile"

tell application myApplication
    activate
    if the (count of windows) is not 0 then
        set window_name to name of front window
    end if
end tell

set timeoutSeconds to 2.0
set uiScript to "keystroke \"p\" using command down"
my doWithTimeout(uiScript, timeoutSeconds)
set uiScript to "click menu button \"PDF\" of sheet 1 of window \"" & window_name & "\" of application process \"" & myApplication & "\""
my doWithTimeout(uiScript, timeoutSeconds)
set uiScript to "click menu item 2 of menu 1 of menu button \"PDF\" of sheet 1 of window \"" & window_name & "\" of application process \"" & myApplication & "\""
my doWithTimeout(uiScript, timeoutSeconds)
set uiScript to "keystroke \"" & myPDFName & "\""
my doWithTimeout(uiScript, timeoutSeconds)
set uiScript to "keystroke return"
my doWithTimeout(uiScript, timeoutSeconds)

on doWithTimeout(uiScript, timeoutSeconds)
    set endDate to (current date) + timeoutSeconds
    repeat
        try
            run script "tell application \"System Events\"
" & uiScript & "
end tell"
            exit repeat
        on error errorMessage
            if ((current date) > endDate) then
                error "Can not " & uiScript
            end if
        end try
    end repeat
end doWithTimeout
Sebastian Gallese