[Pythonmac-SIG] Webbrowser module?

Bob Ippolito bob@redivi.com
Fri, 11 Jan 2002 07:25:20 -0500


On Friday, January 11, 2002, at 04:27 AM, Jack Jansen wrote:

>
> Recently, Steven Burr <sburr@home.com> said:
>> On Thursday, January 10, 2002, at 02:57 PM, Jack Jansen wrote:
>>
>>>
>>> Recently, "Schollnick, Benjamin" <Benjamin.Schollnick@usa.xerox.com>
>>> said:
>>>> I just sat down and attempted to use the webbrowser module
>>>> on my Mac... And failed...
>>>>
>>>> I've got the internet control panel configured right...
>>>> (Pointing to the right browser)...
>>>>
>>>> But
>>>>
>>>> webbrowser.open ("http://www.yahoo.com")
>>>>
>>>> returns a trace & english text of "cannot locate runnable browser".
>>>
>>> MacPython or MachoPython? Which version? Which OS? Could you send the
>>> stacktrace?
>>
>> I suspect Benjamin is referring to webbrowser on MachoPython.  The
>> InternetConfig class doesn't work for MachoPython.  When I tried using
>> webbrowser from python running in a Terminal, it opened the url in
>> lynx.  If I hadn't had lynx installed, I'm certain I would have gotten
>> the "cannot locate runnable browser" message as well.
>
> I have a bugfix request for myself in sourceforge to enable the use of
> Internet Config in MachoPython, but I haven't started on implementing
> it yet. The IC module is available under MachoPython, but I think it
> would also require LaunchServices, which isn't available yet.
>
>> I have actually been trying to come up with a patch to webbrowser that
>> would allow it to open a url in an Aqua browser while running in
>> MachoPython and finally found something that works.  It's a very simple
>> patch that uses the osascript command (i.e. AppleScript from the 
>> command
>> line) to open the url in Internet Explorer.
>
> This sounds like a nice stopgap. Could you try adding the IC stuff so
> that it really opens the users' default browser? This shouldn't be too
> difficult, mainly combining stuff from the classic MacPython
> webbrowser code with your osascript code.
>
> One thing, though: what will happen if you run this when you don't
> have access to the window server? I.e. when you're on Mac OS X Server,
> or when you're in an ssh session to an OSX machine where you ar enot
> logged in on the console? Doing a Carbon call in such circumstances
> will crash Python, and that probably isn't acceptable...
>

Cocoa has a message you can send to an instance of NSWorkspace called 
openURL: that accepts objects of type NSURL.  The code would look like 
this:

void openURL(char *theURL) {
   NSString *urlstring;
   NSURL *url;
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   urlstring = [NSString stringWithCString: theURL];
   url = [NSURL URLWithString:urlstring];
   [[NSWorkspace sharedWorkspace] openURL: url];
   [pool release];
}

Even if +[NSWorkspace sharedWorkspace] fails, sending a message to a nil 
object doesn't crash anything.  Worst case it'll complain in the console 
log.

'course this means having some extra #includes and perhaps linking to 
more frameworks but you're doing this anyways if you're using carbon.