Feature request: Token for Finder Selection


I just recently found the token %FinderInsertionLocation% and with that I thought there would be a token which returned the Finder Selection.

We have the action to open the Finder Selection, so KM already knows about what is selected.

So it could be nice to have a text token for this as well.

Maybe like %FinderSelection%

I know of the For Each with the Finder Selection but in some cases it would be I could use just the text token.

There is no token for FinderSelection because there is potentially more than thing selected.

Hence, Finder Selection is handled by the Finders Selection collection in the For Each action.

I suspected something like that would be the case, since it was not there.

My idea for the token came when I saw the Open Finder Selection action. Which also opens multiple selections.

I some times make a macro, where I just select one file and want to capture it in KM and the For Each always seems a bit “big” to use for such a small task.

If you know there is exactly one thing selected, then you can just run the For Each action and leave the variable as it is (which will be the last in the list).

But at this point, why not just support multiple selections - for most use cases, you'll then get more functionality for free.

Keyboard Maestro Actions.kmactions (846 B)

1 Like

Well, @peternlewis is one approach, but it has the following limitations:

  1. Results in the LAST item selected if the Finder has multiple selections
  2. No error checking -- returns blank (but NOT empty. why is this?) if nothing is selected in the Finder.

I started out trying to handle these in KM actions, but stopped after I got to this:

and I found that the test for EMPTY variable was failing when there was no Finder selection. @peternlewis, it looks empty, but what character(s) does it actually contain, and why?

So, I reverted to my simple AppleScript solution:

Example Output

With Multiple Items Selected

With NO Items Selected

##Macro Library   Get First Item of Finder Selection via AS [Example]

<a class="attachment" href="/uploads/default/original/3X/3/0/30f550023454376e6e46d1858efd4feeb8f50670.kmmacros">Get First Item of Finder Selection via AS [Example].kmmacros</a> (3.2 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**


<img src="/uploads/default/original/3X/2/4/2477c483b9ead7d19f9cc26a7228ac3c6ce9c6a2.png" width="469" height="738">


### AppleScript to Get First Item In Finder Selection

tell application "Finder" to set finderSelectionList to selection as alias list

  if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
  set scriptResults to POSIX path of (item 1 of finderSelectionList)
on error errMsg
  set scriptResults to ("[ERROR]" & linefeed & errMsg)
end try

return scriptResults


Local__FilePosixPath will never be empty in the If condition because the For Each action will execute its actions once for each selected item, and Local__FilePosixPath will be set the path of the selected item. It will never be set to empty. If there is no items selected, it will never be set at all and the If action will never be executed.

OK, thanks for the clarification.
But to further clarify, it is not about the IF action. If there are no Finder items selected, then none of the For Each Actions will be executed.

So, now that I better understand the behavior of the For Each, my macro can be simplified to this. The key is setting the KM Variable Local__FinderFirstItemPath BEFORE the For Each block.