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)
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.
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.
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.
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.