Find and Replace in Selected Text


#1

I commonly use the same find and replace for plain text. Is there a way to select text and hit a trigger in BTT to start up a simple find and replace in the selected text?

My specific situation has me converting html encoded URLs back to decoded URLs hundreds of times a day.


#2

Unfortunately I haven't gotten to that feature yet, it has been on my todo list for quite a while. I hope this will become possible very soon.


#3

With Automator you can however relatively easy create a quick action that does this:

You can then assign a keyboard shortcut to the quick action in System Preferences =>Keyboard => Shortcuts and trigger it in BTT if you want.

(For some reason it's necessary to concatenate an empty string with the input first, otherwise it will throw an error.)


#4

It also works using BTT when doing it like this:

The shell script action would be configured like this:

osascript  -l JavaScript <<\EOF

var BetterTouchTool = Application('BetterTouchTool');
BetterTouchTool.includeStandardAdditions = true;

var result = BetterTouchTool.get_string_variable("selected_text");

result = result.replace("test", "replacement");

BetterTouchTool.setTheClipboardTo(result);

Instead of the replace, you can also use the JavaScript url decode/encode functions.


#5

Thank you for your quick reply. I was not able to get this working. I did find this page from Apple, do you think this might hold the key to the answer?

https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/EncodeandDecodeText.html#//apple_ref/doc/uid/TP40016239-CH51-SW6


#6

When I replaced the text with the encoded text (eg: & a m p ; ), there seems to be an issue with the semi-colon.


#7

I was able to solve this issue by using TextSoap. I applied a keyboard shortcut that is triggered by the touch bar. It seems to work flawlessly.