Copy file paths of folder contents Macro (v11.0.2)

I have several folders, each of which contains a number of different file types. Each folder will always contain a file with the extension '.shp'. I need to copy the full path of each .shp file simply by selected its parent folder.

The attached is as far as I got with trying to copy the paths of the folder contents. But it does not work. Any help appreciated.

Copy file paths of folder contents Macro (v11.0.2)

Copy file paths of folder contents.kmmacros (2.2 KB)

Update for the benefit of any other readers: as spotted by @Nige_S, to ensure that the macro works with all folder paths, $KMVAR_local_item should be in quotes. The updated shell command is therefore:

ls -R "$KMVAR_local_item" | grep -e ".shp$"

The original post follows.
ā€“ kevinb @ 2024-12-20 13:54 UTC


Although you didn't ask for a solution that would work when more than one folder is selected, I thought you might need that so I thought along those lines. Rather than use nested For Each actions, I thought it less confusing (for me at least!) just to let the shell handle things.

The For Each here is so that you can select any number of folders at once, rather than just one.

The shell script is:

ls -R $KMVAR_local_item | grep -e ".shp$"
  • ls -R = list recursively (so it will look into any subfolders).
  • $KMVAR_local_item = the variable name local item wrapped up so it can be used by the shell.
  • "|" = pipe the result of that to grep.
  • grep -e = look through that result for anything that matches the regular expression pattern.
  • ".shp$" = the pattern matches any filename ending in ".shp".

In order for the list of ".shp" files to be properly formatted when more than one folder has been selected in the Finder, you will need to change a setting in the Execute Shell Script action. Go to its cog and disable "Trim Results".

If you don't like the look of all that, I'm sure someone can provide a solution that does not involve the shell, but it seems that I'm not in that mode right now. :wink:

1 Like

You called, Sir?

This is also recursive, putting a linefeed-delimited list of paths onto the System Clipboard.

Set the "For Each" to anything but "recursively" if you don't want to check subfolders, and if you only want (or will only ever find) one ".shp" file then change the "Append Variable" action to "Set System Clipboard to %Variable%Local_item%", follow it with a "Break from loop", and delete the last action.

List .shp Paths.kmmacros (3.9 KB)

Image

Easy shell version that includes files in subfolders would be

List .shp Paths (shell version).kmmacros (2.1 KB)

Again, this might not be a concern, but the use of %FinderSelection% means that exactly one folder should be selected in the Finder.

The %FinderSelection% token returns (v10.0+) the path of the currently selected file in the Finder. If there is none or more than one file selected, then it returns the empty string.
ā€“ token:FinderSelection [Keyboard Maestro Wiki]

This is why I mentioned nested For Each actions. The token %FinderSelections% (sic) could then be iterated through.

Yes need to be able to select multiple folders. Attached only works if one folder selected.

Cannot seem to get attached to work. I get error 'No such file or directory'

Edit I think it is working, but it is copying the name of every single .shp file stored elsewhere in the parent directory. This is resulting in 3,500 lines.

I need it to only look inside the contents of the selected folders, which each contain a single .shp file.

In theory that's impossible if you have selected the right folder.

The for each is set to use "the Finder's selection".

Can we just check some basics first, just to rule them out?

  • You are selecting the right folder(s) and then triggering the macro?
  • It definitely is this version of the macro that is active and being triggered?

Then you need to re-state your problem, because in your OP you said "by selected its parent folder", singular.

So do you want to:

  • For one or more folders selected in the Finder
  • List the path for every file ending in ".shp"
  • And put that list on the System Clipboard

...and if so do you want to restrict the search to those selected folders or include their sub-folders too?

Start by wrapping the shell script's variable in double-quotes, in case you have spaces in your file paths: ls -R "$KMVAR_local_item"...

1 Like

Ah, yes, that will be it!

This works now thanks

1 Like