Previous  |  Main  |  Next
AppleScript Logo

BBEdit DropSearch

Script for: BBEdit 6.x
Copyright: Matthias Steffens, use at your own risk!
Description: Searches all dropped items with BBEdit 6.x!

This is a script droplet on which you can drop any combination of files, folders,
file groups, Search scripts, etc. After that, a dialog pops up, where you can specify
your search pattern as well as some search options. If you proceed, all dropped items
will be searched with BBEdit 6.x and presented within a search results browser.

The following search options are available:
  • search with grep
  • search case sensitive
  • match entire word
Note: Don't use this droplet while BBEdit 5.x is running! If you do, you
          may need to recompile the script in order to have it working again.
Necessary: StandardAdditions.hqx from MacOS 8.5.x or greater  (command: "choose from list")
Download: BBEditDropSearch_v1.0.hqx (for BBEdit 6.x)
The Script:
property SearchPattern : "Enter your search pattern here!"
property SearchOptions : {"search with grep"}

global SearchItems, SearchWithGrep, SearchWithGrepDisplay, SearchCaseSensitive, MatchEntireWords, FileListCount, FolderListCount, FilePlural, FolderPlural


on run -- inform the user to drop files on the app instead of double clicking it:
	try
		set DialogResult to display dialog "Searches all dropped files and/or folders with BBEdit 6.x" & return & return & ¬
			"(or press 'Choose Folder' below to specify a single search folder)" & ¬
			return & return & "The following search options are available:" & ¬
			return & return & "      - search with grep" & return & "      - search case sensitive" & ¬
			return & "      - match entire word" with icon note buttons {"Choose Folder", "Quit"} default button 2
		
		if button returned of DialogResult is "Choose Folder" then
			-- offer the option to specify a single search folder {as opposed to dragging file(s)/folder(s) onto the app}:
			set SearchItems to my GetSearchFolder() as list
			
			-- although it's obvious that there's only 1 search folder here,
			-- we go thru 'ExtractSearchItems' in order to setup all variables right:
			my ExtractSearchItems()
			
			-- extract search options:
			my GetSearchOptions()
			
			-- pop up the main dialog:
			my MainDialog()
		end if
	on error ErrMsg number ErrNum
		display dialog ErrMsg & return & return & ErrNum with icon caution buttons "Cancel" default button 1
	end try
end run


on open (theItems) -- extract the dropped files and get some basic information about 'em:
	try
		set SearchItems to theItems
		
		-- separate dropped files and folders and count them individually:
		my ExtractSearchItems()
		
		-- extract search options:
		my GetSearchOptions()
		
		-- pop up the main dialog:
		my MainDialog()
	on error ErrMsg number ErrNum
		display dialog ErrMsg & return & return & ErrNum with icon caution buttons "Cancel" default button 1
	end try
end open


on MainDialog() -- display some search information and ask the user to specify search pattern & options:
	set DialogResult to display dialog "Search with BBEdit 6: " & FileListCount & " File" & FilePlural & " & " & ¬
		FolderListCount & " Folder" & FolderPlural & return & return & ¬
		"Current Search Options:" & return & ¬
		"      - search mode:                  " & SearchWithGrepDisplay & return & ¬
		"      - search case sensitive:    " & SearchCaseSensitive & return & ¬
		"      - match entire word:         " & MatchEntireWords & return & return & ¬
		"Search Pattern:" default answer SearchPattern with icon note buttons {"Cancel", "Search Options…", "OK"} default button 3
	
	if button returned of DialogResult is not "Cancel" then
		set SearchPattern to text returned of DialogResult -- extract the search pattern
		if button returned of DialogResult is "Search Options…" then
			-- uses the "choose from list" command from the "Standard Additions" scripting addition (from MacOS 8.5.x or greater):
			set ListResult to choose from list {"search with grep", "search case sensitive", "match entire word"} with prompt ¬
				"Choose Your Search Options:" OK button name "OK" cancel button name ¬
				"Cancel" default items SearchOptions with multiple selections allowed and empty selection allowed
			
			if ListResult is not false then -- if false: user pressed "Cancel"
				set SearchOptions to ListResult
				-- extract the chosen search options:
				my GetSearchOptions()
				-- pop up the main dialog:
				my MainDialog()
			end if
		end if
		
		if button returned of DialogResult is "OK" then my DoSearch()
	end if
end MainDialog


on ExtractSearchItems()
	set FileList to {}
	set FolderList to {}
	-- seperate dropped files and folders in order to count them individually:
	repeat with i from 1 to (count SearchItems)
		set TheItem to item i of SearchItems
		if (TheItem as string) ends with ":" then -- a folder
			copy TheItem to end of FolderList
		else -- a file
			copy TheItem to end of FileList
		end if
	end repeat
	-- count the number of dropped files and folders for display purposes:
	set FileListCount to count FileList
	set FolderListCount to count FolderList
	if (FileListCount = 1) then -- single file dropped
		set FilePlural to ""
	else -- multiple files dropped
		set FilePlural to "s"
	end if
	if (FolderListCount = 1) then -- single folder dropped
		set FolderPlural to ""
	else -- multiple folders dropped
		set FolderPlural to "s"
	end if
end ExtractSearchItems


on DoSearch() -- search the dropped files and folders:
	tell application "BBEdit 6.0"
		activate
		find SearchPattern searching in SearchItems options {search mode:SearchWithGrep, case sensitive:SearchCaseSensitive, match words:MatchEntireWords}
	end tell
end DoSearch


on GetSearchOptions() -- extract the chosen search options:
	if SearchOptions contains "search with grep" then
		tell application "BBEdit 6.0" to set SearchWithGrep to grep
		set SearchWithGrepDisplay to "grep"
	else
		tell application "BBEdit 6.0" to set SearchWithGrep to literal
		set SearchWithGrepDisplay to "literal"
	end if
	if SearchOptions contains "search case sensitive" then
		set SearchCaseSensitive to true
	else
		set SearchCaseSensitive to false
	end if
	if SearchOptions contains "match entire word" then
		set MatchEntireWords to true
	else
		set MatchEntireWords to false
	end if
end GetSearchOptions


on GetSearchFolder()
	choose folder with prompt "Choose a search folder:"
end GetSearchFolder

 


Contact: Matthias Steffens  |  Previous  |  Main  |  Next  |  Last Updated: 24-Sep-00