Get File Name of Finder Selection (Not Filepath)?

How can I the name of the file in the finder's selection? I don't want the whole file path, just the name of the file itself.

You can use either the Get File Attribute action, or the Split Path action. Here's an example of the former:

Be aware that this includes the extension as well. If you just want the file name, you'll need to get the base name instead.



1 Like

I tried the solution just for fun. Maybe I can use it in the future. When I select a file, it works. But when I select multiple files, it always shows the last file. What am I doing wrong or what do I have to change to make it work for multiple files. I used "Display Text in Window" at the end. Sorry for such a question, but I want to understand Keyboard Maestro.

I suspect you're putting the Display Text action in the wrong place. Try moving it inside the For Each action like this:

KM 0 2020-09-01_00-00-52

You'll then see a window displayed for each of the files you've selected.

The For Each action is a loop that performs the same set of actions on every item contained in its collection. If those actions only consist of getting the file's name to the same variable, then the variable will end up containing the name of the last file in the collection when the loop is done. If you want the loop to make a list of all the files selected (or in a folder if you choose that collection) you need to append each file's name to the same variable:

1 Like

@tiffle @gglick

I wanted that if I mark e.g. 5 files in the Finder, I would get a list with the 5 file names. But I always got only the last file name displayed. So I added the "Display Text" action to the "For Each" action. After that I got 5 windows with one file name each. The action "Append Variable with Text" was unknown to me. But by doing so I was able to solve the problem.

Thanks to both of you for the answer.