How to open a new Firefox window with URL argument
I am trying to upgrade the existing answer
How to open a new Firefox window with Terminal
to accept a URL as an argument and open a NEW Firefox window.
I can use
open -a Firefox 'http://localhost:3000'
but it opens in a tab and not a NEW window as desired.
One variation is
open -n -a Firefox 'http://localhost:3000'
which gives me the standard error
Close Firefox, A copy of Firefox is already open. Only one copy of Firefox can be open at a time
BUT it opens the URL in my default browser Safari.
I have tried various options based on the man page for open and on the Mozilla site for opening URLs with their products but they say that the info is deprecated and may not work. It does not for me e.g.
/Applications/Firefox.app/Contents/MacOS/firefox -new-window "http://localhost:3000"
/Applications/Firefox.app/Contents/MacOS/firefox -remote "openURL(http://localhost:3000, new-window)"
both fail with the common error above.
I tried to modify the Script in the earlier link. I updated it to the following.
# A function to be able to open up a new Firefox window if firefox is already
# running.
function firefox-window() {
/usr/bin/env osascript <<-EOF
on run argv
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
return "I am trying to open " & item 1 of argv & " in a new Firefox window."
(*
if & item 1 of argv &
return "I am the if you seek"
tell application "Firefox" OpenURL & item 1 of argv &
end if
*)
end tell
end run
EOF
}
I get the error:
execution error: Can’t make item 1 of {} into type Unicode text. (-1700)
and I am stuck there. So I am getting a problem getting the URL into the applescript as an argument.
Well I get a host of other errors but the original script runs despite the ones before the UNICODE error.
~$ firefox-window 'http://localhost:3000'
2012-06-10 16:13:30.258 osascript[789:60f] Error loading /Library/ScriptingAdditions/Adobe Unit
Types.osax/Contents/MacOS/Adobe Unit Types: dlopen(/Library/ScriptingAdditions/Adobe Unit
Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found. Did find:
/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no
matching architecture in universal wrapper
osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit
Types.osax" declares no loadable handlers.
294:300: execution error: Can’t make item 1 of {} into type Unicode text. (-1700)
I was hoping that this could be simple but it has me banging my head on the wall. I am mystified that it will not work with a simple open command so I can do it in a shell script.
Any help would be appreciated on getting Firefox to open a NEW window while passing a URL to it.
Solution 1:
Note: The following works up to OS X 10.7 but not in 10.8 – see this question.
Open up AppleScript Editor.app and paste the following:
on run argv
tell application "System Events"
if (name of processes) contains "Firefox" then
tell application "Firefox" to activate
keystroke "n" using command down
delay 0.1 -- UI scripting delay
else
tell application "Firefox" to activate
delay 0.3 -- more delay
end if
keystroke "l" using command down
keystroke item 1 of argv
keystroke return
end tell
end run
Here, we'll either open a new Window with Cmd-N, or just activate Firefox. I included a custom delay — this is necessary since UI actions have an inherent delay and the script would then type away even though the window wasn't ready yet.
Finally, we'll keystroke
the first argument, which is item 1 of argv
.
Save this file under firefox-window.scpt
, e.g. in your home folder. Then modify the shell function in ~/.bash_profile
:
function firefox-window() {
osascript ~/firefox-window.scpt "$1"
}
This will pass the first command line argument $1
to the AppleScript, to be accessed as item 1 of argv
.
Save the .bash_profile
and don't forget to restart your terminal or enter source ~/.bash_profile
. Then, simply run the command with:
firefox-window apple.com
firefox-window google.com
… et cetera.