Get user default browser

How can I get user default browser (the one set on Safari general preferences) as text?
I need something like:

I suppose is a value on some .plist file
Thanks in advance.

P.S. “open location” don’t solve my need.


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}
		set {a1, a2} to {2, 1}
	end if
	set pListpath to (path to preferences as Unicode text) & ""
	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 ""
	end tell
end default_Browser

Hi crgand,

Here on my Mac (10.5.5) this information is stored in the «» 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
end tell

Some people don’t open .html files in browsers. (I open them in TextMate myself.)

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 ""
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?

Thanks again.

Yes, just add activate

set myBrowser to "Camino" -- no coercion, a literal string IS text
tell application myBrowser
	open location ""
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()
	open location ""
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/ line 207.
 at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/ line 18
Compilation failed in require at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/ line 18.
BEGIN failed--compilation aborted at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/ line 18.
Compilation failed in require at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/ line 431.
BEGIN failed--compilation aborted at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/ 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.

Reading man perl I’ve find a solution adding before the perl scirp execution:

do shell script "defaults write Prefer-32-Bit -bool yes"

and after:

do shell script "defaults write Prefer-32-Bit -bool no"

Perhaps there is a more clean solution.

P.S. Running pert5.8.8 instead of perl give me same error.

Thanks, but this is part of an application that open urls on different browsers, one of it is the default one.

FWIW another approach to the 32 bit issue:

tell application id DefaultBrowserID()
end tell

on DefaultBrowserID()
	do shell script "export VERSIONER_PERL_PREFER_32_BIT=yes; " & ¬
		"perl -MMac::InternetConfig -le 'print +(substr((GetICHelper \"http\"), 0, 4))'"
end DefaultBrowserID