0

我正在尝试将我在 Safari 中打开的 URL 的 Web 内容保存为 PDF。我能够通过 Automator 成功实现它,但我想要一个 AppleScript 来做到这一点。

这是我试图运行的脚本,它给我一个错误:

error "System Events got an error: Can’t set window \"Save\" to \"~/Desktop/Reports/\"." number -10006 from window "Save"

我做的步骤:

  1. 为另存为 PDF 创建了快捷方式...
  2. 打开有一些网页内容的 Safari
  3. 运行以下脚本:

    set destination_folder to "/Users/swatt/Desktop/Reports"
    tell application "System Events"
        keystroke "p" using {command down} -- activate print menu item
        delay myDelay
        keystroke "p" using {command down, option down, control down} -- to select the Save as PDF… option
        keystroke "g" using {shift down, command down} -- to select the folder location
    
        set value of text field 1 of sheet of window "Save" to destination_folder
    
        -- Now click the Go button
        click button "Go" of sheet of window "Save"
        delay myDelay
    
        -- Now that we are in our desired folder, set the file name and save
        set value of text field 1 of window "Save" to "TestResults.pdf"
    
        --click button "Save" of window "Save"
        click button "Go"
    end tell
    
4

1 回答 1

1

键盘快捷键适用于最前面的应用程序,但对于 UI 元素(窗口文本字段等),您必须指定进程

tell application "System Events"
    tell application process "Safari"
        keystroke "p" using {command down} -- activate print menu item
        --  other lines
        --
    end tell
end tell
于 2012-06-09T14:09:14.033 回答