Как программно генерировать PDF из какого-либо документа о OSX?

Другой альтернативный способ:

let newObj = JSON.parse(JSON.stringify(oldObj))
18
задан Denis Hennessy 9 November 2008 в 21:57
поделиться

2 ответа

Я думаю, что Вы могли использовать 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
6
ответ дан 30 November 2019 в 09:30
поделиться

Я набрал приведенный ниже код с помощью Automator (записал действие, а затем перетащил конкретное действие из окна «Watch Me Do», чтобы получить Applescript). Если вы хотите распечатать PDF-файл из приложения, отличного от Safari, вам, возможно, придется выполнить тот же процесс и настроить этот Applescript вокруг диалогового окна «Печать», поскольку каждая программа может иметь свой графический интерфейс печати.

# 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
0
ответ дан 30 November 2019 в 09:30
поделиться