Hello, I want to make a script that will be available in folder’s contextual menu in Finder. I want the script to be valid only for folders, and to receive the folder that it was invoked from as its input. How can I achieve that?
Numo. I’m not aware of any direct method to do exactly what you want, but it’s easily done with a shortcut by way of the contextual Quick Actions submenu. You can then run the AppleScript from within the shortcut (best avoided if possible) or you may be able to do what you want with the shortcut itself (best approach). The Shortcuts app requires macOS Monterey or newer, though.
The following shortcut is a simple demonstration of the above. To test this shortcut, download and install (it’s easily deleted). Then select and right-click on a folder in Finder and select Get Folder Path in the Quick Actions submenu.
Get Folder Path.shortcut (21.6 KB)
What I want to do is iterate through every subfolder in the folder and flatten the file structure to open all the files in the folder at once. I don’t know if you can understand what I want to do. The usecase is to play all files in IINA, disregarding subfolders.
I don’t have IINA for testing, but the following shortcut opened PDF, TXT, and SCPT files on my Sonoma computer. My test folder was four subfolders deep and opened all files with their default app.
Open Files.shortcut (22.2 KB)
It opens the files only from the first subfolder. Thank you so much for your help
Nuno. I downloaded and retested the shortcut, and it opened all files in a folder four subfolders deep. There’s no way for me to know why it doesn’t work for you.
The shortcuts app has an oddity that might be encountered in the use of the shortcut included above. The Get contents of folder action–which is used in the shortcut–returns files if the recursive option is enabled and both files and folders if not. As a result, disabling the recursive option breaks the shortcut.
The only way to fix this, that I’m aware of, is a repeat loop which checks whether an item is a folder or not. This approach is incorporated in the following shortcut.
Open Files Not Recursive.shortcut (22.7 KB)
^thank you. That worked.
It is really surprising that there’s no obviously direct way to filter out files or folders from a list containing both. It makes me wonder whether we’ve missed something glaringly obvious enough to overlook.
But, in lieu of such a revelation, there’s a speedy method that avoids looping, which can be used in situations where the contents of each file is of importance and not the inodes nor any of their attributes (including file paths, however file names are carried through):
Get Contents Of Folder
➔ Base64 Encode
(Line Breaks: None
or any)
➔ Base64 Encode
(set to Decode
).
Since a directory doesn’t have any data that can be read as a byte stream, these cannot be encoded as base-64 and are skipped over. Decoding is applied to everything else, restoring the contents of each file together with the file name. However, this is a list of temporary files created during encoding to avoid changing the originals. Therefore, viewing them in Finder isn’t likely to be useful, but opening them in any desired application for viewing is perfectly fine.
CJK. Thanks for looking at my shortcut.
It does seem that there should be a easier way to get file or folders only, but that doesn’t seem to be the case. Your suggestion is an ingenious one that seems to work well. An alternative for Nuno might be to filter by file extensions, which avoids the repeat loop, but I didn’t know what extensions to use.