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 replace strings are entered by the user in one dialog. This is easily changed if desired.

-- Revised 2021.09.27

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 replace strings separated by a greater-than symbol" default answer "search string > replace string" with title "Search and Replace" with icon note)
	set {searchString, replaceString} to {"", ""}
	set text item delimiters to {" > ", ">"}
	try
		set {searchString, replaceString} to {text item 1, text item 2} of dialogResult
	end try
	if searchString = "" then errorDialog("A search or replace 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 replaceString
	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 & replaceString & quote with title "Search and Replace" with icon note
	else
		display dialog "Replace " & searchStringMatches & " instances of " & quote & searchString & quote & " with " & quote & replaceString & quote with title "Search and Replace" with icon note
	end if
	
	set c2 to c2 + (((count replaceString) - (count searchString)) * searchStringMatches)
	tell application "Script Debugger" to tell document 1
		set selection to modifiedText
		set selection to {c1, c2}
		-- compile without showing errors -- enable if desired
	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()

Thank you peavine, great script

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


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

Thanks One208 for looking at my script and for the suggestion. I’ve modified my script in post 1 to include a user-option to select the entire script.

Hi peavine,

thanks for the great script!
I have added a search/replace dialog.

Greetings
Dirk


-- Revised 2021.09.26

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 my showSearchReplaceDialog()
	if dialogResult is missing value then
		return
	end if
	set {searchString, replaceString} to {searchString, replaceString} of dialogResult
	
	set text item delimiters to searchString
	set modifiedText to text items of selectedText
	set searchStringMatches to ((count modifiedText) - 1)
	set text item delimiters to replaceString
	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 & replaceString & quote with title "Search and Replace" with icon note
	else
		display dialog "Replace " & searchStringMatches & " instances of " & quote & searchString & quote & " with " & quote & replaceString & quote with title "Search and Replace" with icon note
	end if
	
	set c2 to c2 + (((count replaceString) - (count searchString)) * searchStringMatches)
	tell application "Script Debugger" to tell document 1
		set selection to modifiedText
		set selection to {c1, c2}
		-- compile without showing errors -- enable if desired
	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

on showSearchReplaceDialog()
	script theScript
		use scripting additions
		use framework "Foundation"
		use framework "AppKit"
		use framework "Carbon"
		
		property ca : current application
		property dialogWindow : missing value
		property searchTextField : missing value
		property replaceTextField : missing value
		property replaceButton : missing value
		property replaceClicked : false
		
		on showSearchReplaceDialog()
			if current application's AEInteractWithUser(-1, missing value, missing value) ≠ 0 then
				return missing value
			end if
			
			if ca's NSThread's isMainThread() then
				my performSearchReplaceDialog:(missing value)
			else
				its performSelectorOnMainThread:"performSearchReplaceDialog:" withObject:(missing value) waitUntilDone:true
			end if
			
			if my replaceClicked then
				return {searchString:searchTextField's stringValue() as text, replaceString:replaceTextField's stringValue() as text}
			end if
			return missing value
		end showSearchReplaceDialog
		
		on performSearchReplaceDialog:args
			set findLabel to ca's NSTextField's labelWithString:"Search:"
			findLabel's setFrame:(ca's NSMakeRect(20, 85, 70, 20))
			
			set my searchTextField to ca's NSTextField's textFieldWithString:""
			searchTextField's setFrame:(ca's NSMakeRect(87, 85, 245, 20))
			searchTextField's setEditable:true
			searchTextField's setBordered:true
			searchTextField's setPlaceholderString:"Search for …"
			searchTextField's setDelegate:me
			
			set replaceLabel to ca's NSTextField's labelWithString:"Replace:"
			replaceLabel's setFrame:(ca's NSMakeRect(20, 55, 70, 20))
			
			set my replaceTextField to ca's NSTextField's textFieldWithString:""
			replaceTextField's setFrame:(ca's NSMakeRect(87, 55, 245, 20))
			replaceTextField's setEditable:true
			replaceTextField's setBordered:true
			replaceTextField's setPlaceholderString:"Replace with …"
			
			set cancelButton to ca's NSButton's buttonWithTitle:"Cancel" target:me action:"buttonAction:"
			cancelButton's setFrameSize:{94, 32}
			cancelButton's setFrameOrigin:{150, 10}
			cancelButton's setKeyEquivalent:(character id 27)
			
			set my replaceButton to ca's NSButton's buttonWithTitle:"Replace" target:me action:"buttonAction:"
			replaceButton's setFrameSize:{94, 32}
			replaceButton's setFrameOrigin:{245, 10}
			replaceButton's setKeyEquivalent:return
			replaceButton's setEnabled:false
			
			set windowSize to ca's NSMakeRect(0, 0, 355, 125)
			set winStyle to (ca's NSWindowStyleMaskTitled as integer) + (ca's NSWindowStyleMaskClosable as integer)
			set my dialogWindow to ca's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:(ca's NSBackingStoreBuffered) defer:true
			
			dialogWindow's contentView()'s addSubview:findLabel
			dialogWindow's contentView()'s addSubview:searchTextField
			dialogWindow's contentView()'s addSubview:replaceLabel
			dialogWindow's contentView()'s addSubview:replaceTextField
			dialogWindow's contentView()'s addSubview:cancelButton
			dialogWindow's contentView()'s addSubview:replaceButton
			
			dialogWindow's setTitle:"Search and Replace"
			dialogWindow's setLevel:(ca's NSModalPanelWindowLevel)
			dialogWindow's setDelegate:me
			dialogWindow's orderFront:me
			dialogWindow's |center|()
			
			ca's NSApp's activateIgnoringOtherApps:true
			ca's NSApp's runModalForWindow:dialogWindow
		end performSearchReplaceDialog:
		
		on buttonAction:sender
			if sender is my replaceButton then
				set my replaceClicked to true
			end if
			my dialogWindow's |close|()
		end buttonAction:
		
		on controlTextDidChange:aNotification
			set sender to aNotification's object()
			if sender is my searchTextField then
				if sender's stringValue() as text ≠ "" then
					my (replaceButton's setEnabled:true)
				else
					my (replaceButton's setEnabled:false)
				end if
			end if
		end controlTextDidChange:
		
		on windowWillClose:aNotification
			ca's NSApp's stopModal()
		end windowWillClose:
	end script
	return theScript's showSearchReplaceDialog()
end showSearchReplaceDialog

main()

(Tested with Catalina and Bug Sur)

Thanks Dirk. That is outstanding. :slight_smile:

BTW, I was interested by one small change you made in the basic functioning of my script. If the user does not enter a replace string, the script deletes the search string wherever it occurs in the selected text. Whether this is desirable or not is a matter of personal preference, although, for my own use, I had previously modified my script in post 1 to operate this way. The user is notified that the replace string is “”, so there’s no confusion as to what is going to happen.

Edit September 27, 2021. As pointed out by db123, the default behavior of Script Debugger’s Search and Replace is to delete the search string if the replace string is empty, and I’ve modified my script in post 1 to operate this way.

Hi peavine,

this is the default behavior for Find & Replace, and I think the script is perfect. I have added it to the scripts in the Script Debugger and assigned a shortcut in Preferences → Key Bindings.

I wasn’t aware you could add scripts to Script Debugger and set key-bindings for them. That’s a nice feature.

That’s why I had mentioned it. :wink:
Script Debugger is already an ingenious tool.

This is not correct. Replace all searches and replaces the entire code, not just the selected part.

Fredrik71. I originally wrote this script in response to a request on the Script Debugger forum and to Shane’s suggestion that the forum member write a script to accomplish what was desired. The fact that my script is not of interest to you doesn’t mean it’s not of interest to others.

https://forum.latenightsw.com/t/replace-only-in-selection/3293

Second, as db123 notes, Script Debugger will not restrict a search and replace to a selection–it doesn’t matter if the regex option is checked or not. Why couldn’t you take the few minutes it takes to verify your criticism?

Third, the purpose of my script is to do a quick search and replace on text selected in the editor window and has absolutely nothing to do with regex. I clearly state the purpose of my script in my initial post:

I do not object to constructive criticism of my work, but your post is wrong in every respect.

1 Like

db123. I’ve encountered an intermittent issue with your script in which the replace string is not shown in the confirmation dialog and “” is shown instead. I have not encountered this issue with my script, so I assume the issue is with the dialog handlers. I’ll do some additional research to better determine when exactly this issue arises.

@peavine: Strange. This has not yet occurred with me (Big Sur).

db123. Thanks for letting me know that. I run the script with absolutely no changes and I run it by way of Script Debugger, so I’m not doing anything that might cause the issue. It’s very intermittent but I’ll track it down.

@peavine: The script debugger runs the script from the menu using the osascript process. This is a background process and I had to use some tricks to get the dialog to the foreground (current application’s AEInteractWithUser and NSApp’s activateIgnoringOtherApps).
Under Catalina it also behaves a bit different. So the buttons are not accessible with the tab key. In Big Sur they are. I think it is more likely due to the ScriptingBridge.

Edit: I have changed the script so that the values are saved to properties before the dialog is closed. Maybe this is the reason:


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 my showSearchReplaceDialog()
	if dialogResult is missing value then
		return
	end if
	set {searchString, replaceString} to {searchString, replaceString} of dialogResult
	
	set text item delimiters to searchString
	set modifiedText to text items of selectedText
	set searchStringMatches to ((count modifiedText) - 1)
	set text item delimiters to replaceString
	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 & replaceString & quote with title "Search and Replace" with icon note
	else
		display dialog "Replace " & searchStringMatches & " instances of " & quote & searchString & quote & " with " & quote & replaceString & quote with title "Search and Replace" with icon note
	end if
	
	set c2 to c2 + (((count replaceString) - (count searchString)) * searchStringMatches)
	tell application "Script Debugger" to tell document 1
		set selection to modifiedText
		set selection to {c1, c2}
		-- compile without showing errors -- enable if desired
	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

on showSearchReplaceDialog()
	script theScript
		use scripting additions
		use framework "Foundation"
		use framework "AppKit"
		use framework "Carbon"
		
		property ca : current application
		property dialogWindow : missing value
		property searchTextField : missing value
		property replaceTextField : missing value
		property cancelButton : missing value
		property replaceButton : missing value
		property searchString : missing value
		property replaceString : missing value
		property replaceClicked : false
		
		on showSearchReplaceDialog()
			if current application's AEInteractWithUser(-1, missing value, missing value) ≠ 0 then
				return missing value
			end if
			
			if ca's NSThread's isMainThread() then
				my performSearchReplaceDialog:(missing value)
			else
				its performSelectorOnMainThread:"performSearchReplaceDialog:" withObject:(missing value) waitUntilDone:true
			end if
			
			if my replaceClicked then
				return {searchString:my searchString as text, replaceString:my replaceString as text}
			end if
			return missing value
		end showSearchReplaceDialog
		
		on performSearchReplaceDialog:args
			set findLabel to ca's NSTextField's labelWithString:"Search:"
			findLabel's setFrame:(ca's NSMakeRect(20, 85, 70, 20))
			
			set my searchTextField to ca's NSTextField's textFieldWithString:""
			searchTextField's setFrame:(ca's NSMakeRect(87, 85, 245, 20))
			searchTextField's setEditable:true
			searchTextField's setBordered:true
			searchTextField's setPlaceholderString:"Search for …"
			searchTextField's setDelegate:me
			
			set replaceLabel to ca's NSTextField's labelWithString:"Replace:"
			replaceLabel's setFrame:(ca's NSMakeRect(20, 55, 70, 20))
			
			set my replaceTextField to ca's NSTextField's textFieldWithString:""
			replaceTextField's setFrame:(ca's NSMakeRect(87, 55, 245, 20))
			replaceTextField's setEditable:true
			replaceTextField's setBordered:true
			replaceTextField's setPlaceholderString:"Replace with …"
			
			set my cancelButton to ca's NSButton's buttonWithTitle:"Cancel" target:me action:"buttonAction:"
			cancelButton's setFrameSize:{94, 32}
			cancelButton's setFrameOrigin:{150, 10}
			cancelButton's setKeyEquivalent:(character id 27)
			
			set my replaceButton to ca's NSButton's buttonWithTitle:"Replace" target:me action:"buttonAction:"
			replaceButton's setFrameSize:{94, 32}
			replaceButton's setFrameOrigin:{245, 10}
			replaceButton's setKeyEquivalent:return
			replaceButton's setEnabled:false
			
			set windowSize to ca's NSMakeRect(0, 0, 355, 125)
			set winStyle to (ca's NSWindowStyleMaskTitled as integer) + (ca's NSWindowStyleMaskClosable as integer)
			set my dialogWindow to ca's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:(ca's NSBackingStoreBuffered) defer:true
			
			dialogWindow's contentView()'s addSubview:findLabel
			dialogWindow's contentView()'s addSubview:searchTextField
			dialogWindow's contentView()'s addSubview:replaceLabel
			dialogWindow's contentView()'s addSubview:replaceTextField
			dialogWindow's contentView()'s addSubview:cancelButton
			dialogWindow's contentView()'s addSubview:replaceButton
			
			dialogWindow's setTitle:"Search and Replace"
			dialogWindow's setLevel:(ca's NSModalPanelWindowLevel)
			dialogWindow's setDelegate:me
			dialogWindow's orderFront:me
			dialogWindow's |center|()
			
			ca's NSApp's activateIgnoringOtherApps:true
			ca's NSApp's runModalForWindow:dialogWindow
		end performSearchReplaceDialog:
		
		on buttonAction:sender
			if sender is my replaceButton then
				set my searchString to searchTextField's stringValue()
				set my replaceString to replaceTextField's stringValue()
				set my replaceClicked to true
			end if
			my dialogWindow's |close|()
		end buttonAction:
		
		on controlTextDidChange:aNotification
			set sender to aNotification's object()
			if sender is my searchTextField then
				if sender's stringValue() as text ≠ "" then
					my (replaceButton's setEnabled:true)
				else
					my (replaceButton's setEnabled:false)
				end if
			end if
		end controlTextDidChange:
		
		on windowWillClose:aNotification
			ca's NSApp's stopModal()
		end windowWillClose:
	end script
	return theScript's showSearchReplaceDialog()
end showSearchReplaceDialog

main()

Thanks db123. I’ll give the new version a try.

db123. I’ve been using the new version of your script for almost 3 days now and haven’t encountered the issue I note above. So, everything looks great. Thanks.

@peavine: Apparently the NSTextFields were occasionally reset to default values when closing the dialog. By saving the values in properties before closing, the problem may no longer occur.

Thanks for your feedback!

You can save yourself a lot of work if you use Script Debugger’s UI, rather than write your own. So your script would start like this:

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

Note that it says version 8.0.2 is required. This has only just been released, and it fixes a bug where the search replace string property wasn’t always updated correctly.

Using ASObjC you could then find the matching ranges and replace them one at a time (starting at the end), so that unchanged text would retain its format.

There seems to be some misunderstanding as to the intended use of my script, and I thought I should clarify. I can most easily do this with a use example.

When working on a script, I frequently paste in the script a handler obtained from one of numerous sources. As often as not, one of the variables has a name that does not make sense in the context of the script and so I replace it. I do this by selecting the handler and running the search-and-replace script contained earlier in this thread. This works great.

The key issue here is speed and simplicity. Script Debugger’s built-in search-and-replace is full-featured and I use it often. In the above use example, my script with db123’s excellent edit works better for me.

BTW, Shane has made numerous suggestions for improving my scripts, which I almost always adopt, and I greatly appreciate his advice. In this one particular instance, I think my existing script better meets my needs.

I think Shane meant that you could spare the script section with the dialog window and access the input fields of Script Debugger instead. That’s right, but we don’t deal with Applescript exclusively solution-oriented, but also to get to know AppleScriptObjC, for example. In this respect it was worth the effort (for me) and replacing with a popup window is even faster in the use case:

Still, thanks to Shane for the info and especially for the brilliant idea of being able to run scripts with shortcuts and for the Script Debugger app in general!