Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the default browser name in bash script on Mac OS X

I want to find out if the default browser is Google Chrome on a Mac OS X machine before the script executes.

How can I do it? thanks!

like image 380
Gal Avatar asked Sep 08 '15 12:09

Gal


People also ask

How do I change my default browser on Mac terminal?

The only way is to set it in the main Settings: From the Apple menu, choose System Preferences, then click General. Click the “Default web browser” pop-up menu and choose a web browser, like Chrome.

How do I access Bash on OSX?

Hold the Ctrl key, click your user account's name in the left pane, and select “Advanced Options.” Click the “Login Shell” dropdown box and select “/bin/bash” to use Bash as your default shell or “/bin/zsh” to use Zsh as your default shell. Click “OK” to save your changes.

Is OSX terminal Bash?

Mac OS X comes with the Bourne Again SHell (bash) as the default user shell and also includes the TENEX C shell (tcsh), the Korn shell (ksh), and the Z shell (zsh). bash, ksh, and zsh are compatible with sh, the original Bourne shell.

What is Bash on Mac?

Bash is a command-line interface shell program used extensively in Linux and macOS. The name Bash is an acronym for "Bourne Again Shell," developed in 1989 as a successor to the Bourne Shell.


1 Answers

You can grep/awk the launch services preferences list to find out which browser is set as default:

x=~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist; \
plutil -convert xml1 $x; \
grep 'https' -b3 $x | awk 'NR==2 {split($2, arr, "[><]"); print arr[3]}'; \
plutil -convert binary1 $x

This sets a variable (x) to the launch services preferences list, then converts it using plutil to xml format so we can grep it. We locate the string we're looking for (https) then output the result. The final step is to convert the plist back to binary format.

If chrome is set to be default you will get:

Result:

com.google.chrome
like image 158
l'L'l Avatar answered Nov 14 '22 21:11

l'L'l