Note:
As of at least Firefox v50, you can make Firefox default to opening URLs in a new window, by unchecking Open new windows in a new tab instead
on the General
tab of Firefox's preferences.
Note, however, that this is a persistent setting that affects all URLs opened from outside of Firefox.
The solution below may still be of interest if you don't want to rely on the state of that setting.
(Unfortunately, due to Firefox's limited AppleScript support, there is no similarly robust solution for always opening in a tab, irrespective of the state of the setting).
Here's a more robust solution that:
doesn't rely on a fixed delay and
is language-neutral (as is the keystroke-sending approach); i.e., it also works with localized names for the menus and commands (e.g., "Datei" for "File", ...)
However, because GUI scripting is employed for programmatically clicking on a menu item, you do need to authorize the calling applicationfor assistive access first (a one-time action that requires administrative privileges) - e.g., if your script is run from a terminal, you must authorize Terminal.app
, but even Script Editor.app
must be authorized if you want to run your script while developing it.
tell application "Firefox"
# Make Firefox frontmost.
activate
# Wait until it is truly frontmost.
repeat while not frontmost
delay 0.1
end repeat
# Open a new window using GUI scripting (requires authorization for assistive access),
tell application "System Events" to tell application process "Firefox"
set windowCountBefore to (count of windows)
# Click on File > New to open a new window, but locate it
# by keyboard shortcut rather than by name, so as to also work
# with localized menu names.
tell menu 1 of menu bar item 3 of menu bar 1
click (first menu item whose value of attribute "AXMenuItemCmdChar" is "N" and value of attribute "AXMenuItemCmdModifiers" is 0)
end tell
# Wait for the new window to appear.
repeat while (count of windows) = windowCountBefore
delay 0.2
end repeat
end tell
# Finally, open the URL.
open location "http://example.org/"
end tell
Note:
Under normal circumstances, if Firefox is responsive, this should work reliably. However, the code could be improved by implementing timeouts for the waiting loops.
Firefox isn't the fastest at opening the new window, so Firefox will first activate, and the new window will only appear about a second or so later.
The new window will invariably have an empty first tab, and the URL will open in the 2nd tab.
A side note re the unusual-looking tell application "System Events" to tell application process "Firefox"
construct:
- The main block targets the Firefox application (
tell application "Firefox"
).
- However, GUI scripting must be done in the context of the
System Events
application (tell application "System Events"
), and, within that context, it is the Firefox process that must be targeted (tell application process "FireFox"
). While you could write two nested tell
blocks, they're combined into a single block here for convenience and brevity.