How to open a new Firefox window with Terminal
Solution 1:
You need to use AppleScript for this. The ideal solution would be to use a built-in function from Firefox, but it doesn't offer one – its AppleScript dictionary is very limited. So we have to emulate keyboard shortcuts.
Open up your ~/.bash_profile
and add the following shell function:
function firefox-window() {
/usr/bin/env osascript <<-EOF
tell application "System Events"
if (name of processes) contains "Firefox" then
tell application "Firefox" to activate
keystroke "n" using command down
else
tell application "Firefox" to activate
end if
end tell
EOF
}
This will call osascript
, which executes AppleScript commands, then activate Firefox, and then emulate a ⌘N keypress – but only if it's already running. If not, Firefox will just be opened, so you don't get two new windows. Also, you can exchange "n"
to "t"
to get new tabs, obviously.
Save the ~/.bash_profile
file and enter source ~/.bash_profile
to reload it. Then, just call the following function whenever you need a new Firefox window:
firefox-window
Of course, feel free to change the function's name.
If you want to be able to pass an URL argument from the command line, see this answer: How to open a new Firefox window with URL argument.
~/.bash_profile
is where all your custom functions should reside. If the file doesn't exist, you can just create it.
Shell functions are more powerful than aliases, as for example they allow you to use arguments too. You could theoretically pass the URL of the new window too, and then tell Firefox to open it with the OpenURL
or Get URL
command – but I haven't tried them.
Regarding the syntax used: The <<-EOF
is a here document, making it easier to pass multi-line input to osascript
. The input will be parsed until the EOF
marker appears again.
Solution 2:
A simpler, easier approach :
- First test on the terminal :
open -n /Applications/"Firefox Developer Edition".app
If that works, then go to ~/.bash_profile, and create a simple function:
## Open Firefox:s
function firefox() {
echo "Opening Firefox Browser ...";
open -n /Applications/"Firefox Developer Edition".app
}
Then write: source ~/.bash_profile
to activate this into the ENV on mac.