[SOLVED] Abbreviate a File Path in the Clipboard with Tilde ~/ i.e. (Unix $HOME-path notation)

Whenever I copy a path from Finder, it always defaults to /Users/dannywyatt/NameOfTheFile, but I like to always use the ~ instead of /Users/dannywyatt
Is there a way for KM to always modify this so whenever there's a clipboard entry that contains /Users/dannywyatt it replaces it with ~ ?
So if the clipboard is
/Users/dannywyatt/NameOfTheFile
KM automatically replaces it with
~/NameOfTheFile

One of the options in the Filter action is:

  • Abbreviate with Tilde in Path
1 Like

Thank you. I never used the Filter action, so I'm not familiar with it yet. Can you share how to achieve it?
I was reading this article (action:Filter [Keyboard Maestro Wiki]), but it doesn't show a real life example with a macro, so I can't really understand how Filters work and how they can be used...

Abbreviate user home part of filepath with ~.kmmacros (2.8 KB)

2 Likes

Thank you for sharing, but I think your macro was a bit confusing (without proper clarification of what each action was doing) and it took me a while to understand what I actually needed and what I didn't.

Basically what I needed to make this work was to disable your 1st and 3rd actions and then at the end just add a Keystroke action with the CMD+V trigger:
image

Thank you for your help :slight_smile:

1 Like

Sounds like a good division of labour :slight_smile:

( Experimentation is where and how we learn these things )

2 Likes

A potential problem with your solution is that it is going to apply the filter every time you press ⌘V in Keyboard Maestro - whatever you are pasting.

I approach this task the other way around. I have a Macro that copies the selected Finder Item's path to the Clipboard as a Tilde Abbreviated Path ready to be pasted anywhere.

KM Path as Tilde.kmmacros (16.5 KB)

Click to Show Image

And I have used Keyboard Maestro's new ability to add Quick Actions to the Finder Context Menu:

Click to Show Image

Which leads to it showing up like this in the Finder (so, I don't have to remember a shortcut).

Screen Recording 2022-08-16 at 15.29.36-Animated GIFF 640 12fps

5 Likes

Just for fun, here's another idea. If the clipboard contains your user path, ⌘V will prompt for the option to paste either the full or abbreviated path. Of course, you could also adapt this to simply paste the abbreviated path with its own hotkey, but I thought it would be nice not to have to remember another hotkey.

Full Paste - Tilde Paste.kmmacros (23 KB)

Macro screenshot

1 Like

Is this a real "problem" in the sense that I will eventually experience some kind of issues?

I also don't want to have another shortcut for "Paste" if I don't need to.

The Finder Quick Action is not useful for me, just because I want this to be exclusive to KM. If I end up migrating to a new computer and I pick another username, I want all my macros to still work. That's the only purpose. I rarely need to paste the path with the tilde outside KM.

But I enjoy seeing other approaches and looking at these macros I can see other useful things that can be used for other scenarios. Thanks for sharing!

That's another option for sure.
In my case, I only want this to be abbreviated inside KM and that's the only format I want to use, so having to choose every time I paste a path with users/dannywyatt, would be extra/unnecessary steps.

Do you see any issues why my approach of just having that filter and the keystroke? Would that be a problem in a specific scenario or something?

Potentially, if you start referencing paths in shell scripts, yes. I've had to state full paths on occasion for them to play ball.

It's not like you're pasting user paths every five seconds, so if I were in your position, I'd allow myself the flexibility.

And since more options are fun -- here's a Text Expansion version. Copy a path to your clipboard and, as set, ;~; will paste that path in abbreviated form while ;/; will paste the absolute path.

Munge Path from Clipboard.kmmacros (3.1 KB)

1 Like

If the clipboard contains an image, the Macro will fail with an error message. And interestingly, the Filter Acton doesn't have a setting to disable that failure.

Sticking with your Macro, a way to avoid that is to add in an "If Then Else" check to make sure the Clipboard contains a path before applying the filter. You might at least check that the Clipboard contains "/". There are probably more thorough checks but this was the first I could think of.

Click to Show Image

To be honest, I'm still not at the point where I even know anything about shell scripts, so I think that wouldn't be a problem, but in case I do need those, I can always remove the tilde and add users/dannywyatt. Not a big issue.
Thanks for clarifying that

How would the clipboard have an image, if I cam from Finder where I selected to copy the path...? I'm confused...
Also, this filter is just acting on paths that contain the full path and converts it to the abbreviated version, right? Can you clarify?

Maybe you might be copying an icon from one Keyboard Maestro Macro to another? Or copying and pasting an Action. Remember that this Macro will fire in Keyboard Maestro every time you press ⌘V.

The Macro doesn't know that you have just been in the Finder. All it knows it to apply the filter every time you ⌘V paste in Keyboard Maestro.

1 Like

But isn't the filter just changing the path? An image doesn't have a path... or at least, not that kind of path.

I just tried it and when I have an image on the clipboard, the filter shows that it doesn't have text:
image

I was able to copy and paste an image without any issues.
Have you tried this yourself and noticed that it didn't work, or are you assuming that it could happen?

Yes, what I mean is that whenever I need this kind of behavior, I'm in the middle of a certain workflow. It's not that I copy the path and go do something else (such as copying images). I copy the path and immediately paste it inside KM where it needs to go.

But let me know if you are able to copy-paste images as well, because I was and no issues at all.

I unleashed monsters over here! haha
So far the simple solution with 3 actions, seems to be enough, though.

I'm a bit confused about this...
If the path already has the ~ why would we need to have the filter? The same for the / version.

The idea is that all paths that contain /users/dannywyatt/ are converted to ~
Nothing fancy, just super simple conversion.

We're (ab)using the fact that a ~ path won't be further abbreviated and a / path won't be further expanded to create a macro that will insert the path format of your choice regardless of the format on the clipboard. Saves on having to check which type of path there is and then deciding what to do.

And this makes it more generally useful -- you can expand or abbreviate according to your need, using two easily-remembered "switches". So at the moment it is super-simple: copy path to clipboard, switch to KM, set insertion point, type ;~; and you're done. But if you ever need to go from abbreviated to full path anywhere you just need to remember ;/;.

So simple to use now, still simple to use as your needs expand -- but that versatility often requires a little more complexity in the macro itself.

I also agree with @Zabobon that it would be improved with some error checking, if only to give a nicer notification than "Macro failed..." when something else is on the clipboard.