質問

私は、ユーザーがPDFを生成する必要があるドキュメントのコレクションを(任意のアプリケーションから)選択できるOSXのプロジェクトに取り組んでいます。標準のMacintoshの[印刷]ダイアログには、[PDFとして保存...]などのPDF関連のコマンドが多数あるPDFボタンがあります。ただし、ユーザーの操作を必要とせずにPDFファイルを生成する必要があります。理想的には、これがあらゆるタイプのドキュメントで機能することを望みます。

これまでに検討したオプションは次のとおりです。

  • Automatorアクション。 AutomatorにはPDFライブラリがありますが、PDFファイルを生成するのではなく、PDFファイルを操作するためのアクションを提供します。実際のプリンターにのみファイルを印刷するためのFinderアクションがあります。
  • AppleScript。一部のアプリケーションには、PDFファイルを生成する機能があります(たとえば、「" test.pdf"にdocを保存」をPagesに送信すると、PDFが生成されます(ただし、これはPagesでのみ機能します-あらゆる種類の文書のサポートが必要です) )。
  • カスタムプリンター。仮想プリンタードライバーを作成してからオートマトンアクションを使用することもできますが、ユーザーを印刷リストの余分なプリンターと混同するという考えは好きではありません。

ユーザーが次の手順を実行しているかのように、アクティブなアプリケーションとやり取りする方法があることを望みます。

  1. Do Cmd-P(印刷ダイアログを開く)
  2. 「PDF」をクリックしますボタン
  3. 「PDFとして保存...」を選択します。 (メニューの2番目の項目)
  4. 保存ダイアログでファイル名を入力
  5. 「保存」をクリックします

それが最善のアプローチである場合(本当ですか?)、実際の問題は次のとおりです。UIイベントを外部アプリケーション(キーストローク、マウスイベント、メニュー選択)に送信するにはどうすればよいですか?

更新: 1つのポイントを明確にするために、PDFに変換する必要があるドキュメントは、他のアプリケーションによって作成されたドキュメントです。たとえば、ユーザーはWord文書、Numbersスプレッドシート、OmniGraffleの図面、またはWebページを選択できます。共通点は、これらのドキュメントのそれぞれに関連するアプリケーションがあり、そのアプリケーションがそれを印刷する方法を知っていることです(そしてOSXはPDFファイルに印刷出力をレンダリングする方法を知っています)。

したがって、Cocoa Dev Centralのサンプルは、私のアプリケーションから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

他のヒント

CUPS-PDFというプログラムをご覧ください

「PDFとして保存」という機能を実行するOS X用の仮想プリンターです。メソッドは、通常のプリンターを介して印刷するときに、それを通過するすべての印刷ジョブがPDF出力になることを除きます。

一度インストールすると、 lp コマンドを使用してシェルまたはAppleScriptを作成できます。

たとえば、仮想プリンターをセットアップしたら、test.txtを印刷して、pdfとして自動的に保存できます。 AppleScriptを使用してこれを行うには、次のコードを使用します。

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

CUPS-PDFアプリは、すべての出力を/ Users / Shared / CUPS-PDFに保存します。そのパスを変更できるかどうかはわかりませんが、スクリプトでファイルを取得して移動できます。

ただし、いくつかの注意事項があります。

最初に、 lp コマンドは.docファイルを印刷できません。ただし、これを行うことができるサードパーティのアプリは他にもあると思います。

2番目に、CUPS-PDFアプリはシステム環境設定のプリンターペインに名前にハイフンが含まれていると表示されますが、CUPSはキュー名にアンダースコアが付いていると表示されます。そのため、コマンドラインでは、アンダースコア付きのCUPS_PDFであるCUPSキュー名を参照する必要があります。

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

このためにbashでエイリアスを作成しました:

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

Automatorの助けを借りて以下のコードを入力しました(アクションを記録し、「Watch Me Do」ウィンドウから特定のアクションをドラッグしてApplescriptを取得します)。 Safari以外のアプリケーションからPDFを印刷する場合、各プログラムには異なる印刷GUIがある可能性があるため、同じプロセスを実行し、印刷ダイアログでこの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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top