The scripts included below were written for use with Script Debugger and Script Editor, and both utilize Shane’s Dialog Toolkit Plus script library. These scripts include search-all, match-word, and consider-case options. I tested these scripts on Monterey; they can be tested on earlier versions of macOS by removing the first line of the scripts.
Dialog Toolkit Plus can be downloaded from:
https://latenightsw.com/freeware/The Script Debugger script:
The Script Debugger script is:
-- revised 2022.07.31
use AppleScript version "2.8" -- remove to test with earlier versions of macOS
use framework "Foundation"
use script "Dialog Toolkit Plus" version "1.1.0"
use scripting additions
on main()
set {searchString, replaceString, searchOption, wordOption, caseOption} to displayDialog()
if searchString = "" then errorDialog("A search string was not entered")
searchAndReplace(searchString, replaceString, searchOption, wordOption, caseOption)
end main
on displayDialog()
set dialogWidth to 330
set verticalSpace to 10
set {theButtons, minWidth} to create buttons {"Cancel", "OK"} cancel button 1 default button 2
set {searchCheckbox, unusedTop, searchWidth} to create checkbox "Search All" left inset 0 bottom 0 max width dialogWidth
set {wordCheckbox, unusedTop, wordWidth} to create checkbox "Match Words" left inset (searchWidth + 8) bottom 0 max width dialogWidth
set {caseCheckbox, theTop, caseWidth} to create checkbox "Consider Case" left inset (searchWidth + wordWidth + 16) bottom 0 max width dialogWidth
set {replaceField, replaceLabel, theTop, fieldLeft} to create side labeled field "" placeholder text "replace with..." left inset 0 bottom (theTop + verticalSpace) total width dialogWidth label text "Replace:" field left 0
set {searchField, searchLabel, theTop, fieldLeft} to create side labeled field "" placeholder text "search for..." left inset 0 bottom (theTop + verticalSpace) total width dialogWidth label text "Search:" field left 0
set allControls to {searchCheckbox, wordCheckbox, caseCheckbox, replaceField, replaceLabel, searchField, searchLabel}
set {buttonName, controlsResults} to display enhanced window "Search and Replace" buttons theButtons acc view width dialogWidth acc view height theTop acc view controls allControls initial position {} without align cancel button
return {item 6, item 4, item 1, item 2, item 3} of controlsResults
end displayDialog
on searchAndReplace(searchString, replaceString, searchOption, wordOption, caseOption)
tell application id "com.latenightsw.ScriptDebugger8" to tell document 1
if searchOption is false then
set selectedText to selection
set {c1, c2} to character range of selection
else
set selectedText to source text
set selection to {1, (count selectedText)}
end if
end tell
if selectedText = "" then errorDialog("A text selection was not found")
set selectedText to current application's NSMutableString's stringWithString:selectedText
if wordOption is true and caseOption is true then
set searchPattern to "\\b" & searchString & "\\b"
set optionText to "The match-words and consider-case search options were enabled."
else if caseOption is true then
set searchPattern to searchString
set optionText to "The consider-case search option was enabled."
else if wordOption is true then
set searchPattern to "(?i)" & "\\b" & searchString & "\\b"
set optionText to "The match-words search option was enabled."
else
set searchPattern to "(?i)" & searchString
set optionText to "No search options were enabled."
end if
set searchStringMatches to (selectedText's replaceOccurrencesOfString:searchPattern withString:replaceString options:(current application's NSRegularExpressionSearch) range:{0, selectedText's |length|()})
if searchStringMatches = 0 then errorDialog("The search string " & quote & searchString & quote & " was not found in the selected text. " & optionText)
set selectedText to selectedText as text
if replaceString = "" then
if searchStringMatches = 1 then
set dialogText to "Delete 1 instance of " & quote & searchString & quote
else
set dialogText to "Delete " & searchStringMatches & " instances of " & quote & searchString & quote
end if
else
if searchStringMatches = 1 then
set dialogText to "Replace 1 instance of " & quote & searchString & quote & " with " & quote & replaceString & quote
else
set dialogText to "Replace " & searchStringMatches & " instances of " & quote & searchString & quote & " with " & quote & replaceString & quote
end if
end if
display dialog dialogText with title "Search and Replace" with icon note
tell application id "com.latenightsw.ScriptDebugger8" to tell document 1
set selection to selectedText
if searchOption is false then
set c2 to c2 + (((count replaceString) - (count searchString)) * searchStringMatches)
set selection to {c1, c2}
else
set selection to {1, 0}
set selection to {1, (count selectedText)}
end if
end tell
end searchAndReplace
on errorDialog(theText)
display dialog theText buttons {"OK"} cancel button 1 default button 1 with title "Search and Replace" with icon stop
end errorDialog
main()
The Script Editor script is:
-- revised 2022.07.31
use AppleScript version "2.8" -- remove to test with earlier versions of macOS
use framework "Foundation"
use script "Dialog Toolkit Plus" version "1.1.0"
use scripting additions
on main()
set {searchString, replaceString, searchOption, wordOption, caseOption} to displayDialog()
if searchString = "" then errorDialog("A search string was not entered")
searchAndReplace(searchString, replaceString, searchOption, wordOption, caseOption)
end main
on displayDialog()
set dialogWidth to 330
set verticalSpace to 10
set {theButtons, minWidth} to create buttons {"Cancel", "OK"} cancel button 1 default button 2
set {searchCheckbox, unusedTop, searchWidth} to create checkbox "Search All" left inset 0 bottom 0 max width dialogWidth
set {wordCheckbox, unusedTop, wordWidth} to create checkbox "Match Words" left inset (searchWidth + 8) bottom 0 max width dialogWidth
set {caseCheckbox, theTop, caseWidth} to create checkbox "Consider Case" left inset (searchWidth + wordWidth + 16) bottom 0 max width dialogWidth
set {replaceField, replaceLabel, theTop, fieldLeft} to create side labeled field "" placeholder text "replace with..." left inset 0 bottom (theTop + verticalSpace) total width dialogWidth label text "Replace:" field left 0
set {searchField, searchLabel, theTop, fieldLeft} to create side labeled field "" placeholder text "search for..." left inset 0 bottom (theTop + verticalSpace) total width dialogWidth label text "Search:" field left 0
set allControls to {searchCheckbox, wordCheckbox, caseCheckbox, replaceField, replaceLabel, searchField, searchLabel}
set {buttonName, controlsResults} to display enhanced window "Search and Replace" buttons theButtons acc view width dialogWidth acc view height theTop acc view controls allControls initial position {} without align cancel button
return {item 6, item 4, item 1, item 2, item 3} of controlsResults
end displayDialog
on searchAndReplace(searchString, replaceString, searchOption, wordOption, caseOption)
tell application "Script Editor" to tell document 1
if searchOption is false then
set selectedText to contents of selection
set {c1, c2} to character range of selection
else
set selectedText to contents
set {c1, c2} to {1, (count selectedText)}
set selection to {0, c2}
end if
end tell
if selectedText = "" then errorDialog("A text selection was not found")
set selectedText to current application's NSMutableString's stringWithString:selectedText
if wordOption is true and caseOption is true then
set searchPattern to "\\b" & searchString & "\\b"
set optionText to "The match-words and consider-case search options were enabled."
else if caseOption is true then
set searchPattern to searchString
set optionText to "The consider-case search option was enabled."
else if wordOption is true then
set searchPattern to "(?i)" & "\\b" & searchString & "\\b"
set optionText to "The match-words search option was enabled."
else
set searchPattern to "(?i)" & searchString
set optionText to "No search options were enabled."
end if
set searchStringMatches to (selectedText's replaceOccurrencesOfString:searchPattern withString:replaceString options:(current application's NSRegularExpressionSearch) range:{0, selectedText's |length|()})
if searchStringMatches = 0 then errorDialog("The search string " & quote & searchString & quote & " was not found in the selected text. " & optionText)
set selectedText to selectedText as text
if replaceString = "" then
if searchStringMatches = 1 then
set dialogText to "Delete 1 instance of " & quote & searchString & quote
else
set dialogText to "Delete " & searchStringMatches & " instances of " & quote & searchString & quote
end if
else
if searchStringMatches = 1 then
set dialogText to "Replace 1 instance of " & quote & searchString & quote & " with " & quote & replaceString & quote
else
set dialogText to "Replace " & searchStringMatches & " instances of " & quote & searchString & quote & " with " & quote & replaceString & quote
end if
end if
display dialog dialogText with title "Search and Replace" with icon note
set c2 to c2 + (((count replaceString) - (count searchString)) * searchStringMatches)
tell application "Script Editor" to tell document 1
set contents of selection to selectedText
set selection to {(c1 - 1), c2}
end tell
end searchAndReplace
on errorDialog(theText)
display dialog theText buttons {"OK"} cancel button 1 default button 1 with title "Search and Replace" with icon stop
end errorDialog
main()