macOS VenturaではPDFをOCR処理し、テキストを埋め込むことがOS標準で可能になりました。従来では通常、有料ソフトやスキャナのバンドルソフトが必要だったので、これって結構すごいことだと思っています。
- プレビュー Appのファイル > 書き出す…から可能
- 埋め込み済(とその他謎の理由)のファイルには「テキストを埋め込む」チェックボックスが表示されない
そうなってくると、以前スキャンした書類のPDFをすべて検索可能にしたくなってきます。しかしながら、そのようなコマンドラインまでは用意されていません。不本意ですが、AppleScriptでGUIを操作することにしました。
embed.applescript
on run argv set infile to item 1 of argv set temp to AppleScript's text item delimiters set AppleScript's text item delimiters to "." set outfile to text from text item 1 to text item -2 of infile & "_embedded.pdf" set AppleScript's text item delimiters to temp tell application "Preview" activate open infile as POSIX file end tell tell application "System Events" tell process "Preview" tell menu bar 1's menu bar item "ファイル"'s menu "ファイル" pick menu item "書き出す…" end tell tell window 1's sheet 1 if exists checkbox "テキストを埋め込む" then if not (value of checkbox "テキストを埋め込む" as boolean) then click checkbox "テキストを埋め込む" end if delay 1 keystroke "g" using {command down, shift down} delay 1 set value of sheet 1's text field 1 to outfile delay 1 keystroke return delay 1 click button "保存" else delay 1 key code 53 delay 1 end if end tell end tell end tell tell application "Preview" close window 1 quit end tell end run
このスクリプトをosascript embed.applescript hoge.pdf
で実行すればhoge.pdfと同ディレクトリにhoge_embedded.pdfという名で埋め込み済みのPDFが生成されます。これをxargs
でぶん回します。
(Adobe Scan以外で作成された、)テキストが埋め込まれていないファイルにのみ実行したいのでSpotlightのメタデータでフィルタすることにしました。
% mdfind -onlyin . 'kMDItemFSName == "*.pdf" && kMDItemCreator != "Adobe Scan*"' -0 | xargs -0 -n1 osascript embed.applescript
しばらく待てば、カレントディレクトリ下すべてのPDFファイルにテキストが埋め込まれるはずです。
- その間操作できないのはご愛嬌
- 重いファイルはタイムアウトするのもご容赦ください