Hi, would be great if it were possible to replace only in selected text.
Currently it’s necessary to use menu Edit > Edit With BBEdit and Selected text only in BBEdit’s Find window. That’s of course doable but gets a bit cumbersome if there’s a lot to replace (because I want to compile after each replacement which means I currently have to switch between BBEdit and Script Debugger a lot)
Have you tried doing it via script? You can get the selection, act on that using AppleScript (or BBEdit if you prefer), then set the updated selection.
@ShaneStanley’s suggestion is your only means to improve this particular workflow. BBEdit is immensely scriptable, and Script Debugger isn’t bad – so you can do quite a lot between them.
Didn’t try a script yet as asking for the search and the replace string via dialog wasn’t attractive.
Idea now is to to get the search and the replace string from Script Debugger, then apply that to selected text. I can get the search string from NSFindPboard, but where is the replace string stored?
tell application id "com.latenightsw.ScriptDebugger8" -- require v8.0.2
set findString to search string
set replaceString to search replace string
set usesRegex to search uses regex
set ignoresCase to search ignores case
end tell
tell application id "com.latenightsw.ScriptDebugger8" -- requires Script Debugger 8.0.2 or later
set searchString to search string
set replaceString to search replace string
end tell
Note the requirement for version 8.0.2 or later – there was a bug in earlier versions that meant sometimes a stale search replace string was returned.
-- Replace in selection
-- Requires Script Debugger 8.0.2 or later
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
tell application "Script Debugger"
try
if not (exists script window 1) then return
set theDocument to current document of script window 1
set theSelection to selection of theDocument
if theSelection = "" then error "Please select some text"
set theSearchString to search string
if theSearchString = "" then error "Please provide a search string"
set theSearchReplaceString to search replace string
if theSearchReplaceString = "" then error "Please provide a replacement string"
set theResult to my replaceString(theSelection, theSearchString, theSearchReplaceString, search uses regex, search matches words, search ignores case)
if theResult ≠ theSelection then set selection of theDocument to theResult
on error error_message number error_number
if the error_number is not -128 then display alert "Script Debugger" message error_message as warning
return
end try
end tell
on replaceString(theText, thePattern, theReplacement, useRegex, useWholeWord, useIgnoreCase)
try
set theOptions to 0
if useRegex then
set theOptions to theOptions + ((current application's NSRegularExpressionSearch) as integer)
else if useWholeWord then
set theOptions to theOptions + ((current application's NSRegularExpressionSearch) as integer)
set thePattern to current application's NSString's stringWithString:thePattern
set thePattern to thePattern's stringByReplacingOccurrencesOfString:("\\w+") withString:("\\\\b$0\\\\b") options:((current application's NSRegularExpressionSearch)) range:{location:0, |length|:thePattern's |length|()}
end if
if useIgnoreCase then set theOptions to theOptions + ((current application's NSCaseInsensitiveSearch) as integer)
set theString to current application's NSString's stringWithString:theText
set newString to theString's stringByReplacingOccurrencesOfString:(thePattern) withString:(theReplacement) options:theOptions range:{location:0, |length|:theString's |length|()}
set newText to newString as string
on error error_message number error_number
activate
if the error_number is not -128 then display alert "Error: Handler \"replaceString\"" message error_message as warning
error number -128
end try
end replaceString