You are not logged in.
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.
Applescript:
-- 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()
Last edited by peavine (2021-09-27 07:17:24 am)
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline
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
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.
Last edited by peavine (2021-09-23 07:44:32 am)
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline
Hi peavine,
thanks for the great script!
I have added a search/replace dialog.
Greetings
Dirk
Applescript:
-- 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)
Last edited by db123 (2021-09-26 07:22:50 am)
Offline
Thanks Dirk. That is outstanding.
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.
Last edited by peavine (2021-09-27 08:00:10 am)
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline
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.
Last edited by db123 (2021-09-26 02:57:17 pm)
Offline
I wasn't aware you could add scripts to Script Debugger and set key-bindings for them. That's a nice feature.
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline
That's why I had mentioned it.
Script Debugger is already an ingenious tool.
Last edited by db123 (2021-09-27 04:17:29 am)
Offline
@peavine and @db123
Nice script, but why?...
Script Debugger already include Regular Expression find and replace
The default for most application it use shortcut command + F
If custom selection or part of the source code it will only do search and replace on that part.
Thanks for sharing anyway....
if you are the expert, who will you call if its not your imagination.
Offline
If custom selection or part of the source code it will only do search and replace on that part.
This is not correct. Replace all searches and replaces the entire code, not just the selected part.
Offline
@peavine and @db123
Nice script, but why?...
Script Debugger already include Regular Expression find and replace
The default for most application it use shortcut command + F
If custom selection or part of the source code it will only do search and replace on that part.
Thanks for sharing anyway....
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 … ction/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:
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.
I do not object to constructive criticism of my work, but your post is wrong in every respect.
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline
@peavine
To have other workflow or approach to something doesn't mean something else is not useful.
Nor I have send any criticism to you or anyone else. In the opposite... I said: Nice Script and also Thanks for sharing.
Do you really think I'm a worse person for that, or maybe you are to sensitive.
Ask yourself how many people thank you for sharing your code, I did... also for @db123 code.
@db123
I thought it was possible to select text and do search on that and I was wrong.
Any code that are shared in public forum become part of the community. So I do not really understand why anyone could take it personal if someone have an opinion. It was not more and that but I guess it was not welcome, so sorry for that.
Last edited by Fredrik71 (2021-09-28 09:53:27 am)
if you are the expert, who will you call if its not your imagination.
Offline
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.
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline
@peavine: Strange. This has not yet occurred with me (Big Sur).
Offline
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.
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline
@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:
Applescript:
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()
Last edited by db123 (2021-10-09 01:33:35 pm)
Offline
Thanks db123. I'll give the new version a try.
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline
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.
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline
@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!
Offline
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:
Applescript:
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.
Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com
Offline
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.
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline
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:
Ctrl+F -> Enter search value -> Tab Key -> Enter Replace value -> Enter Key - finished!
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!
Offline
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. You could still use the same code to do the actual work.
Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com
Offline
I revised the script in post 16 to remove an unnecessary error handler and to cleanup the code a bit. I did not alter db123's dialog handler, which works great as written.
Applescript:
-- Revised 2022.02.28
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 -- select entire script if no selection
display dialog "A text selection was not found" buttons {"Cancel", "Select All"} cancel button 1 default button 1 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 error number -128
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
display dialog "The search string " & quote & searchString & quote & " was not found in the selected text" buttons {"OK"} cancel button 1 default button 1 with title "Search and Replace" with icon stop
else 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 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()
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline
This script works with Script Editor but is otherwise identical to that above.
Applescript:
-- Revised 2022.03.05
on main()
tell application "Script Editor" to tell document 1
set selectedText to contents of selection
set {c1, c2} to character range of selection
end tell
if c1 = c2 then -- select entire script if no selection
display dialog "A text selection was not found" buttons {"Cancel", "Select All"} cancel button 1 default button 1 with title "Search and Replace" with icon caution -- disable if desired
tell application "Script Editor" to tell document 1
set selectedText to contents
set {c1, c2} to {1, (count selectedText)}
set selection to {0, c2}
end tell
end if
set dialogResult to my showSearchReplaceDialog()
if dialogResult is missing value then error number -128
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
display dialog "The search string " & quote & searchString & quote & " was not found in the selected text" buttons {"OK"} cancel button 1 default button 1 with title "Search and Replace" with icon stop
else 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 Editor" to tell document 1
set contents of selection to modifiedText
set selection to {(c1 - 1), c2}
end tell
end main
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()
Last edited by peavine (2022-03-05 09:50:31 am)
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline