Tuesday, September 21, 2021

#1 2021-09-12 07:19:25 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1043

Search and Replace in Script Debugger

Occasionally I want to do a search and replace in Script Debugger but to limit its scope to selected text. That's the purpose of the script included below.

The operation of the script requires no explanation except to note that both the search and replacement strings are entered by the user in one dialog. This is easily changed if desired.

Also, some users may find it useful to compile the script after the search and replace is complete. I have included this command in the script but have disabled it with comment characters.

Applescript:

-- Revised 2021.09.16

on main()
   tell application "Script Debugger" to tell document 1
       set selectedText to selection
       set {c1, c2} to character range of selection
   end tell
   if selectedText = "" then errorDialog("A text selection was not found")
   
   set dialogResult to text returned of (display dialog "Enter the search and replacement strings separated by a greater-than symbol" default answer "search string > replacement string" with title "Search and Replace" with icon note)
   set text item delimiters to {" > ", ">"}
   try
       set {searchString, replacementString} to {text item 1, text item 2} of dialogResult
   on error
       errorDialog("A search or replacement string was not entered")
   end try
   
   set text item delimiters to searchString
   set modifiedText to text items of selectedText
   set searchStringInstances to ((count modifiedText) - 1)
   set text item delimiters to replacementString
   set modifiedText to modifiedText as text
   set text item delimiters to {""}
   
   if searchStringInstances = 0 then errorDialog("The search string " & quote & searchString & quote & " was not found in the selected text")
   
   if searchStringInstances = 1 then
       display dialog "Replace 1 instance of " & quote & searchString & quote & " with " & quote & replacementString & quote with title "Search and Replace" with icon note
   else
       display dialog "Replace " & searchStringInstances & " instances of " & quote & searchString & quote & " with " & quote & replacementString & quote with title "Search and Replace" with icon note
   end if
   
   set c2 to c2 + (((count replacementString) - (count searchString)) * searchStringInstances)
   tell application "Script Debugger" to tell document 1
       set selection to modifiedText
       set selection to {c1, c2}
       -- compile without showing errors
   end tell
end main

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()

Last edited by peavine (2021-09-16 08:14:50 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#2 2021-09-14 01:42:30 am

One208
Member
Registered: 2021-03-15
Posts: 25

Re: Search and Replace in Script Debugger

Thank you peavine, great script

I have mod it for myself to select entire script if no selection made

Applescript:


if selectedText = "" then set selectedText to contents of document 1

Offline

 

#3 2021-09-14 10:19:40 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1043

Re: Search and Replace in Script Debugger

Thanks One208 for looking at my script and for the excellent suggestion.

Last edited by peavine (2021-09-14 02:27:27 pm)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#4 2021-09-16 09:18:53 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1043

Re: Search and Replace in Script Debugger

This script is similar to that above, differing only in that the user is prompted whether to select the entire script if a text selection is not found.

Applescript:

-- Revised 2021.09.20

on main()
   tell application "Script Debugger" to tell document 1
       set selectedText to selection
       set {c1, c2} to character range of selection
   end tell
   
   if c2 = 0 then
       display dialog "A text selection was not found" buttons {"Cancel", "Select All"} cancel button 1 default button 2 with title "Search and Replace" with icon caution -- disable if desired
       tell application "Script Debugger" to tell document 1
           set selectedText to source text
           set {c1, c2} to {1, (count selectedText)}
           set selection to {c1, c2}
       end tell
   end if
   
   set dialogResult to text returned of (display dialog "Enter the search and replacement strings separated by a greater-than symbol" default answer "search string > replacement string" with title "Search and Replace" with icon note)
   if dialogResult = "" then errorDialog("Search and replacement strings were not entered")
   set text item delimiters to {" > ", ">"}
   set {searchString, replacementString} to {text item 1, text item 2} of dialogResult
   if searchString = "" or replacementString = "" then errorDialog("A search or replacement string was not entered")
   
   set text item delimiters to searchString
   set modifiedText to text items of selectedText
   set searchStringMatches to ((count modifiedText) - 1)
   set text item delimiters to replacementString
   set modifiedText to modifiedText as text
   set text item delimiters to {""}
   
   if searchStringMatches = 0 then errorDialog("The search string " & quote & searchString & quote & " was not found in the selected text")
   
   if searchStringMatches = 1 then
       display dialog "Replace 1 instance of " & quote & searchString & quote & " with " & quote & replacementString & quote with title "Search and Replace" with icon note
   else
       display dialog "Replace " & searchStringMatches & " instances of " & quote & searchString & quote & " with " & quote & replacementString & quote with title "Search and Replace" with icon note
   end if
   
   set c2 to c2 + (((count replacementString) - (count searchString)) * searchStringMatches)
   tell application "Script Debugger" to tell document 1
       set selection to modifiedText
       set selection to {c1, c2}
       -- compile without showing errors
   end tell
end main

on errorDialog(dialogText)
   display dialog dialogText buttons {"OK"} cancel button 1 default button 1 with title "Search and Replace" with icon stop
end errorDialog

main()

Last edited by peavine (Yesterday 08:19:15 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)