views:

504

answers:

2

I think what I need here is to know which magic command-line or OSA script program to run to start up a URL in an existing Firefox browser, if one is running, or to also start up Firefox if it isn't. On Mac.

I'm testing a Python program (Crunchy Python) which sets up a web server then uses Firefox for the front end. It starts the web application with the following:

try:
    client = webbrowser.get("firefox")
    client.open(url)
    return
except:
    try:
        client = webbrowser.get()
        client.open(url)
        return
    except:
        print('Please open %s in Firefox.' % url)

I have Safari on my Mac as the default, but I also have Firefox installed and running. The above code started the new URL (on localhost) in Safari. Crunchy does not work well in Safari. I want to see it in Firefox, since I do have Firefox. Under Python 2.5, 2.6, and 2.7 (from version control) I get this:

>>> import webbrowser
>>> webbrowser.get("firefox")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/webbrowser.py", line 46, in get
    raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser

Firefox is there. I tried using webbrowser.get("/Applications/Firefox.app/Contents/MacOS/firefox %s") which starts up a new instance of Firefox then complains that another instance of Firefox is already running.

I really would like webbrowser to open up the URL in an existing tab/window of Firefox, if it's already running, or in a new Firefox is not already running.

I looked at webbrowser.py and it looks like there is no 'firefox' support for MacOSX. That's okay, I can add that. But I don't know how to open the URL in Firefox in the way I want to.

Ideas? And for now, I can force Crunchy to give me the URL, which I can manually paste into Firefox.

A: 

Apple uses launch services to find applications. An application can be used by the open command - Apple developer man page for open

The python command you want is

client = webbrowser.get("open -a /Applications/Firefox.app %s")

Following Nicholas Riley 's comment

If Firefox is on the list of Applications then you can get away with open -a Firefox.app %s

Mark
You shouldn't hard-code the path to Firefox; it can be installed anywhere.
Nicholas Riley
"open -a /Applications/Firefox.app %s" doesn't work. Even from the command-line that opens a URL using Safari, my default browser. I dug around some and it seems to be a difference between OS X 10.4 (which I have) and 10.5. If I do "open -a Firefox %s" then it works. Another option (after looking around) is "open -b org.mozilla.firefox %s". Don't really know what a "bundle identifer" is, but various places uses that incantation. I'll use the -b version. Thanks for the clue!
Andrew Dalke
A bundle identifier is a unique identifier for applications on Mac OS X (partial replacement for the four-byte "creator code"). Launch Services indexes bundle identifiers and type information in order to handle application/document/URL opening; if you want to see what kind of information it indexes, run /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -dump.
Nicholas Riley
+1  A: 

You should use Launch Services to open the URL. You can do this with the LaunchServices module, or with Apple's open utility, or with my launch utility (here):

open is probably easiest:

% open -b org.mozilla.firefox http://www.stackoverflow.com/

(or, of course, the equivalent in Python with subprocess or similar) should do what you want.

Nicholas Riley
Really, Python's "webbrowser" should do that. If this is the right general answer then I'll contribute a patch to the main Python code, unless you want to be the one to do that?
Andrew Dalke
Go right ahead.
Nicholas Riley
I've added it to the Python bug tracker as issue 7192 (see http://bugs.python.org/issue7192 ). That includes a partial patch, but I don't think it's a full solution.
Andrew Dalke