I have a handler for this purpose, it depends on the OS version.
The handler returns the bundle identifier of the default browser
on default_Browser()
tell (system attribute "sysv") to set MacOS_version to it mod 4096 div 16
if MacOS_version is 5 then
set {a1, a2} to {1, 2}
else
set {a1, a2} to {2, 1}
end if
set pListpath to (path to preferences as Unicode text) & "com.apple.LaunchServices.plist"
tell application "System Events"
repeat with i in property list items of property list item 1 of contents of property list file pListpath
if value of property list item a2 of i is "http" then
return value of property list item a1 of i
end if
end repeat
return "com.apple.Safari"
end tell
end default_Browser
Here on my Mac (10.5.5) this information is stored in the «com.apple.LaunchServices» plist file. But retrieving this information can be a tedious task, so I wrote myself a little Obj-C tool that displays the default browser on the command line. The tool uses the Launch Services of Mac OS X and the (most simple) source can be studied here. If you want to use it in your own scripts, then download the utility, unzip it and call/use it like shown below:
set toolpath to ((path to desktop) as Unicode text) & "defbrowser"
-- set toolpath to ((path to me) as Unicode text) & "Contents:Resources:defbrowser"
set output to do shell script (quoted form of POSIX path of toolpath)
tell application id output
activate
end tell
Thanks for pointing my attention to this problem, Bruce! I modified the utility, it now returns the default handler/app bundle for the «http» URL scheme.
Don’t forget that even when you’ve identified the default browser, there’s no guarantee that it’ll be able to understand the code in your ‘tell’ block. In fact, it’s unlikely that you’ll be able to compile the code anyway with a variable in the ‘tell’ line.
First, thanks all for your effort in your answer.
I forgot to say that I’m on AppleScript Studio, but I think that this doesen’t matter.
I need this to avoid a double handler (one for the default browser and one for the user choice), but seems that solutions are longer than the problem.
I’ll use [open location “myUrl”] for the default browser and [do shell script “open -a” & myBrowser & “myUrl”] for the user choice.
This work:
set myBrowser to "Camino" as text
tell application myBrowser
open location "http://www.example.com/"
end tell
Although this open browser window behind my application window, instead using [open location] or [do shell script] without tell statement, browser open in front of my application; this is behavior I need.
Just curious to know if [tell application myBrowser] could open window as frontmost one?
set myBrowser to "Camino" -- no coercion, a literal string IS text
tell application myBrowser
activate
open location "http://www.example.com/"
end tell
Today I read this handler by Christopher Masto via John Gruber:
on GetDefaultWebBrowser()
set _scpt to "perl -MMac::InternetConfig -le " & ¬
"'print +(GetICHelper \"http\")[1]'"
return do shell script _scpt
end GetDefaultWebBrowser
Then I can:
tell application GetDefaultWebBrowser()
activate
open location "http://www.example.com/"
end tell
This perl script works fine with 10.5 but I get this AppleScript Error running on 10.6.2:
Can't load '/System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/auto/MacPerl/MacPerl.bundle' for module MacPerl: /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/auto/MacPerl/MacPerl.bundle: no appropriate 64-bit architecture (see "man perl" for running in 32-bit mode) at /System/Library/Perl/5.10.0/darwin-thread-multi-2level/DynaLoader.pm line 207.
at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/Types.pm line 18
Compilation failed in require at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/Types.pm line 18.
BEGIN failed--compilation aborted at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/Types.pm line 18.
Compilation failed in require at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/InternetConfig.pm line 431.
BEGIN failed--compilation aborted at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/InternetConfig.pm line 431.
Compilation failed in require.
BEGIN failed--compilation aborted. (2)
Any Idea how to resolve?
I’m running it on a MacBook 2Ghz Core2Duo.
Thanks.