AppleScript - 如何在没有标题的弹出按钮中选择菜单项?

时间:2015-07-17 03:35:35

标签: firefox applescript

AppleScript相对较新......

我正在尝试创建AppleScript以自动化Firefox中的文件/保存页面...操作。具体来说,我需要选择"网页,完成"从另存为...对话框而不是默认"所有文件"选择对话框底部的弹出按钮。 (我特意使用Firefox和此选项,因为我想在运行一些JavaScript代码后保存当前的html内容 - 以解析后续处理的值。)

我已经能够通过使用以下方式选择弹出菜单(没有标题)来解决这个问题:

((pop up buttons of window "Save As") whose description is "All Files")

并通过发送击键" w"选择"网页,完成"在弹出菜单中。

我试图找到一种更强大的方法,而不是依赖于" w"选择我想要的菜单项。我试过了:

click menu item "Web Page, complete" of 
    ((pop up buttons of window "Save As") whose description is "All Files")

但那并没有奏效。在查看辅助功能检查器时,看起来弹出按钮(下拉列表)和菜单项之间有一个菜单,但我无法弄清楚如何引用它。

任何帮助将不胜感激。这是完整的脚本:

tell application "Firefox" to activate
delay 0.25

    tell application "System Events"

        tell process "Firefox"
            set frontmost to true

            click menu item "Save Page As…" of menu "File" of menu bar 1
            delay 0.25

            repeat until window "Save As" exists
                delay 0.5
            end repeat

            click ((pop up buttons of window "Save As") whose description is "All Files")
            delay 0.5

            -- This didn't work:
            click menu item "Web Page, complete" of ((pop up buttons of window "Save As") whose description is "All Files")

            -- This works but only because the first entry is "Web Page, complete"
            keystroke "w"
            keystroke return
            delay 0.5

            set outputfilename to "foo3.html" as text

            keystroke outputfilename
            keystroke return

            delay 0.5
      end tell
end tell

1 个答案:

答案 0 :(得分:1)

试试这个

activate application "Firefox"

tell application "System Events"

    tell process "Firefox"
        set frontmost to true
        click menu item "Save Page As…" of menu "File" of menu bar 1
        repeat until window "Save As" exists
            delay 0.2
        end repeat

        tell window "Save As"
            tell pop up button 1 of group 1
                if value is not "Web Page, complete" then
                    click
                    delay 0.5
                    pick menu item "Web Page, complete" of menu 1
                end if
            end tell

            set outputfilename to "foo3.html"
            keystroke outputfilename
            click button "Save"
        end tell
    end tell
end tell