我工作上的一个项目OS x这里的用户可以选择一个收集的文件(从任何应用程序),我需要产生PDF。标准Macintosh打印对话具有PDF按钮其中有一些PDF命令有关的包括"保存PDF...".然而,我需要产生的PDF文件,而不需要用户相互作用。我想要这工作与任何类型的文档。

这里的选择,我已经探讨了到目前为止:

  • 自动化器的行动。有一个PDF图书馆自动化器,但它提供了行动的工作PDF文件,不能产生他们。有一个搜索行动的任何印刷文件,但只有一个真正的打印机。
  • AppleScript.有些应用有能力产生的PDF文件(例如,如果发送保存文档在"试验。pdf"'页它将产生一个PDF(但这仅适用于页-我需要支持任何类型的文件)。
  • 定打印机。我可以创建一个虚拟的打印机驱动,然后用自动化器的行动,但我不喜欢这个想法混淆用户的一个额外的打印机打印清单。

我的希望是,还有一些方式交互活动的应用程序,如果是用户进行下列步骤:

  1. 做Cmd-P(打开打印对话)
  2. 点击"PDF"的按钮
  3. 选择"存PDF..."(第二个项目在菜单)
  4. 类型的文件以节省对话
  5. 点击"保存"

如果这是最好的方法(?) 然后真正的问题是:我怎么送UI事件的外部应用程序(键鼠的事件,菜单选择)?

更新: 只是为了澄清一点:文件我需要转换成PDF文件,通过创建 其他应用程序.例如,用户可能选择一个字文件或一个号码的电子表格或一个OmniGraffle绘图或一个网页。其共同点是,每个文件有一个相关应用程序和应用知道如何打印出来(并OS x知道怎么渲染的打印输出到一份PDF文件)。

因此,这样可可开发中心没有帮助,因为它们对产生一个PDF从 我的申请.

有帮助吗?

解决方案

我认为你可以使用applescript打开文件和随后的使用 applescript UI脚本 援引的印刷单。

例如:

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

其他提示

看看一个节目叫 杯子-PDF

它是一个虚拟的打印机for OS X这不是什么"保存PDF"方法并在印通过正常的打印机,除了每一个印刷作业通过它的结果在一个pdf格式的输出。

一旦它安装后你们可以创造外壳或AppleScripts使用 lp 命令。

例如,一旦虚拟打印机的安装你能打印test.txt 和它的自动保存作为一个pdf。要做到这一使用AppleScript你将使用下列代码:

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

杯子-PDF应用程序,将所有输出/用户/Shared/杯-PDF。我不知道如果你可以改变这条道路,但你可以检索的文件在您脚本和移动。

有几点需要注意,虽然。

第一, lp 命令不能打印。doc文件。我认为还有一些其他第三方应用程序,这将允许你这样做虽然。

第二,杯子-PDF应用程序表示打印机窗格中的系统偏好作为具有连字符的名称,但杯表示队列名的具有下划线。因此,在命令你需要参考杯的队列名其是CUPS_PDF带有下划线。

甚至如果你找不到它非常有助于建立一个脚本通过 lp 命令 仍然要涉及GUI脚本后来有一个虚拟的打印机应该为你节省一些步骤。

你可以使用杯喜欢这个

    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

我已经创建了一个别名在庆典:

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

我输入代码下面的援助的自动化器(记录的行动,然后拖着的具体行动的"看我做的"窗口,以便获得Applescript).如果你想打印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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top