4

I am running Firefox v10.0.1 with OS X Lion v10.7.3

From the Apple dock, you can right-click on Firefox icon and choose NEW and a new Firefox window will open.

From terminal, I have tried

    open -n /Applications/Firefox.app

but it says (assuming Firefox is already open)

A copy of Firefox is already open. Only one copy of Firefox can be open at a time.

How can you open a New Window in Firefox from the Terminal's command line?

1

4 Answers 4

5

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.

7
  • Ok, I will give this a try. Will this also work to start Firefox if it is not running, or is this only if it is already running?
    – jsherk
    Mar 3, 2012 at 23:13
  • Am receiving this when trying to reload: -bash: /Users/myname/.bash_profile: line 9: syntax error: unexpected end of file
    – jsherk
    Mar 3, 2012 at 23:17
  • This will also work if Firefox is not running, although it will then open two windows – it could be tweaked not to do this, of course. What's your complete bash_profile, can you post it somewhere (Pastebin, etc.)? I'm guessing there's a missing quote. Check if you typed everything correctly.
    – slhck
    Mar 3, 2012 at 23:22
  • Here you go: www.iwebss.com/test/bashprofile.txt
    – jsherk
    Mar 3, 2012 at 23:40
  • Sorry, it must have been an indentation error. You shouldn't indent EOF. See my updated post!
    – slhck
    Mar 3, 2012 at 23:42
2

A simpler, easier approach :

  1. 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.

1

I know this is an old post, I got here looking for an answer and then I figured I could try the old "invoke the app directly" approach...

So, to open a new Firefox window in Terminal, you can do:

/Applications/Firefox.app/Contents/MacOS/firefox --new-window

And if you want to open an URL in the new window:

/Applications/Firefox.app/Contents/MacOS/firefox --new-window URL

# For example:
/Applications/Firefox.app/Contents/MacOS/firefox --new-window https://superuser.com/questions/396434/how-to-open-a-new-firefox-window-with-terminal

There's even something cooler you can do if it's what you need. You can create an Automator app with the script inside and then you can double click that app to open a specific website in a new window.

To do that:

  1. Open Automator
  2. Create New (⌘N)
  3. Select Application
  4. In the search box above, type script
  5. Select Run Shell Script
  6. Paste the command from above with the URL you need to open in a new window
  7. Save the newly created app to your desired location.
  8. (Bonus) You can drag the app to the Dock and clicking it will run the script every time. I think you cannot delete the original file, if you do it might break the link.

The Automator window should look like this: Automator with script to open a url in new Firefox window

0

I think the matter is similar with opening a new tab from command line, so I would like to point out this thread where someone dealt with a similar issue using an AppleScript.

on firefoxRunning()
    tell application "System Events" to (name of processes) contains "Firefox"
end firefoxRunning

on run argv

    if (firefoxRunning() = false) then
        do shell script "open -a Firefox " & (item 1 of argv)
    else
        tell application "Firefox" to activate

        tell application "System Events"
            keystroke "t" using {command down}
            keystroke item 1 of argv & return
        end tell
    end if
end run

What's more on Mojave I get an error when executing the code from .bash_profile : System Events got an error: my cool script is not allowed to send keystrokes. (1002)

1
  • Since Mojave, applications that want to send keystrokes need to be give System Events permission, under the Security preferences > Privacy > Accessibility May 10, 2020 at 22:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .