The Script:
|
property SearchItems : {}
property SearchPattern : ""
global ShowResultString, ShowResultPosition, ShowResultFileName, ShowResultFilePath, ResultFile, ResultFileName, ResultFileContents
-- set either of the following variables to 'true' or 'false'
-- in order to control what columns will be shown in the search results window:
set ShowResultString to true -- display the matched text string
set ShowResultFileName to true -- display the file name of the file that contains the match
set ShowResultPosition to true -- display the line number of the match
set ShowResultFilePath to true -- display the file path of the file that contains the match
set ResultFile to ""
set oldDelims to AppleScript's text item delimiters
-- define the search target:
-- search a complete folder:
set SearchItems to choose folder with prompt "Choose a search folder:" -- ask the user to specify a search folder
-- if you want to search a single file comment the line above and uncomment the next line:
-- set SearchItems to choose file with prompt "Search file:" -- ask the user to specify the file to be searched
-- or, alternatively, use some hardcoded references like:
-- set SearchItems to alias "Disk:folder:file" -- or:
-- set SearchItems to {alias "Disk:folder:", alias "Disk:folder:file1", alias "Disk:folder:file2"}
-- specify the search pattern:
set DialogResult to display dialog "Search for:" default answer SearchPattern with icon note buttons {"Cancel", "OK"} default button 2
if button returned of DialogResult is not "Cancel" then
set SearchPattern to text returned of DialogResult
my DoSearch()
end if
set AppleScript's text item delimiters to oldDelims
on DoSearch() -- the search routine:
tell application "BBEdit 6.5"
-- 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 false -- specify whether BBEdit will pop up a search results window
set ReturnResults to true -- NOTE: in order to have this script work this variable *must* be set to 'true'!!
-- 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}
-- perform the search:
set TheHitList to find SearchPattern searching in SearchItems options SearchOptions
-- 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
set NewResultList to {}
if ReturnResults then -- BBEdit will return a list of records:
set ResultList to found matches of TheHitList
set ct to count ResultList
repeat with i from 1 to ct -- for every record, extract the relevant data:
set ResultItem to item i of ResultList
set {ResultItemFile, ResultItemLine, ResultItemStart, ResultItemEnd} to {result_file, result_line, start_offset, end_offset} of ResultItem
set ResultEntry to my BuildResultEntry(ResultItemFile, ResultItemLine, ResultItemStart, ResultItemEnd)
copy ResultEntry to end of NewResultList
end repeat
-- finally, merge the new result list and present it within a new BBEdit window:
set AppleScript's text item delimiters to {return}
set NewResultString to (text items of NewResultList) as string
activate -- bring BBEdit to the front
make new window with properties {name:("search results for: " & SearchPattern), tab width:33, contents:NewResultString} -- popup the search results window
select insertion point before character 1 of text window 1 -- goto line 1
end if
end if
end tell
end DoSearch
on BuildResultEntry(NewResultFile, ResultLine, ResultStart, ResultEnd) -- build a result entry line suitable for later display within the search results window:
set AppleScript's text item delimiters to ""
if (NewResultFile as string) is not (ResultFile as string) then -- we've got a new file, so:
set ResultFile to NewResultFile -- save the new file reference
tell application "Finder" to set ResultFileName to name of ResultFile -- let the finder resolve the file's name
set ResultFileContents to read ResultFile -- get the contents of the file
end if
set ResultEntryList to {}
-- for each item check if it's supposed to show up in the search results window:
-- [btw, the order of these if/then blocks controls the order of the columns in the search results window:]
if ShowResultString then -- extract the matched string from the file's text contents:
set ResultString to (characters ResultStart thru ResultEnd of ResultFileContents) as string
copy ResultString to end of ResultEntryList
end if
if ShowResultFileName then -- print the file's name:
copy ResultFileName to end of ResultEntryList
end if
if ShowResultPosition then -- print the line number plus its character offset range:
set ResultPosition to (ResultLine & " (" & ResultStart & "-" & ResultEnd & ")") as string
copy ResultPosition to end of ResultEntryList
end if
if ShowResultFilePath then -- print the file's path:
copy (ResultFile as string) to end of ResultEntryList
end if
set AppleScript's text item delimiters to {tab}
set ResultEntry to (text items of ResultEntryList) as string
return ResultEntry
end BuildResultEntry
|