-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/06/28 21:28
# dMod: 2021/06/28 21:37
# Appl: BBEdit
# Task: Replace Either RegEx or Literal Text in the Designated BBEdit Window.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Replace, @RegEx, @Literal
-------------------------------------------------------------------------------------------
# Use "Front_Text_Window" to Target the Front Window.
# Otherwise use the Window Name as the targetWindow.
# REMEMBER - The backslash for RegEx metacharacters "\" must be escaped in AppleScript "\\".
# Verbose:
set matchStr to "Nyet\\s"
set replaceStr to "Da"
set targetWindow to "Front_Text_Window"
set grepFlag to {|regEx|:true}
bbeditReplaceMatchInTargetWindow(matchStr, replaceStr, targetWindow, grepFlag)
# Less Verbose:
# bbeditReplaceMatchInTargetWindow("Nyet\\s", "Da", "Front_Text_Window", {|regEx|:true})
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on bbeditReplaceMatchInTargetWindow(matchStr, replaceStr, targetWindow, grepFlag)
tell application "BBEdit"
if targetWindow = "Front_Text_Window" then
set targetWindowRef to a reference to the front text window
else
set targetWindowRef to a reference to text window targetWindow
end if
if grepFlag's |regEx| = true then
set searchModeType to grep
else
set searchModeType to literal
end if
# Case-Sensitive is OFF by Default – Switch on using (?-i)
tell targetWindowRef's text
replace matchStr using replaceStr options {search mode:searchModeType, starting at top:true}
end tell
end tell
end bbeditReplaceMatchInTargetWindow
-------------------------------------------------------------------------------------------