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

An AppleScript to Google search the current site in Safari Web Browsers
After I updated to Safari 3, AcidSearch stopped working properly, so I uninstalled it. I got Inquisitor instead, but I missed the ability to do a Google site search on whatever site I was visiting. So I put together this AppleScript, mostly assembled from other people's scripts: [robg adds: To use this, save it as a Script in ~/Library/Scripts/Safari, and make sure the Scripts Menu is enabled. Then just select it while visiting any page in Safari, enter the search term in the box, and you'll run a Google search restricted to that site. It worked fine for me when testing.]
    •    
  • Currently 1.83 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (6 votes cast)
 
[9,536 views]  

An AppleScript to Google search the current site in Safari | 6 comments | Create New Account
Click here to return to the 'An AppleScript to Google search the current site in Safari' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to Google search the current site in Safari
Authored by: johnga1t on Dec 13, '07 08:07:04AM
alternatively, you could use this javascript bookmarklet:

javascript:Qr=prompt('Search%20Site%20for','');if(Qr)location.href='http://www.google.com/search?&q=site:'+encodeURIComponent(window.location.hostname)+'+'+escape(Qr)

which will search the domain for whatever you enter in the pop-up box. nice because it'll work on any browser/platform.

[ Reply to This | # ]
An AppleScript to Google search the current site in Safari
Authored by: ben42 on Dec 15, '07 05:42:47AM

Thanks, this is a better solution. For those who are not up on bookmarklets: bookmark any old page and then edit the bookmark. Change the name to "Search This Site" (or whatever) and paste the javascript into the URL field.



[ Reply to This | # ]
An AppleScript to Google search the current site in Safari
Authored by: macmaxbh on Dec 13, '07 08:21:37AM
There is an AcidSearch beta which works again with Leopard:
http://pozytron.com/

I use it, but useful hint nevertheless.

[ Reply to This | # ]
An AppleScript to Google search the current site in Safari
Authored by: ben42 on Dec 14, '07 07:22:36PM

The newest Acidsearch may work with Leopard, but I had problems using it with Tiger and Safari 3.



[ Reply to This | # ]
An AppleScript to Google search the current site in Safari
Authored by: zoopiedoop on Dec 15, '07 12:02:51PM

AcidSearch 0.7b3 works reliably for me in Safari 3 on Tiger 10.4.11. (The newer version of AcidSearch did not and I had to downgrade.)



[ Reply to This | # ]
Here's one for LaunchBar users
Authored by: macevangelist on Dec 16, '07 01:51:35PM

on run
	set searchDomain to frontmostSafariDomain()
	if searchDomain is false then return false
	
	searchSiteWithGoogle(searchDomain, askForSearchString())
end run

on handle_string(searchString)
	set searchDomain to frontmostSafariDomain()
	if searchDomain is false then return false
	
	searchSiteWithGoogle(searchDomain, searchString)
end handle_string

on searchSiteWithGoogle(searchDomain, searchString)
	set searchString to searchAndReplace(searchString, " ", "+")
	set searchString to searchAndReplace(searchString, "\"", "%22")
	tell application "Safari" to open location "http://www.google.com/search?hl=en&q=site%3A" & searchDomain & "+" & searchString
	tell application "System Events" to if (exists process "LaunchBar") then set visible of process "LaunchBar" to false
end searchSiteWithGoogle

on askForSearchString()
	activate
	return text returned of (display dialog "Please enter a search string:" default answer "")
end askForSearchString



on frontmostSafariDomain()
	-- Init
	set searchDomain to false
	
	-- Checks
	tell application "System Events" to if (exists process "Safari") is false then return false
	tell application "Safari" to if (count of windows) < 1 then return false
	
	-- Get URL of frontmost Safari window
	tell application "Safari" to set currentURL to URL of document 1
	
	
	set prvDlmt to AppleScript's text item delimiters
	
	try
		set AppleScript's text item delimiters to "://"
		set searchDomain to text items 2 thru -1 of currentURL as string
		set AppleScript's text item delimiters to "/"
		set searchDomain to text item 1 of searchDomain as string
		set AppleScript's text item delimiters to "."
		set searchDomain to text items -2 thru -1 of searchDomain as string
	end try
	
	set AppleScript's text item delimiters to prvDlmt
	
	return searchDomain
end frontmostSafariDomain

on searchAndReplace(aString, searchString, replaceString)
	-- No need to continue execution if the string to search for is not in the string
	if aString does not contain searchString then return aString
	
	-- Save delimiters
	set prvDlmt to AppleScript's text item delimiters
	
	try
		set AppleScript's text item delimiters to searchString
		set returnString to text items of aString
		set AppleScript's text item delimiters to replaceString
		set returnString to returnString as string
	on error
		set returnString to aString
	end try
	
	-- Restore delimiters
	set AppleScript's text item delimiters to prvDlmt
	
	return returnString
end searchAndReplace


[ Reply to This | # ]