Find & replace inside clipboard text, each time I press cmd+c

Here's my wish:

Everytime I command+c any text (mostly a windows paths that contains backslashes), I wish the slashes automatically get changed to normal /-slashes.

So X:\whatever\stuff\path
I'll just select "\whatever\stuff\path", press command+c and I want to paste "/whatever/stuff/path"

How do I do that?

Tried this but it seems to cause an infinite loop:

Macro iconFix path slashes (Disabled)
Triggered by any of the following:
Trigger iconSystem Clipboard Changed
Will execute the following actions:
Action iconNotification “%ExecutingMacro%”
Copied %SystemClipboard%
Action iconSearch and Replace System Clipboard Using String (ignoring case)
Search for “\”
Replace with “/”
Store result to System Clipboard.
Notify on failure.

If you want it to happen everytime you copy something it is very easy.
The macro below will trigger everytime you copy anything and replace "" with "/".

But now it will happen no matter what you copy. Another way could be to make a macro which checks if the clipboard starts with "" and then only do the search and replace.
Or make another macro attached to V, which then does the replacing right before pasting.

Keyboard Maestro 8.2.4 “Replace / with / everytime clipboard changes” Macro

Replace - with - everytime clipboard changes.kmmacros (1.3 KB)

This also seems to cause an infinite loop, no?

I added a macOS notification, and it just keeps notifying:

14

Remember that you can cancel a macro from the KM status menu.

Or use a macro.

I have a macro which cancels all running macros set to ESC.

Keyboard Maestro 8.2.4 “Cancel all macros” Macro

Cancel all macros.kmmacros (1.7 KB)

Temporarily fixed it by setting trigger to ⌃⌘C but still wish I didn't have to do that

Use @JimmyHartington’s macro, but make it conditional:

  • If System Clipboard contains “\”
    • Search & Replace System Clipboard “\” with “/”
2 Likes

I would use even more stringent conditions, and verify that the posix path that would result from the edit does in fact exist, ie. it is confirmed to be a file path and not some other string that uses a backslash. An outline for the workflow might be:

  1. Clipboard changes to: X:\path\to\file.txt
  2. String replacement to variable:
    a. Find regex pattern: ^[A-Za-z]:\\.*
    b. Replace with: \\
    c. Replace all occurrences of "\" with "/"
  3. Set variable to the result of (2), ie. /path/to/file.txt
  4. Check value of variable is a filepath that exists; if so, set the clipboard to the variable's value
1 Like

In case it isn't clear yet, changing the contents of the clipboard within any macro will trigger the clipboard-change trigger that triggered the macro in the first place, resulting in the infinite loop. I've had the same problem recently. Peter's suggestion is probably the simplest way to resolve this.

1 Like

OK, so the clipboard trigger might not be the best one. But the main point of my suggestion wasn’t how the macro is triggered, but the strength of the condition used to test the contents of the clipboard. Therefore, I standby my recommendation, as it avoids affecting the clipboard contents when it is something such as "A \ B".

Anyway, it wouldn’t result in an infinite loop the way I have set it out. The clipboard is only changed if the contents contains a valid file path. If it doesn’t, no change is made, so the macro would terminate. If it does, the macro is triggered once more, but then the path is already in posix form, so no change is made. It runs at most two times.

I apologize. I think I extracted the wrong quote because I'm on pain medication for a toothache. As a result I'm right only 70% of the time instead of my usually 80%.

1 Like

I think, 66.6% would still be good enough. And even an average of 50.1% would clearly be better than not posting at all :grinning:


Edit/Note:

My intention is not to encourage people to post poorly researched or plain wrong information!

What I wanted to say:

If you have an idea how something could be done, or a possible explanation of something, then it’s better to post it, even if there are chances that your proposal/assumption is wrong (or there are better ones). Even if the solution is suboptimal – or doesn’t work at all –, or the explanation is wrong, it might still serve as inspiration for others to build upon.

Of course, if doing so, the idea/proposal/solution/assumption should be properly marked, for example with “I think,…”, “Have you tried to…”, “I would…”, “Maybe…”, “One possibility is that…”, etc.

1 Like

Awesome. Thank you all!

I'm glad to meet someone who understands statements about probability. However if I said I was right 49% of the time or even 20% of the time, that would be just as useful as being right 51% of the time or 80% of the time. The only useless number is 50%.

I still have a toothache so if I'm wrong, chalk it up to that.