Open File With Name Matching Clipboard?

This seems like it must be quite basic, but I've searched and couldn't find an answer.

I'm trying to use the current clipboard to open a file whose name matches, within a specific folder. Am I close?

...or is this better?

Hi, Neil

The second works for me (assuming you want to open every matching file). If it's not doing what you wanted, what's going wrong? Could you provide some sample filenames, and say what happens and what you were expecting?

I can't see how the first would work, as the "If" block isn't within the "For each" loop - so it would only work if the last file in the directory happened to match.

Best wishes,

Hi Steve,

Thanks for helping out. Ok, so I'm running OCR on the bottom of this plugin window as an example:

That copies "Scheps Omni Channel (m)" to the clipboard. When I pasted it here just now, it automatically entered a line break, so I'm not sure if the OCR/clipboard is adding one. Perhaps that's interfering with things?

Here's the example script I'm trying to open, in the named folder:


When I run the second version of the macro, nothing happens. It's worth noting that if I try to rename the script by pasting from the clipboard, I get this error:


If I hit backspace before hitting enter, it removes the line break and renames the file. So maybe it is related to the line break? I wonder if there's a way to remove that from the clipboard prior to searching?

You're very welcome.

I like your approach - I haven't used OCR much, but this seems like a great application. So could you...?

  • Transfer the clipboard to a variable (Set Variable "PluginName" to "%SystemClipboard%" - or similar). You could work entirely with the clipboard, but I find variables simpler (and prevents anything untoward happening if you accidentally press CMD-C whilst the macro is running)
  • Transform it. I think "Filter Variable PluginName" (or Clipboard, if you prefer) with "Trim Whitespace" to "Source" should be all that's needed. If that doesn't work, let me know.
  • Use the variable (or clipboard) for the comparison with the 'Path' variable.

Best wishes,

Test trimming clipboard text.kmmacros (1.7 KB)

Clever stuff! The notification shows me that the line break is gone. Nice.

For some reason it's still not opening the file. Hmm...

Never mind... I changed 'matches' to 'contains' and now it works!!!! Thanks so much!

Good idea - contains will be simpler.

Using match would have meant that the brackets around the '(m)' in the plugin name would have been treated as regex tokens (unless they were escaped with a backslash), not regular characters.

1 Like

Great knowledge! Thanks again.

1 Like

Hey Neil,

You don't need to iterate through the paths in the directory – all you need to do is form the complete path.

This one is pure Keyboard Maestro actions:

Open a File Name from the Clipboard in a Given Directory v1.00.kmmacros (7.3 KB)

(I have not trimmed whitespace in this one.)

For my own use I'd compact things a bit:

Open a File Name from the Clipboard in a Given Directory (Shell) v1.00.kmmacros (5.8 KB)


I must be doing something wrong, because I tried KM action version (I have no idea about scripting) and couldn't get it to work for me for some reason. I understand the thought process, and it makes perfect sense, but I'm happy with the other method which works every time, so all good. :slight_smile:

The DEBUG statement is in there, so you can pop it open and eyeball the path you've assembled.

For the macro to NOT work the path must be inaccurate in some way.


I figured it out. You have to manually add a / at the end of the local_SourceDir path and the OCR doesn't include the .app extension so it doesn't make the full path that way, so for this particular application, the 'contains' part of the other macro helps get around that.