Safari: "Clear Recent Searches" in Google menubar

Anyone know how to script System Events to “Clear Recent Searches” in Safari’s Google menubar menu? Thanks.

Jacques, your script breaks for me if the location/search field is not fourth in the toolbar (the default arrangement). I also have AcidSearch installed, which places a menu item right at the bottom of the list, again (unfortunately) breaking it.
Here is a script that works for me (and doesn’t rely on using GUI scripting): [tested on Safari 2.0.4]

activate application "Safari"
tell application "System Events" to tell process "Safari"
	-- Focus on search field
	keystroke "f" using {command down, option down}
	-- Press down key to show search menu
	key code 125
	-- Highlight and activate clear menu item
	keystroke "Clear Recent Searches" & return
end tell

Oh, and can I just ask, what is ASCII character 4?

Thanks for the replies. Since I have Safari set to the default arrangement, Jacques script works for me. Qwerty Denzel’s script breaks for me – it searches for “Clear Recent Searches” instead of deleting the recent searches.

The first 127 ASCII characters (7 bits worth) were originally developed for Teletype machines with the eighth bit a parity bit. Character #4 was then EOT – End of Transmission - Control-D on a Teletype (and on my keyboard - dual-core G5). Don’t know what it means in Jacques’ solution.


Using the default layout with Safari v3.1.2, the following script no longer works:

tell application "System Events" to tell process "Safari"
	set frontmost to true
	tell front window to tell text field 2 of splitter group 1 of group 4 of tool bar 1
		click button "search"
		keystroke (ASCII character 4)
		delay 0.5
		keystroke (ASCII character 13)
	end tell
end tell

Anyone know the correct GUI scripting?

Try this,

tell application "System Events"
	set btn to item 1 of UI elements of last item of UI elements of tool bar 1 of window 1 of application process "Safari"
	click button 1 of text field 2 of item 1 of btn
	click menu item "Clear Recent Searches" of menu 1 of button 1 of text field 2 of item 1 of btn
end tell

Thanks Mark!