Search and Replace Shortcut

The Shortcuts app does not have the ability to do a search and replace in an action (that I could find), so I wrote a shortcut that performs this task. A few comments:

  • The open source swiftDialog app is required.

  • The shortcut must be run as a keyboard shortcut, Service, or Quick Action. It works on text selected in the action. It will work with some apps other than the Shortcuts app (e.g. TextEdit, Script Editor, and Script Debugger).

  • Regular expression are supported, but the following characters have to be escaped to be treated as literal characters: * . ? + [ ( ) { } ^ $ | \. The support for regular expressions can be disabled in the Replace Text action.

  • The shortcut has an intermittent bug that causes the dialog to move and reappear when the OK button is selected with the mouse. This doesn’t happen when the Return key is used instead of the mouse.

Search and Replace.shortcut (23.8 KB)

This shortcut is the same as that above, differing only in that it notifies the user of the number of search string matches before proceeding:

Search and Replace.shortcut (24.7 KB)

BTW, the above shortcut contains numerous Run Shell Script actions, and I suspect only one is really needed. I’ll work on that later.