How to check if clipboard contains file path?


I wrote a macro to reveal in Finder the file corresponding to the path in the clipboard.

I would like to start the macro by checking that the clipboard indeed contains a Unix path, and displaying an error message if not.

thank you very much

Reveal File with path in clipboard.kmmacros (25.4 KB)

One foolproof (I believe) way to do this is to make sure the clipboard contains text, and that the text there is a valid path by checking it in the shell, like this:

Download Macro(s): __Does path exist.kmmacros (5.8 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 13.6.5
  • Keyboard Maestro v11.0.2

This macro is set up to run whenever the clipboard changes, which may not be what you want—if not, just copy the if/then into whatever macro you want to use it in.

It works by making sure that the clipboard contains text, and then tries to ls the contents of that text. If the text is a valid path (to a folder or a file), then the ls command will work. But if it's not, the output will contain the text no such file or directory. So if the output doesn't contain that text, then the clipboard contains a valid path.

In theory, if you have a filename that includes a file named " such file or directory," then this macro would incorrectly state the clipboard does not contain a path for that file. You could make this more robust by using a regular expression to find the words in the right spot in the output string, but really, that seems like a needless complication.

There may be downsides to this approach that I haven't considered, as it was the first thing that came to mind when I thought about it.



Security is a downside. You are passing to the shell a string that could contain anything. Something in the paste buffer could be a shell command that could wipe out your hard drive in less than a second.

1 Like

thanks very much. I installed your macro at the beginning of mine, but disabled it until you get a chance to answer @Airy 's comment.

Good point ... but wouldn't this fix that problem: Change the shell command to:

ls "`pbpaste`"

That would enquote the passed string, which would prevent appending (for instance) a | rm * command to the end, as it would get interpreted as part of the path. There are probably some ways around that (quoting the quotes, somehow?), but this is an exposure with any shell command you run that accepts a passed string.

You could further sanitize it, I guess, by checking that the clipboard does not contain certain characters (pipe, backticks, quotes) that are unlikely to be in directory names?


1 Like

thank you very much.
Just out of intellectual curiosity, could you simply ascertain that the clipboard contains at least two "/" because it is very uncommon for me to write "/" twice in a non path string?
Something like this, but for two or more "/"

You could, using a "matches" condition and a regular expression:


It wouldn't be completely foolproof, of course, but should work for your use case. (And it would avoid the possible security issues in the shell.)


1 Like

thanks again very much
You are right about shell scripts in that I already have so many of them in my macros.

Again out of intellectual curiosity (this problem is solved), how would you apply a regex condition to the clipboard with the if then else action ?

I don't see an option to do so under if then else → clipboard

thank you


As noted above, use the "matches" condition.


I agree with you on each point. Moreover, the chance is pretty small that a string would do damage, although you added such a string in your post! You are braver than I am.

But where I disagree is that I don't think you need to pass this data to a shell command to get the answer. The question was "I would like to start the macro by checking that the clipboard indeed contains a Unix path, and displaying an error message if not" and I think there are safer ways to solve that. For example you could use this command:

I didn't test this to confirm it, but if you use that action, I think it would harmlessly attempt to examine the file, and if the string is a valid path, the action will succeed, but if the string is not a valid path, the action will fail. If I'm right, this would be a 100% safe method to use. It could work like this:

Just make sure you turn on the "Failure aborts macro" flag for the Get Date action. It is off my default.


I am no expert in Mac path structures, file/folder naming rules, nor in Regex, but the Regular Expression bellow should remove some possibilities for false positives. And if I am thinking correctly – and not overlooking something significant (something I might very well be doing, so anyone do chime in if this is the case) – I believe there shouldn't be many possibilities for false negatives here either:


Apart from assuming your files will either be in a volume or in one of the four default top level directories of your startup disk, I believe the expression above to be quite conservative though, and it could probably be limited allot more. For instance by lowering the 255 character length cap in folder or file names to something resembling more sensible naming conventions, or by adding more characters to exclude in the [^ ] character groups.
Or if you for instance know you'll only be wanting to check file paths either in an external volume or your Users Home folder, you could set the condition within KM to match something like this:


I think this can be done very simply using Keyboard Maestro native Actions. If the clipboard contains anything other than a valid path to the file to be revealed it will show the warning, otherwise it will reveal the file.


bravo ! works perfectly. thank you