Determining an operating system's default browser

J

John McMonagle

Is there a python module which can determine an operating system's
default web browser application.

I would like to use it in the following context: I have a GUI
application where all the documentation is written as html pages. I
wish to have these html help pages open in the default browser when
selected from the application's Help menu

If python can determine the path to the default browser, I can then just
spawn it.

Regards,

John McMonagle
 
L

Larry Bates

You don't have to determine it. Just os.startfile('page1.html')
and let the OS figure it out.

-Larry Bates
 
J

John McMonagle

You don't have to determine it. Just os.startfile('page1.html')
and let the OS figure it out.

Works great for Windows - not available on Unix though.
 
J

Juho Schultz

John said:
Is there a python module which can determine an operating system's
default web browser application.

I would like to use it in the following context: I have a GUI
application where all the documentation is written as html pages. I
wish to have these html help pages open in the default browser when
selected from the application's Help menu

If python can determine the path to the default browser, I can then just
spawn it.

Regards,

John McMonagle
I think the htmlview command is used for this purpose (displaying html
documentation of applications) on many Linuxes. So you could try
os.system('htmlview'). Given the variety of Unixes (and even Linuxes)
out there this is not a general solution.

So I would try htmlview first. If it does not work, the program could
ask the user for his favourite browser the 1st time he looks at docs,
and save this to a configuration file.
 
F

Fuzzyman

John said:
Is there a python module which can determine an operating system's
default web browser application.

I would like to use it in the following context: I have a GUI
application where all the documentation is written as html pages. I
wish to have these html help pages open in the default browser when
selected from the application's Help menu

If python can determine the path to the default browser, I can then just
spawn it.

The module webrowser module does this - and I use it for exactly this
purpose. :)

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
 
J

Jorgen Grahn

Take a look at the desktop module for similar functionality for KDE,
GNOME and the Mac OS X desktop environment (as well as Windows, of
course):

Note that those do not, of course, work on all Unices.

On my machines, there is One Correct Way of doing these things, and that's
to look in the MIME support/configuration files (~/.mailcap, and so on),
first for the user, then system-wide. Something there might tell you what
program should handle text/html content.

This mechanism is widespread under Unix, I think ... but I don't know if
there is a general interface to it, or a Python interface.

/Jorgen
 
P

Paul Boddie

[desktop module]
Note that those do not, of course, work on all Unices.

Correct: they work only for the stated desktop environments.
On my machines, there is One Correct Way of doing these things, and that's
to look in the MIME support/configuration files (~/.mailcap, and so on),
first for the user, then system-wide. Something there might tell you what
program should handle text/html content.

Indeed. Thanks for reminding me about mailcap/metamail - I used them in
a project about ten years ago, and I suppose not having any
opportunities to use them in the intervening period probably pushed
them to the back of my mind.
This mechanism is widespread under Unix, I think ... but I don't know if
there is a general interface to it, or a Python interface.

Here's an example:

import mailcap
command, entry = mailcap.findmatch(mailcap.getcaps(), "text/html")

Here's another more comprehensive set of examples:

http://effbot.org/librarybook/mailcap.htm

Sadly, when considering my KDE desktop's default browser, inquiring the
application for HTML files yields Mozilla Firefox from my mailcap file.
What I've discovered is that the issue of an URL-opening application is
often quite separate from an appropriate application to open a file of
a given type. Perhaps I should call desktop.open something like
desktop.urlopen instead, since that name arguably describes the purpose
of the function more accurately.

Paul
 
J

Jorgen Grahn

Jorgen Grahn wrote: ....

Indeed. Thanks for reminding me about mailcap/metamail - I used them in

And thanks for mentioning the real name of that mechanism!

....
import mailcap

I guess I shouldn't be surprised that there is a standard module
for that ... ;-)

....
Sadly, when considering my KDE desktop's default browser, inquiring the
application for HTML files yields Mozilla Firefox from my mailcap file.

Yes, but that's only because you haven't configured your mailcap, right?
Which might be a real problem -- most people don't touch their .mailcaps,
especially those who use KDE or Gnome.

Also, sometimes you want a GUI browser to open, and sometimes you want a
console-based one like lynx or w3m. I guess most people who use mailcap do
so because they use console-based mail readers, and thus want the HTML stuff
appear inside the same terminal. Another reason this might be unsuitable
for the OP.

So, while I called it "The One Correct Way" above, I admit it might not be a
very good way ;-)

/Jorgen
 
P

Paul Boddie

Jorgen said:
And thanks for mentioning the real name of that mechanism!

Happy memories! ;-)

[...]
I guess I shouldn't be surprised that there is a standard module
for that ... ;-)

It's another stop on the standard library mystery tour!

[...]
Yes, but that's only because you haven't configured your mailcap, right?
Which might be a real problem -- most people don't touch their .mailcaps,
especially those who use KDE or Gnome.

Going by the following discussion, they abandoned interoperability with
mailcap and went with their own cocktail of new inventions:

https://listman.redhat.com/archives/xdg-list/2003-April/msg00009.html

The above message is where the mailcap-related arguing begins. It boils
over here:

https://listman.redhat.com/archives/xdg-list/2003-April/msg00031.html
Also, sometimes you want a GUI browser to open, and sometimes you want a
console-based one like lynx or w3m. I guess most people who use mailcap do
so because they use console-based mail readers, and thus want the HTML stuff
appear inside the same terminal. Another reason this might be unsuitable
for the OP.

So, while I called it "The One Correct Way" above, I admit it might not be a
very good way ;-)

If there's one "correct" way, it would seem that the xdg people are
always interested in making at least one other "correct" way.
Nevertheless, in defence of the desktop module, its purpose is merely
to "open" things in the context of the current desktop: correct or
standards compliant file type identification doesn't necessarily enter
the picture. ;-)

Still, in the supported environments, desktop.open should do what the
user would expect, given the preference those environments typically
have for their "new and improved" way of doing things involving
configuration dialogues that the user might well have seen (and
probably closed very quickly in horror). Sadly, it doesn't seem to
involve mailcap files, however.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top