Launching a local file in the default browser

Ben Rubinstein benr_mc at cogapp.com
Fri Aug 15 11:16:01 EDT 2003


on 9/8/03 7:13 am, Richard Gaskin wrote

> Yep, the same ol' question:  how do I launch a local file in the default
> browser on Mac OS X?
> 
> And for future reference:  Where is the definitive answer archived?

Richard,

Probably too late because I've just been catching up with a bunch of mailing
list mail; but my answer to this is "it's a real pain".

The standard way as others have noted is to use AppleScript to ask the
Finder to open the URL.  This works for "http://" URLs, but for "file://"
URLs it tends to start looking at type and creator, and sending them to that
app instead.  You may be able to get by setting the filetype to "????????";
I dabbled with this, adding a preference to my app to do this, but although
it seemed to work in my first casual test on X, my first X user said it
didn't help him - in any case my app really wants to create files assigned
to BBEdit, but preview them in the browser, so I looked further.

As others have noted, the standard way to ask a browser to look at a URL
(local or remote) on MacOS has always been through the GURL ("openurl")
AppleEvent.  This was never mandated, nor has it been removed: it was only
ever a convention, but it was widely observed.  IE or Netscape (I think the
latter) introduced it, and all other browsers implemented it - except !?£$&
Safari, which of course is rapidly becoming the default browser on an awful
lot of MacOS X desktops.  This was the first bug I reported to Safari.
Safari instead introduces its own suite of AppleEvents - typically arrogant
Apple behaviour.

As if this wasn't bad enough, there is considerable disagreement about the
exact format of a file URL, because MacOS X paths start with a "/".  So
should it be
    "file:/Users/fred/blah.html"
Or
    "file:///Users/fred/blah.html"
or
    "file://Users/fred/blah.html"


The answer is it depends who you ask.  MetaTalk/Transcript like the first
form.  Safari (v51) and IE 5.2 want the second.  Opera and iCab want the
third.

So... essentially you need a routine which knows which browser it's going to
use.  For my app I've currently given the MacOS X users a special preference
in which they choose the browser, overriding their system-wide preference.
I haven't tried retrieving that system-wide preference as some have
indicated - it might work.   So my URL launching routine (for X) knows the
browser to talk to; and depending on the browser, adjusts the number of
slashes following "file:"; and then depending on the browser, either uses
the openurl event to get that file viewed, or a more complicated script if
the app is Safari.  Of course, all of this is fragile; if the browser isn't
one of the four I know about, I make a guess about how many slashes, and
trust it implements the openurl event.  And if you've chosen to name your
copy of iCab "Safari", the results will be disappointing.

If you don't care about the file type/creator, you may be able to get away
with nullifying those, and asking the Finder to open the URL - but you'll
still have to decide how many slashes to use.

Hope this helps (?!?)

 
  Ben Rubinstein               |  Email: benr_mc at cogapp.com
  Cognitive Applications Ltd   |  Phone: +44 (0)1273-821600
  http://www.cogapp.com        |  Fax  : +44 (0)1273-728866






More information about the metacard mailing list