0

I am trying to make a script that will do an internet search with the default browser and search engine. Open location works for opening with the default browser, but how would I use the default search engine?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Tyler Mullins
  • 77
  • 3
  • 10

1 Answers1

2

Changing the default search engine in Safari changed these preference keys:

defaults read -g NSPreferredWebServices
defaults read -app safari SearchProviderIdentifier

Neither existed on an unused 10.8 VM. NSPreferredWebServices also affects the Spotlight menu and the Search with Google/Yahoo!/Bing service.

You could use something like this:

query=query
id=$(/usr/libexec/PlistBuddy -c 'print NSPreferredWebServices:NSWebServicesProviderWebSearch:NSProviderIdentifier' ~/Library/Preferences/.GlobalPreferences.plist 2> /dev/null)
if [[ $id = com.yahoo.www ]]; then
    url="http://search.yahoo.com/search?p=$query"
elif [[ $id = com.bing.www ]]; then
    url="http://www.bing.com/search?q=$query"
else
    url="https://www.google.com/search?q=$query"
fi
open "$url"

The normal URLs depend on locales though.

Lri
  • 26,768
  • 8
  • 84
  • 82