【问题标题】:AppleScript to save web content as PDF throws error : “Can't set window "Save" to …”将网页内容保存为 PDF 的 AppleScript 引发错误:“无法将窗口“保存”设置为……”
【发布时间】:2016-10-10 16:05:04
【问题描述】:

我正在尝试将我在 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
    

【问题讨论】:

    标签: applescript


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 2014-04-15
      • 2016-08-17
      • 2011-07-29
      • 2020-06-14
      • 1970-01-01
      相关资源
      最近更新 更多