Tyler Mullins
Tyler Mullins

Reputation: 77

Search the web with default browser and engine with applescript

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?

Upvotes: 0

Views: 953

Answers (1)

Lri
Lri

Reputation: 27613

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.

Upvotes: 2

Related Questions