Check default web browser

Im wondering if there is a way using applescript to check what your current default web browser is.
if anyone has an idea to help out that would be great

The reason for the silence here is that one normally sets the default browser in Safari and Safari doesn’t have an AppleScript Dictionary property for what it is. I think the answer is “No” unless someone knows where at the System level, Safari keeps that info - there doesn’t seem to be an obvious place.

Looks like it is stored in com.apple.LaunchServices.plist in your home preferences folder (under ‘LSHandlers’). I have no idea how to read it though.
(Note: I had to change the default value in Safari’s preferences for ‘Default Web Browser’ first before it showed up in com.apple.LaunchServices.plist)

Neat find. I can test for possible browsers this way:

set thePlist to open for access alias ((path to preferences folder as Unicode text) & "com.apple.LaunchServices.plist")
set b to words of (read thePlist)
close access thePlist
set Browsers to {"Camino.app", "Shiira.app", "BumperCar.app", "Firefox.app", "iCab.app", "Internet Explorer.app", "KidsBrowser.app", "Mozilla Thunderbird.app", "Netscape.app", "Opera.app", "WaMCom.app"} -- and add any other
repeat with Bsr in Browsers
	if Bsr is in b then beep 3
end repeat

and if none are found, then I suppose the browser is Safari.

This also seems to work:

try
	do shell script "defaults read com.apple.LaunchServices | grep 'http;' | cut -d \\\" -f 2"
	set defaultBrowser to result
on error
	set defaultBrowser to "com.apple.safari"
end try

--> "com.apple.safari", "org.mozilla.camino", etc.

Adam, you’re script doesn’t work for me. Also, would you mind changing your first line of code?

set thePlist to open for access alias ((path to preferences folder as Unicode text) & "com.apple.LaunchServices.plist")

Another thing, Adam: you’re not testing anything specific with your script. Thus, you might come up with multiple browsers in your script. Consider this:

set thePlist to open for access alias ((path to preferences folder as Unicode text) & "com.apple.LaunchServices.plist")
set b to words of (read thePlist)
close access thePlist
set Browsers to {"com.apple.itunes", "org.mozilla.camino", "com.apple.safari"} -- and add any other
repeat with Bsr in Browsers
	if Bsr is in b then beep 3
end repeat

[Note that I converted the preference file to xml (instead of binary, using plutil). I’m not sure if/how this affects anything.]

Assuming that iTunes is set to handle it internet radio and itms links, and that Safari is your default browser (which is the case for me), this script will beep six times.

Bruce;

Your version:

try
	do shell script "defaults read com.apple.LaunchServices | grep 'http;' | cut -d \\\" -f 2"
	set defaultBrowser to result
on error
	set defaultBrowser to "com.apple.safari"
end try

is clearly the way to go (but impossible for me to imagine because I a) didn’t know of “defaults read etc.” and it’s ability to make sense out of pref xml; b) have never used “cut”; and c) now see that your script is based on a very early entry in the pref file called public.html; (broken up because the bbs won’t accept the full word) LS & Handler & Role & All.

Actually, I was looking for “LSHandlerURLScheme = http;” (which appears near the end of my file). I did this because HTTP is what you actual use while browsing. It’s possible that someone could configure HTML files to open in an editor (BBEdit, etc.).

Also, FYI, using “cut”'s -d (delimiter) and -f options is just like using AS’s text item delimiters.

Thanks for all your help. the script works great :smiley:

Hi,

If anyone is still monitoring this thread, I pasted Bruce’s script into the Editor and ran it … but I didn’t get a value for defaultBrowser. Am I supposed to put this script inside a tell, or save as an app, or something?

Thanks,

Ted

Hmm. when that file doesn’t exist, defaults normally throws an error. However, when piping things together, those errors are sometimes ignored. (If anyone knows more about this, I’d like to know!)

Anyways, ditching the cut command and using AppleScript’s text item delimiters seems to work:

try
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"\""}
	
	do shell script "defaults read com.apple.LaunchServices | grep 'http;'"
	set defaultBrowser to text item 2 of result
on error
	set defaultBrowser to "com.apple.safari"
end try
set AppleScript's text item delimiters to ASTID

return defaultBrowser

Sorry, haven’t really been following this discussion - but here’s a sort of AppleScripty 2¢. (It may be worth noting that not all applications have bundle signatures or identifiers.)

set p to POSIX path of (path to preferences) & "com.apple.LaunchServices.plist"
tell application "System Events" to set x to value of property list item "LSBundleSignature" of property list item ¬
	1 of property list item "U:http" of property list item "LSPrefsBindings" of property list file p
tell application "Finder" to application file id x as alias