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.
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 domaincom.apple.Safari
). And apparently this is the one to update to change Safari’s search engine. (Set toDuckDuckGo
to use DuckDuckGo for instance.)Previously (before
SearchProviderIdentifierMigratedToSystemPreference
), the key wasSearchProviderIdentifier
, and the value wascom.duckduckgo
for the DuckDuckGo search engine.My recommendation would be to set all of them to be safe:
Related question: https://stackoverflow.com/a/16224386