Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
Will look into that later. Here's how to get rid of the Adobe Unit Types error: Unit type conversion error | Mac OS X 10.6 –  slhck Jun 11 '12 at 18:37
    
Please read a few tutorials on AppleScript though. tell application "Firefox" OpenURL & item 1 of argv & is completely wrong syntax. You'd have to write tell application Firefox to, i.e. don't forget to tell it to do something. Furthermore, you can use the & only for concatenating strings, which isn't even necessary here. So in your script just use item 1 of argv and not & item 1 of argv &. –  slhck Jun 11 '12 at 19:27

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

share|improve this answer
    
If the App can take the URL as Argument like Firefox/Chrome/Safari it goes even just enter :: open -a Safari www.google.com –  konqui Jul 22 at 5:40
    
@Konqui but that will open a new tab if the window is already open, not a new window as requested by the OP. –  slhck Jul 22 at 5:53
    
thats true, didn't see that he want's to open it in a new window, on firefox according to this site developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options there should be a -new-tab and -new-window Option to set. –  konqui Jul 22 at 6:01
    
@konqui Good point, perhaps this wasn't possible back then or I must've missed it. –  slhck Jul 22 at 6:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.