AppleScript将Web内容另存为PDF会引发错误:“无法设置窗口”保存“为...”

时间:2012-06-08 16:05:39

标签: applescript

我正在尝试保存我在Safari中以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
    

1 个答案:

答案 0 :(得分: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