Change Safari's User Agent to Google Chrome

Mar 23, '11 07:30:00AM

Contributed by: schuyler

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.]

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20110322215325391