If pasted clipboard content is Finder items, reveal them?

I'm trying to perform additional steps in my "paste" macro if the clipboard content is items (files, folders, aliases/symlinks) copied to the clipboard in Finder. I searched and found that I can check if the clipboard %SystemClipboard% conforms to public.file-url in an If Then Else action. But I can't seem to figure out how to reveal/select the files I copied in the target folder location after pasting them. I've tried using %FinderInsertionLocation%%SystemClipboard% in the Reveal a File action, but it doesn't seem to work.

Ideally, I'd like to be able to make this work so that any/all copied Finder items that I am pasting into another location will be revealed/selected in the target location in Finder after pasting them. But somehow I'm missing a step where I can parse the clipboard data to get just the name of each file, prepend it with the target path, and then append that to a variable that I can later pass to the Reveal a File action to select any/all file(s) that I pasted.

The last step will be checking to see if any pasted files exist in the target location, and if so, selecting the renamed copy of the file (usually formatted as filename copy.extension or filename \d+.extension). But I can wait to figure that part out until after I get the first bit working.

Hopefully this is clear enough to get the sort of help that I need to get this working. Thank you to anyone who can help me figure this out!

1 Like

Multiple select some files in the Finder and copy to the Clipboard. Then use Keyboard Maestro's Display Text in a Window to show %SystemClipboard% You will see that it already contains a list of the copied filenames without you having to do anymore. Something like:

untitled folder
Untitled Project.psd

If there is just one copied file or folder then your idea of %FinderInsertionLocation%/%SystemClipboard% will work if used in a Reveal File Action. (You had missed out the "/")

For doing stuff to multiple selected and copied files one at a time, you would need a For Each Action to iterate through them.


If there is just one copied file or folder then your idea of %FinderInsertionLocation%/%SystemClipboard% will work if used in a Reveal File Action. (You had missed out the "/")

Actually, when it's one file, it retains the path it is copied from, so i still can't combine that with the target path to reveal the file without using a regex replace to strip the path.

Also, the %FinderInsertionLocation% already ends in / and the %SystemClipboard% begins with one (beginning of the path to the source file being copied) so it won't work this way either.

If you copy an item in Finder to the Clipboard with ⌘C, the token %SystemClipboard% token does not hold the path. It just holds the filename.

No they don't. You can test this by selecting a single file or folder in the Finder, copying it with ⌘C and running this test Macro:

Tests for contents of tokens.kmmacros (2.8 KB)

You will get something like this:


If you select and copy multiple files/folders in the Finder and run the Macro you will get something like this:


I find using the Display Text in a Window Action is one of the best ways to debug a Macro. It will show you the actual contents of Variables and Tokens. More times than not it will show why a Macro is not working as expected.

1 Like

Interesting. I'm not sure how I did it before, but I was seeing the results I described when trying to copy one file and paste it into a new location when writing %FinderInsertionLocation%%SystemClipboard% to my debugLog.txt file (easier than popping up a new window and potentially stealing focus). I think it might have been due to trying to perform a regex find/replace on the %SystemClipboard%, which may have converted the clipboard data to the plain text string of the copied file's /path/filename.extension.

Now I need to figure out how to parse the data when copying/pasting multiple Finder items so that I can prepend the %FinderInsertionLocation% path to each filename in the %SystemClipboard%. Any ideas on how to accomplish that?

Okay now I'm really confused. I just tried the same thing again, and this time it did what I was describing previously again.
Variable Inspector:
Screen Shot 2023-07-29 at 2.50.51 PM
From my macro:

From my debug log file:
Screen Shot 2023-07-29 at 2.51.46 PM

Could this be due to a KBM version issue? I'm on macOS Sierra 10.12.6, and running KBM 9.2.

This is really weird. I just used the same macro twice in a row, and got different results each time. The only difference is that, between copying the file from one location and pasting it into another, I selected and deleted the file I previously copied into the target location before pasting the one I just copied into it again.

When doing the copy/paste without deleting the existing copied file in between, I got the desired result you described the first time. But after attempting the same operation again several more times, copying and pasting different files, I got inconsistent results each time. Sometimes it worked and the %FinderInsertionLocation%%SystemClipboard% string was correctly formatted for the new file in the target location. But for some strange reason, more often than that, the string contained both the %FinderInsertionLocation% and the path and filename rather than just the filename for the copied file in the %SystemClipboard%.

Mind you, this is occurring despite the fact that the Value Inspector is showing only the filename for %SystemClipboard% - both after copying the file, and after pasting it into the target location.

@peternlewis I wonder if this might be a bug with KBM 9.2, or maybe specifically with macOS 10.12.6, or maybe due to some other factor?

EDIT: I just tested something else that changed this behavior. I disabled my _G_Clipboard:CopyTo macro, which triggers from ⌘C. Upon doing so, when copying files in Finder, then using the Tests for contents of tokens macro shared by @Zabobon, when viewing the Display Text window, the %SystemClipboard% doesn't contain the file path - only the filename. But when I use my macro to copy the file to the clipboard, despite the Value Inspector showing only the filename.extension for a copied file, the text displayed in the window shows the %SystemClipboard% as containing /path/filename.extension.

Here's the relevant portion of my _G_Clipboard:CopyTo macro when copying files in Finder:

After some further testing, here are some results from the Clipboard History Switcher.
Before disabling/when copying files to the clipboard using my _G_Clipboard:CopyTo macro:

After disabling my _G_Clipboard:CopyTo macro, natively copying files to the clipboard using ⌘C:

So my suspicion is that there may be some issue with the KBM Copy action. I'm going to try replacing it in my macro with sending the ⌘C keystroke instead and see if I get different results.

Still the same inconsistent results using ⌘C in my macro. Sometimes the copied file shows in the Clipboard History Switcher as an image (the file icon), other times in the /path/filename.extension format as text. Despite this, each time the %SystemClipboard% token in the Value Inspector will only show the filename.extension without the path. But when used in the Display Text window action, or referenced by other actions, it will either use/display only the filename.extension (when it shows in the switcher as an image), or as the /path/filename.extension (when it shows in the switcher as text).

Furthermore, I've now also observed that, even when my macro is disabled and I'm using only the native Finder ⌘C shortcut to copy to the clipboard, occasionally, the Clipboard History Switcher will still show the copied file as a text string with format /path/filename.extension, while most of the time the copied file shows in the switcher as an image. And even in these cases, whenever the copied file is shown in the switcher as a path/filename text string, the Display Text window will show the /path/filename.extension for the %SystemClipboard% token, and it will be used as such when referenced in other actions.

I can't replicate the inconsistent results that you are getting.

Forgetting about Keyboard Maestro for a moment, if you copy an item in Finder using the Finder menu item Edit>Copy and then paste as plain text (using Option + Cmd + Shift + V) into some document you will always get the plain text of the filename with extension only. If you multiple select files and copy, then paste as plain text you will get a list of those files. This is standard macOS behaviour.

In my experience, the token %SystemClipboard% holds the plain text version of the current clipboard and if the clipboard contains copied files/folders or single files/folders copied it will be a list of their names without paths.

If you are sometimes getting the full path as well, I think something else must be going on when you do the copy.

To temporarily test out what the token %SystemClipboard% holds after you copy an item/s in Finder you could also try this Macro. Remember to disable it after you have done some testing!

Tests for plain text of clipboard.kmmacros (3.2 KB)

1 Like

If you copy a set of files in the Finder, then the plain text representation of the clipboard will be the file names, one per line.

For a single file, you could possibly use Reveal %FinderInsertionLocation%/%SystemClipboard%.

For more than one file, that wont work, and indeed Keyboard Maestro has no way to revel a set of files directly so you would beed to use AppleScript for that.

See: manual:Whats New [Keyboard Maestro Wiki]

Changed in 10.0.2

Ensure FinderInsertionLocation and FinderSelection(s) tokens return paths without trailing slashes.

Keep in mind that a clipboard contains many different flavors. It is entirely plausible to have one flavor that has only the file name and another that has the full path.

Use the Filter action to Remove Styles which will replace the clipboard with strictly the plain text, or use the Set Variable to Text action to read the SystemClipboard token which will always read just the plain text, and that should get you consistent results (unless the source application is actually giving inconsistent results, which is always a possibility).

The Copy action is simply:

The Display Text (and some other actions) will use the styled text of the clipboard which may be different text to the plain text of the clipboard.

Technically, the SystemClipboard token does not “hold” anything. It references the system clipboard and it will read either the plain text of the system clipboard or the rich text of the system clipboard depending on the context it is in.

Better would be:


Which will display both:

Note that in my tests in Ventura, the Finder gives the same results for both plain and styled text, but that might change from version to version.

1 Like

I'm still seeing this odd/inconsistent %SystemClipboard% behavior, despite following the recommended steps. It doesn't seem to be a styled vs. plain text issue. It's simply that, when copying a single file in Finder, the path is included (consistently lately, at least for today/currently) with the filename in the %SystemClipboard% token - but only wherever it is used in another action. Within the Value Inspector window, only the filename is shown for the token. But if I use the %SystemClipboard% token in a Display Text action, Append Variable with Text action, or any other action, the file path is included.

After some additional testing of this script (after adding an AppleScript action to more reliably close the previously-opened Display Text window, instead of potentially closing the Clipboard History Switcher), I noticed that the script is now actually updating the clipboard format (flavor?) of the copied file to plain text only (other flavors removed). However, rather than leaving just the filename, %SystemClipboard% still contains the path and filename - both before and after removing styles and filtering the %SystemClipboard%.

After copying the file to clipboard, before running the test script:

After running the test script:

If the Value Inspector were showing the path and filename for the %SystemClipboard% token (the way it appears whenever used in other actions), or if the Value Inspector somehow indicated that the token's contents were a text file (or whichever other flavor/format may be in the clipboard), it would be less confusing from an end user standpoint.

As a workaround/stopgap to prevent potential error when a single file is being copied which may contain a file path, I suppose I can use the Search and Replace action to strip any file path characters from the clipboard when copying files in Finder.

What the Finder copies is up to the Finder, so I would suggest your best course of action given the inconsistencies you are seeing is to deal with the inconsistencies - remove the path if it exists using Search & Replace for (?m)^.*/ and replace with ``. that should work whether the path is included with one file or all files if I've got my regex correct.

1 Like

Thanks, Peter! seems that's the way to go. although I'm using a more elaborate regex to separate file names and extensions so that, later on, when I expand this macro to account for pasting items into a folder where items of the same name already exist, I can handle these cases and still select the pasted items even after they've been renamed to resolve file name conflict with the existing files (by adding " copy", or " 1", " 2", etc between the filename and extension, or at the end of the name for folders/files without extension)

A bit late,
But just to make sure you all understand an important detail about the clipboard:

The clipboard can hold the same information (here: file references) in multiple formats, and usually does, too

A common format it "plain text", snd even that exists in multiples, ie. I. UTF-8, -16 and classic MacRoman formats. By default, KM always picks the UTF-8 one, as a single text string.

But in your case, another format (aka flavor) is present: file-url. That one is an array of single paths in URL style. Then there is also the one that contains all paths in plist format.

Ideally, you'd use one of these to get your paths, betthey should work reliably across many apps that support files and selecting them.

Only, I don't know how to access those formats in KM, but I have written other tools that handle these clipboard items (ShowClipboards, iClip, see apps.tempel.org).

(Tyoed crappily on an iPhone)