Mac OS X Safari 6 default search engine change between possible values

1.6k views Asked by At

I would like to change the Safari default search engine programmatically on Mac OS X. I know there are 3 possible values, but where Safari stores the current value? I would like to change from Google to Yahoo from Cocoa code. Is it possible?

I've seen in the a SearchProviderIdentifierMigratedToSystemPreference variable is set to YES in the com.apple.Safari.plist file, that can be located at ~/Library/Preferences. This could be the key to the current value, but where the "System Preference" is stored? Do you have any clues about this?


Update: I've found the this command defaults read -g NSPreferredWebServices always returns the selected default search engine. The problem with this is that when I change the value of it, the safari doesn't changes the value.
Here's the command that changes the value to Bing:
defaults write -g NSPreferredWebServices '{NSWebServicesProviderWebSearch = { NSDefaultDisplayName = Bing; NSProviderIdentifier = "com.bing.www"; }; }';

This command should be executed in the terminal.

1

There are 1 answers

0
Frizlab On

Apparently (tested with macOS 11.5), the NSPreferredWebServices sets the System search engine (where Spotlight redirects you when launching a web search with Spotlight for instance).
This key is updated whenever Safari’s search engine is updated.

Another key is updated too when Safari’s search engine changes: SearchProviderShortName (in domain com.apple.Safari). And apparently this is the one to update to change Safari’s search engine. (Set to DuckDuckGo to use DuckDuckGo for instance.)

Previously (before SearchProviderIdentifierMigratedToSystemPreference), the key was SearchProviderIdentifier, and the value was com.duckduckgo for the DuckDuckGo search engine.

My recommendation would be to set all of them to be safe:

defaults write -g NSPreferredWebServices '{NSWebServicesProviderWebSearch = { NSDefaultDisplayName = Bing; NSProviderIdentifier = "com.bing.www"; }; }'
defaults write com.apple.Safari SearchProviderIdentifier -string com.bing.www
defaults write com.apple.Safari SearchProviderShortName -string Bing

Related question: https://stackoverflow.com/a/16224386