2008-11-09 44 views
18

我正在開發一個OSX項目,用戶可以從中選擇需要生成PDF的文檔集合(來自任何應用程序)。標準的Macintosh打印對話框有一個PDF按鈕,其中有許多與PDF相關的命令,包括「另存爲PDF ...」。但是,我需要生成PDF文件而無需用戶交互。我理想的是希望這可以用於任何類型的文檔。如何以編程方式從OSX上的任何文檔生成PDF?

這裏是我到目前爲止探索的選項:

  • Automator動作。有一個用於Automator的PDF庫,但它提供了處理PDF文件的操作,而不是生成它們。 Finder有一個打印任何文件的動作,但只能打印一個真實的打印機。
  • AppleScript。某些應用程序可以生成PDF文件(例如,如果您將'doc.pdf'中的doc保存到Pages中,它將生成PDF(但這僅適用於Pages) - 我需要支持任何類型的文檔) 。
  • 自定義的打印機。我可以創建一個虛擬打印機驅動程序,然後使用Automator的行動,但我不喜歡在打印列表中的一個額外的打印機混淆用戶的想法。

我希望有一些方法可以與活動應用程序交互,就好像用戶正在執行以下步驟一樣:

  1. Do Cmd-P(打開打印對話框)
  2. 點擊 「PDF」 按鈕
  3. 選擇 「另存爲PDF ...」(在菜單的第二項)
  4. 鍵入文件名在保存對話框
  5. 點擊 「保存」

如果這是最好的方法(是嗎?),那麼真正的問題是:我如何將UI事件發送到外部應用程序(擊鍵,鼠標事件,菜單選擇)?

更新:我只想澄清一點:我需要轉換爲PDF文件是由其他應用創建的文檔。例如,用戶可能會選擇Word文檔或Numbers電子表格或OmniGraffle繪圖或Web頁面。共同點是每個文檔都有一個關聯的應用程序,並且該應用程序知道如何打印它(並且OSX知道如何將打印輸出呈現爲PDF文件)。

因此,可可開發中心的樣本並沒有幫助,因爲它們是從我的應用程序生成PDF。

回答

6

我想你可以用applescript打開一個文檔,然後用applescript UI scripting來調用打印菜單。

例如:

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 
+0

謝謝!那正是我正在尋找的指針。 – 2008-11-10 09:51:06

+2

-1這似乎是一個好方法,如果你打開Safari瀏覽器中的一個文件,但我敢肯定,你不想正確的代碼爲每一種可能的應用 – mcgrailm 2010-12-13 15:19:50

0

我輸入了下面的Automator的援助代碼(記錄的動作,然後拖動具體行動出了「看我做」窗口,以獲得AppleScript的)。如果您想從Safari以外的應用程序中打印PDF,則可能需要運行相同的過程,並圍繞「打印」對話框調整此Applescript,因爲每個程序都可能具有不同的打印GUI。

# 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 
2

看看一個叫程序CUPS-PDF

這是OS X的虛擬打印機它執行「另存爲PDF」的方法做什麼時,通過除穿過每個打印作業的普通打印機打印它會產生一個pdf輸出。

安裝完成後,您可以使用lp命令創建shell或AppleScript。

例如,一旦虛擬打印機的設置,你可以打印的test.txt,並自動保存爲PDF。要做到這一點使用一個AppleScript你可以使用下面的代碼:

do shell script "lp -d CUPS_PDF test.txt" 

的CUPS-PDF應用中的所有輸出保存到/用戶/共享/ CUPS-PDF。我不確定您是否可以更改該路徑,但可以在腳本中檢索該文件並將其移動。

有幾個注意事項雖然。

首先,LP命令不能打印.doc文件。我認爲還有一些其他的第三方應用程序可以讓你做到這一點。

二,CUPS-PDF應用程序顯示在系統偏好設置的打印機窗格爲在它的名字,但CUPS連字符顯示隊列名稱爲具有下劃線。因此,在命令行上,您需要使用下劃線參考CUPS_PDF的CUPS隊列名稱。

即使你不覺得它是非常有用的,以通過LP命令 建立一個腳本,還是要涉及到GUI腳本然後有一個虛擬打印機應該節省一些步驟。

1

你可以使用杯子這樣

on open afile 
     set filename to name of (info for afile) 
     tell application "Finder" 
      set filepath to (container of (afile as alias)) as alias 
     end tell 
     set filepath to quoted form of POSIX path of filepath 
     set tid to AppleScript's text item delimiters 
     set AppleScript's text item delimiters to "." 
     set filename to text item 1 of filename 
     set AppleScript's text item delimiters to tid 
     set afile to quoted form of POSIX path of afile 
     do shell script "cupsfilter " & afile & " > " & filepath & filename & ".pdf" 
    end open 
1

我已經創建了在bash別名此:

convert2pdf() { 
    /System/Library/Printers/Libraries/convert -f "$1" -o "$2" -j "application/pdf" 
} 
相關問題