Previous  |  Main  |  Next
AppleScript Logo

Multi-File Search Framework

Script for: BBEdit 6.x
Copyright: Matthias Steffens, use at your own risk!
Description: Script framework that shows how to script multi-file searches
with BBEdit 6.x. Use this script code within your own scripts...

The scripts "Multi-File Search with Grep" and "BBEdit DropSearch"
show similar examples how to implement multi-file searches.
Necessary: None
Download: MultiFileSearchFramework.hqx (for BBEdit 6.x)
The Script:
-- specify the search item(s):
-- (which can be an alias to a single file or folder as well as a *list* of aliases to any
-- combination of files, folders, file groups, or even applescripts that return a list of aliases!)
set SearchItems to choose folder with prompt "Choose a search folder:" -- ask the user to specify a search folder, or, alternatively,
-- use something like alias "Disk:folder:file" or {alias "Disk:folder:", alias "Disk:folder:file1", alias "Disk:folder:file2"} instead

-- specify the search pattern:
set SearchPattern to "</?body[^>]*>" -- this example grep pattern will find body tags (within html files)

tell application "BBEdit 6.1"
	-- specify various search options (adopt to your needs):
	set SearchWithGrep to grep -- replace 'grep' with 'literal' in order to search without grep
	-- set the following variables to either 'true' or 'false':
	set SearchCaseSensitive to true
	set MatchEntireWords to true
	set DisplayResultsWindow to true
	set ReturnResults to true
	
	-- now, build the proper record of search options:
	set SearchOptions to {search mode:SearchWithGrep, case sensitive:SearchCaseSensitive, match words:MatchEntireWords, showing results:DisplayResultsWindow, returning results:ReturnResults}
	
	-- finally, call the search routine:
	set TheHitList to my DoMultiFileSearch(SearchItems, SearchPattern, SearchOptions)
	
	-- gives examples how one could proceed with the results returned by the search routine:
	if not found of TheHitList then -- nothing found
		activate -- bring BBEdit to the front
		
		-- inform the user that nothing matched the search query:
		display dialog "Couldn't find any matches for \"" & SearchPattern & "\"" with icon note buttons {"OK"} default button 1
		
	else -- something was found			
		if DisplayResultsWindow then -- BBEdit will pop up a search results window
			activate -- bring BBEdit to the front
			
			-- this shows how to get a reference to the particular browser window that holds your search results:
			set SearchResultsWindowRef to match list window of TheHitList
			
			-- ... now you could do whatever you want with this window :-)
			-- as an example, lets zoom the results window to full screen:
			tell application "Finder" to copy bounds of window of desktop to screenSize -- get the screen dimensions
			tell SearchResultsWindowRef -- set properties of search results window:
				set bounds to {0, 41, item 3 of screenSize, item 4 of screenSize} -- zoom window to full screen
			end tell
		end if
		
		if ReturnResults then -- BBEdit will return a list of records:
			--this shows how you could extract all elements of a particular record item into a list:
			set MatchedItemList to {message, message_script, result_file, result_kind, result_line, start_offset, end_offset} of item 1 of found matches of TheHitList
			
			-- or, if SearchItems contains an alias to a *single* file, the following
			-- will open this file and select the first match found:
			(*
			set {SelStart, SelEnd} to {start_offset, end_offset} of item 1 of found matches of TheHitList
			activate -- bring BBEdit to the front
			open SearchItems -- open the file
			select (characters SelStart thru SelEnd) of text window 1 -- select a particular match
			*)
		end if
	end if
end tell


-- perform a multi-file search with BBEdit 6.x:
-- (for simple applications its actually not necessary to put this into
--  an extra routine but we do it here in order to generalize it a bit :-)
on DoMultiFileSearch(SearchItems, SearchPattern, SearchOptions)
	tell application "BBEdit 6.1"
		-- this line does the actual work:
		set TheHitList to find SearchPattern searching in SearchItems options SearchOptions
		return TheHitList -- this line won't be necessary in this case but I do for clarity reasons
	end tell
end DoMultiFileSearch

 


Contact: Matthias Steffens  |  Previous  |  Main  |  Next  |  Last Updated: 14-Oct-01