gurkensaas
gurkensaas

Reputation: 913

Detect default browser in Applescript

Since I changed my default web browser quite often in recent times, I was wondering if there was a way to specifically address your default browser. I could only find a 10 year old Stackoverflow-post that mentioned to do a similar thing with the current mail client but I didn't find anything about the default browser. I suppose there is a similar way of doing this with default browser, but I don't really understand the code of the accepted answer.

Upvotes: 3

Views: 1086

Answers (1)

user3439894
user3439894

Reputation: 7555

For some time now in macOS, the ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist file contains information about the default browser for URLs with HTTP and HTTPS protocols.

The following example AppleScript code parses that file for http; and https; and provides some, not all, logic to do what you want based on what's returned. As coded it favors both being set to the same value and if not, you'll need to add additional logic to suite that scenario.

set defaultBrowserList to paragraphs of (do shell script "defaults read \\
    ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure \\
    | awk -F'\"' '/http;|https;/{print window[(NR)-1]}{window[NR]=$2}'")

if (count defaultBrowserList) is not equal to 2 then return

if item 1 of defaultBrowserList is equal to item 2 of defaultBrowserList then
    set defaultBrowser to item 1 of defaultBrowserList
else
    return
end if

if defaultBrowser is "" or defaultBrowser contains "safari" then
    --  # The default Browser is Safari.
    set defaultBrowser to "Safari"
    --  # Your code goes here.
else if defaultBrowser contains "chrome" then
    --  # The default Browser is Google Chrome.
    set defaultBrowser to "Google Chrome"
    --  # Your code goes here.
else if defaultBrowser contains "firefox" then
    --  # The default Browser is Firefox.
    set defaultBrowser to "Firefox"
    --  # Your code goes here.
else
    set defaultBrowser to "Other"
    --  # Your code goes here.
end if

Notes:

The start of the if defaultBrowser statement tests for nothing "" as well as "safari", because, if Safari is the only Browser installed or if another Browser is installed and has never had a default Browser set, then by default nothing will be returned by the do shell script "defaults ..." command, and this means Safari is the default Browser.

In the example AppleScript code above, the value of the defaultBrowser variable initially gets set to one of the following values:

  • Nothing: ""
  • com.apple.safari
  • com.google.chrome
  • org.mozilla.firefox
  • Some other: output

Then within the if statement, the defaultBrowser gets set to the proper name of the Browser. Although you can certainly modify as needed/wanted.

The example AppleScript code, shown above, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate setting in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

For pre OS X 10.10, replace:

~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure

With:

com.apple.LaunchServices

In the do shell script "defaults ..." command.



Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

Upvotes: 4

Related Questions