Multiple site search script ?

Hi folks, i am a newbie and i landed here in my quest for an existing, customizable Firefox or Safari applescript which would allow to search one term in several sites at once. Do you know of any?

This is to replace the great “Multi Site Search” applescript (by Lisa Thompson) that I use at least twenty times a day with Camino (a Cocoa Firefox-inspired browser, but which unfortunately is become more and more obsolete).
Here is the description as Lisa Thomphson’s homepage is presently unavailable : “Multi-Site Search will ask for a search term, present a list of search-shortcut bookmarks, and open search-result pages for the chosen sites in new tabs or windows (depending on your Tab Prefs settings).Your bookmarked searches have to be saved in a Collection called “Searches” so that the script will know where to find them.”
And here is the script

-- Multi-Site Search v.1.1
-- Script by Lisa Thompson, lthompson.22@mac.com
-- <http://homepage.mac.com/lthompson.22/applescript/forcamino.html>

-- For use with Camino 1.6 & 2.0.
-- Tested in Mac OS 10.5, should work properly in 10.4.
-- Version 1.1: bug fix to ensure script aborts when user chooses "Cancel" from any dialog.

-- Install at the path ~/Library/Scripts/Applications/Camino, where ~ represents your home folder,
-- creating any subfolders that don't already exist; place on the Camino toolbar if desired.

-- Asks for a search term, presents a list of search-shortcut bookmarks saved in a "Searches" Collection, and 
-- opens search-result pages for the chosen sites in new tabs or windows (depending on user pref settings).
-- See the script Read Me file at <http://members.cox.net/lthompson-22/mssreadme.html> for further
-- information on using this script, including how to create a Collection and how to bookmark a search shortcut. 


tell application "Camino"
	
	if not (exists (bookmark folder "Searches")) then
		open location "about:bookmarks"
		set alertMsg1 to "There is no \"Searches\" Collection of search shortcut bookmarks."
		set alertMsg2 to "You must have your search shortcut bookmarks stored in a Collection called \"Searches\". In Bookmarks Manager, right-click in the Collections column and choose \"Add Collection\"."
		display alert alertMsg1 message alertMsg2 as informational buttons {"OK"} default button 1 cancel button 1
		return
	end if
	
	set srchBmarkList to name of every bookmark of bookmark folder "Searches" whose name is not "<Menu Spacer>"
	
	if (srchBmarkList = {}) then
		set alertMsg1 to "There are no search shortcut bookmarks in the  \"Searches\" Collection."
		set alertMsg2 to "You must have your search shortcut bookmarks stored in your \"Searches\" Collection. Click \"Learn More\" to see documentation on Camino's search shortcut bookmarks feature."
		display alert alertMsg1 message alertMsg2 as informational buttons {"Cancel", "Learn More"} default button 2 cancel button 1
		if result = {button returned:"Learn More"} then open location "http://caminobrowser.org/documentation/bookmarks/#shortcuts"
		return
	end if
	
	set tPrompt to "Please enter a search term:"
	set uSrchString to text returned of (display dialog tPrompt with title "Multi-Site Search" default answer ¬
		"" with icon note buttons {"Cancel", "OK"} cancel button 1 default button 2)
	set tPrompt to "Select one or more sites to search:"
	set uBmarkList to choose from list srchBmarkList with title "Multi-Site Search" with prompt tPrompt ¬
		OK button name "Search" with multiple selections allowed without empty selection allowed
	if uBmarkList is false then return
	
	repeat with aBmark in uBmarkList
		open location my replaceChars((URL of bookmark aBmark of bookmark folder "Searches"), "%s", uSrchString)
	end repeat
	
end tell

on replaceChars(srcText, oldChars, newChars)
	if oldChars = "" then return srcText
	set AppleScript's text item delimiters to oldChars
	set tList to (every text item of srcText)
	set AppleScript's text item delimiters to newChars
	set srcText to (tList as text)
	set AppleScript's text item delimiters to ""
	return srcText
end replaceChars

I have no idea if such a script already exists for Firefox or Safari (googling it, i only found this).

If not, even though i am not applescript savvy, I could invest some time in trying to adapt LT’s script if you knowledgeable guys tell me it should be feasible and suggest a method. Any input welcome. Cheers,

HH

Model: Mac Mini
AppleScript: 2.0.1
Browser: Camino 2.1.2
Operating System: Mac OS X (10.5)

Hi FroguetteMiNote. Welcome to MacScripter.

I’m afraid Safari’s bookmarks can’t be scripted directly and Firefox is barely scriptable at all.

Possibly the best recourse would be to write the bookmark data into the script itself, or into a text file which it could read.

If you don’t like that, Safari (at least) can be coaxed into returning bookmark data by means of GUI Scripting (ie. simulating user actions rather than going directly through the application’s scripting interface). The problem with GUI Scripting is that the same code doesn’t necessarily work with everyone’s combination of application version, system version, and preferences… GUI Scripting must be enabled on the host machine by checking “Enable access for assistive devices” in System Preferences’s “Universal Access” pane.

The script below has been tested with Safari 5.1.7 on my Mac OS 10.6.8 system up to the creation of the URLs for ‘open location’:

-- Adapted for Safari 5.1.7 running in Mac OS 10.6.8 by Nigel Garvey from:
-- Multi-Site Search v.1.1
-- Script by Lisa Thompson, lthompson.22@mac.com
-- <http://homepage.mac.com/lthompson.22/applescript/forcamino.html>

-- Originally written for Camino 1.6 & 2.0.

-- Safari's bookmarks can't (so far) be scripted directly, so this script resorts to GUI Scripting.
-- For this, "Enable access for assistive devices" must be checked in System Preferences's "Universal Access" pane.

on main()
	-- Open Safari's Bookmark managing page.
	tell application "Safari" to show bookmarks
	
	tell application "System Events"
		tell application process "Safari"
			set frontmost to true
			tell front window
				-- Check the Collections column for a folder called "Searches". Select it if it exists. Complain if not.
				set SearchesRow to a reference to (first row of table 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of group -1 whose value of text field 1 is "Searches")
				if (SearchesRow exists) then
					set SearchesRow's selected to true
				else
					my showError("There is no \"Searches\" Collection of search shortcut bookmarks.", "You must have your search shortcut bookmarks stored in a Collection called \"Searches\".", {"Cancel", "Create it for me"})
					if (button returned of result is "Create it for me") then
						keystroke "n" using {shift down, command down}
						keystroke "Searches"
						keystroke return
					end if
					error number -128
				end if
				-- Check the "Searches" folder for bookmarks. Gather all the details if any. Complain if not.
				tell rows of outline 1 of scroll area 1 of splitter group 1 of group 2 of splitter group 1 of group 1 of group -1
					if (it exists) then
						set {srchBmarkList, srchURLList} to {value of text field 1, value of text field 2}
					else
						my showError("There are no search shortcut bookmarks in the \"Searches\" Collection.", "You must have your search shortcut bookmarks stored in your \"Searches\" Collection.", {"Cancel"})
						error number -128
					end if
				end tell
			end tell
		end tell
	end tell
	
	tell application "Safari"
		-- Close the Bookmarks page.
		close current tab of window 1
		
		-- Prompt for a search term.
		set tPrompt to "Please enter a search term:"
		set uSrchString to text returned of (display dialog tPrompt with title "Multi-Site Search" default answer ¬
			"" with icon note buttons {"Cancel", "OK"} cancel button 1 default button 2)
		-- Prompt for site(s) section.
		set tPrompt to "Select one or more sites to search:"
		set uBmarkList to (choose from list srchBmarkList with title "Multi-Site Search" with prompt tPrompt ¬
			OK button name "Search" with multiple selections allowed without empty selection allowed)
		if (uBmarkList is false) then error number -128
		
		-- Issue the appropriate 'open location' commands.
		repeat with i from 1 to (count srchBmarkList)
			if (item i of srchBmarkList is in uBmarkList) then
				open location my replaceChars(item i of srchURLList, "%s", uSrchString)
			end if
		end repeat
	end tell
end main

on showError(alertMsg1, alertMsg2, alertBtns)
	tell application "Safari"
		display alert alertMsg1 message alertMsg2 as informational buttons alertBtns default button (get end of alertBtns)
	end tell
end showError

on replaceChars(srcText, oldChars, newChars)
	if ((count oldChars) > 0) then
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to oldChars
		set tList to (every text item of srcText)
		set AppleScript's text item delimiters to newChars
		set srcText to (tList as text)
		set AppleScript's text item delimiters to astid
	end if
	
	return srcText
end replaceChars

main()

Thanks so much for doing this, Nigel! :slight_smile: I will test this asap.

FMN