由于 Firefox 不包含特定的 AppleScript 字典,例如Firefox.sdef file,它不被认为是 AppleScript scriptable 与 e.g. Google Chrome,虽然它会响应一些基本的命令,例如 activate、quit 等,但它需要 UI 脚本 做你想做的事。
以下示例 AppleScript 代码需要Firefox 87或更高版本,并设置其accessibility.force_disabled 偏好到:-1
这是一个示例,说明我将如何指示 Firefox 打开一个新的窗口 到给定的 URL并等待页面完成加载。
示例 AppleScript 代码:
set theURL to "https://news.google.com/"
tell application "Firefox" to activate
delay 0.5 -- # Value may need to be adjusted if Firefox is closed.
my clickApplicationMenuCommand("Firefox", "File", "New Window")
delay 0.5
my setURLofFirefoxFrontWindowTo(theURL)
my waitForFirefoxPageToFinishLoading()
say "foobar"
-- # Handler(s) #
to clickApplicationMenuCommand(appName, appMenuName, appMenuCommand)
tell application appName to activate
delay 0.25
tell application "System Events" to ¬
click ¬
menu item appMenuCommand of ¬
menu appMenuName of ¬
menu bar item appMenuName of ¬
menu bar 1 of ¬
application process appName
end clickApplicationMenuCommand
to setURLofFirefoxFrontWindowTo(theURL)
tell application "System Events"
tell application process "Firefox"
set the value of UI element 1 of ¬
combo box 1 of toolbar "Navigation" of ¬
first group of front window to theURL
key code 36 -- # enter key
end tell
end tell
end setURLofFirefoxFrontWindowTo
to waitForFirefoxPageToFinishLoading()
-- # Requires Firefox version 87 or newer.
-- # Requires accessibility.force_disabled set to: -1
tell application "System Events"
tell application process "Firefox"
repeat until exists UI element "Reload" of ¬
toolbar "Navigation" of group 1 of window 1
delay 0.1
end repeat
repeat while (name of UI elements of ¬
toolbar "Navigation" of group 1 of ¬
window 1 whose description is "Reload") ¬
is not {"Reload"}
delay 0.1
end repeat
end tell
end tell
end waitForFirefoxPageToFinishLoading
注意事项:
示例 AppleScript 代码,如上所示,在 macOS Catalina 下的 Script Editor 中进行了测试 将 系统偏好设置 中的 语言和地区 设置设置为 英语(美国)- 主要 并且为我工作没有问题 1.
-
1 假设 系统偏好设置 > 安全和隐私 > 隐私中的必要和适当的设置已设置/根据需要解决。
使用 Firefox 91.0 版(64 位)测试。
请注意,UI 脚本 非常笨拙且容易失败,尤其是当 OS 和/或应用程序 的版本发生变化时,或者delay commands 的 值 是不够的。尽管如此,对于 Firefox,这里所描述的就是它所需要的。
注意:示例 AppleScript 代码就是这样,没有任何包含的错误处理 em> 不包含任何适当的附加错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide 中的try statement 和error statement。另请参阅Working with Errors。此外,在适当的情况下,可能需要在事件之间使用delay 命令,例如delay 0.5,延迟的值设置得当。