Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Change Safari's User Agent to Google Chrome Web Browsers
Google Maps sometimes will take an interminably long time to load in Safari. I have found that changing Safari's User Agent to Google Chrome consistently fixes this problem, resulting in snappy loading of Google Map web pages.

Changing the User Agent is a cumbersome process, involving making sure the Develop menu is visible in Safari's menu bar, retrieving Google Chrome's User Agent string from the Internet or a saved file, and then entering the string in the User Agent dialog window.

This hint covers how to use AppleScript to simplify this.

The following AppleScript automates this process. Copy and paste the code into AppleScript Editor and save the script either as an application or to ~/Library/Scripts/Applications/Safari and run it from the Scripts menu.
set getUptodateUserAgent to true -- or false to use a previously saved user agent
-- Make sure that Safari's Develop menu (containing the User Agent menu item) is visible in the menu bar
do shell script "defaults write com.apple.Safari IncludeDevelopMenu Yes"
-- Make sure that a window is visible in Safari so that the User Agent menu item is enabled
tell application "Safari"
	activate
	set miniaturized of windows to false
	if documents = {} then make new document with properties {URL:""}
end tell
-- If a previously saved user agent is to be used, retrieve it from the Preferences folder
set preferencesFile to (path to preferences as string) & "GoogleChromeUserAgent.txt"
if not getUptodateUserAgent then
	try
		close access file preferencesFile
	end try
	try
		set googleChromeUserAgent to (read file preferencesFile from 1)
	on error
		set googleChromeUserAgent to ""
	end try
end if
-- If a new user agent is to be used (or if a previous user agent could not be found), get Google Chrome's most up-to-date user agent from www.useragentstring.com, and save it in the Preferences folder for future use
if getUptodateUserAgent or (googleChromeUserAgent = "") then
	set googleChromeUserAgent to do shell script "curl 'http://www.useragentstring.com/pages/Chrome/' | egrep -o 'Mozilla[^<]+' | egrep -m 1 '.+'"
	try
		close access file preferencesFile
	end try
	open for access file preferencesFile with write permission
	set eof file preferencesFile to 0
	write googleChromeUserAgent to file preferencesFile
	close access file preferencesFile
end if
-- Change to the Google Chrome user agent in Safari
tell application "System Events" to tell process "Safari"
	set frontmost to true
	tell menu bar 1's menu bar item "Develop"'s menu "Develop"'s menu item "User Agent"'s menu "User Agent"'s menu item "Other…" to click
	delay 0.5
	tell window 1's sheet 1
		tell scroll area 1's text area 1 to set value to googleChromeUserAgent
		tell button "OK" to click
	end tell
end tell
Notes:

Set the getUptodateUserAgent variable to true to get Google Chrome's most up-to-date user agent from www.useragentstring.com; set it to false to use a previously saved user agent. The script may break if www.useragentstring.com changes its webpage structure or content.

The most recently retrieved Google Chrome User Agent string is saved in the text file GoogleChromeUserAgent.txt in the preferences folder. Getting the User Agent string from this file is faster than retrieving a new one from www.useragentstring.com each time.

Entering the string into Safari's User Agent dialog window relies on GUI scripting. The script may break if Safari's UI design or content changes.

[crarko adds: I haven't tested this one. Note: script updated per author's input.]
    •    
  • Currently 2.71 / 5
  You rated: 4 / 5 (7 votes cast)
 
[7,273 views]  

Change Safari's User Agent to Google Chrome | 4 comments | Create New Account
Click here to return to the 'Change Safari's User Agent to Google Chrome' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Change Safari's User Agent to Google Chrome
Authored by: Frozited on Mar 23, '11 08:22:07AM

Is there a way to change the user agent only for specific websites ?



[ Reply to This | # ]
Change Safari's User Agent to Google Chrome
Authored by: schuyler on Mar 25, '11 03:58:58PM

I am not aware of any Safari feature that permits site-specific assignment of a User Agent.

The following Applescript is a poor man's attempt to approximate such a feature. It prompts the user for an address (eg, 1 Infinite Loop, Cupertino, CA), then opens the address in a new Google Map window, and changes the User Agent to Google Chrome using the original script. Like the original script, it can be run either as a standalone application or from Safari's script menu. Incidentally, it would appear that any change to the User Agent remains in effect <u>only</u> for the current tab or window and is lost when that tab or window is closed.

-- Replace xxx in the following statement with the full HFS path to the User Agent-changing script, eg: "Macintosh HD:Users:username:Desktop:UserAgentChangeScript.app"
set pathToUserAgentScript to "xxx"
activate
set newURL to (display dialog "Enter a map address:" default answer "")'s text returned
tell application "Safari"
activate
make new document with properties {URL:"http://maps.google.com/maps?q=" & newURL}
end tell
run script file pathToUserAgentScript

Edited on Mar 29, '11 05:11:14AM by crarko



[ Reply to This | # ]
Re: Change Safari's User Agent to Google Chrome
Authored by: Mister Ed on Mar 23, '11 09:12:26PM

This worked great! Google images searches have also sped up significantly. I used to get a message that a script might be stalled. Now, no problems. Looks like Google may be crippling other browsers.

For those who don't know how to get the "Scripts" menu to show up, you need to open AppleScript Editor's preferences and select "Show Script menu in menu bar".

I also had to create the Applications/Safari folder in ~/Library/Scripts/.



[ Reply to This | # ]
Change Safari's User Agent to Google Chrome
Authored by: e_whizz on Mar 24, '11 03:16:30AM

Interesting, is this deliberate sabotage by google, to make their browser seem better?

Technically it does not 'make safari quicker', but potentially Google are sending Chrome optimised javascript etc to the browser they now think is Chrome. If this works flawlessly and faster in Safari, masquerading as Chrome, then that is what they should be sending a Safari user agent aswell.

Google may also be sending extra code to work around bugs in Safari, or to cater for older versions of Safari (where they don't have specific code branches for each version of Safari).



[ Reply to This | # ]