如何在 OSX 中录制将网页保存为 PDF 的“宏”

如何在 OSX 中录制将网页保存为 PDF 的“宏”

我经常在 OSX 上通过 Chrome 将页面保存为 PDF。页面为 apple-P,然后单击 PDF 按钮和“另存为 PDF...”菜单项。我总是使用预填充的文件名并保存在默认目录中。

是否可以将其保存为自动脚本?如果可以的话,我肯定会将其作为按钮添加到 Chrome 的某个位置,这样我就可以“保存以供以后阅读”

谢谢你的帮助。

答案1

我只用 Safari 和 Chrome 测试过,但看到此主题适用于更复杂的脚本。

delay 0.3 -- time to release modifier keys if the script is run with a shortcut
tell application "System Events" to tell (process 1 where frontmost is true)
    if name is "Google Chrome" then
        keystroke "p" using {option down, command down}
        delay 0.1
        tell menu button 1 of window 1
            click
            click menu item "Save as PDF…" of menu 1
        end tell
        keystroke "l" using {option down, command down}
        keystroke return
    else
        keystroke "p" using command down
        tell menu button 1 of sheet 1 of window 1
            click
            click menu item "Save as PDF…" of menu 1
        end tell
        keystroke "l" using {option down, command down}
        keystroke return
    end if
end tell

您还可以使用韓國,但必须再次加载页面,并且不支持身份验证或屏蔽广告。

tell application "Google Chrome" to tell active tab of window 1
    set u to URL
    set t to title
end tell
do shell script "wkpdf --source " & quoted form of u & " --output ~/Desktop/" & quoted form of t & ".pdf"

如果您在系统偏好设置中将“另存为 PDF...”分配给 ⌘P,则只需按住命令键并按下 PP(或在 Chrome 中按下 ⌥⌘P 和 ⌘P)即可打开对话框。

答案2

使用 Automator 中的服务模板。服务接收没有输入Google Chrome 应用程序,工作流程为“实用工具”>“Watch Me Do”,然后执行打印操作,以便 Automator 可以捕获它。(Chrome 不提供任何 Automator 操作,但“Watch Me Do”仅需要 Universal Access。)

另外,你可以使用 AppleScript,但弄清楚如何引用事物可能会很“有趣”,而且我注意到它的字典没有引用标准打印设置类型,因此您可能无法控制打印对话框。

相关内容