Find and Replace in Selected Text

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.

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.

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

1 Like

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.

1 Like

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

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

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.

I'm trying to do something similar, but I can't get this example to fully work either. The last step, sending the ⌘V , sends the PREVIOUS clipboard contents. I've tried adding in manual delays between various steps thinking it's a timing issue, but that doesn't help either.

If I remove the ⌘V step, then manually hit ⌘V after running the script, it does what I would expect.

I'm testing with exact same script/action as Andreas's example above.

BTT 2.717 on Mojave 10.14.4.

Any thoughts?

2 Likes

I am having the same issue where having ⌘V at the end uses the previously copied item. A bug, perhaps?

1 Like

I was trying to do the same thing. I found the following shell script to decode HTML which looks pretty simple, but don't know how to make it work with the selected text?

cat foo.html | perl -MHTML::Entities -pe 'decode_entities($_);'

Has anyone ever figured out why the paste step used the PREVIOUS content and found a solution / workaround ? I believe I have a related issue with variables having prior content in them...

2 Likes