Enclosing folders via zsh modifiers
August 15, 2025 at 5:36 PM by Dr. Drang
I don’t want to give you the impression that I get all my blogging ideas from Jason Snell, but here’s the second one this week. It was inspired by his post this morning about using the new folder automation feature in macOS 26. After bookmarking the article so I can refer to it after upgrading to 26, I started thinking about how he and Dan Moren went about extracting the name of a file’s enclosing folder from its full path name.
They went about it in basically the same way: splitting the Unix path string on slash characters. The difference was that while Dan was happy to get the full path to the enclosing folder, Jason wanted just the enclosing folder’s name. For example, for a file named
/Users/jsnell/Desktop/Drop It Here/Markdown File.md
Dan wanted
/Users/jsnell/Desktop/Drop It Here
whereas Jason wanted
Drop It Here
Their solutions were Shortcuts-native, by which I mean they used Shortcuts actions only—no “outside” calls to AppleScript or the shell or any other scripting language. But I immediately thought of using the shell’s dirname
and basename
commands to replace their and actions.
It wasn’t hard to do, but I thought it was clumsy because dirname
and basename
don’t use standard input, so I couldn’t just pipe the output of one to the other. (dirname
by itself would give me Dan’s solution, but I wanted to make my life harder by replicating Jason’s solution.)
A cleaner approach came from zsh
’s modifiers, suffixes you can append to pull out parts of a file path. Recent versions of bash
also have modifiers, but not the old version that Apple supplies with macOS.
Because I’m not running macOS 26, I couldn’t build a folder automation Shortcut, but here’s a proof-of-concept Shortcut named
that runs as a Quick Action on selected files in the Finder.For each selected file, the full path is passed as the argument to this script,
echo ${1:h:t}
and the output is displayed in a window.
$1
is the first (and only) argument to the script. The :h
modifier pulls out the “head” of the path, which is the path to the enclosing folder. The :t
modifier returns the “tail” of that, which is just the name of the enclosing folder. Brace expansion isn’t necessary in this case, but I tend to use it when doing anything more complicated than just referencing a variable.
As you can see in the Shortcuts sidebar, this is saved as a Quick Action that’s meant to be invoked from the context menu in the Finder.
To test it, I added a Drop It Here
folder to my Desktop and put two files in it: Markdown File.md
and Another Markdown File.md
. Note that I’ve included spaces in both the file names and the folder name to see whether that screws up the shell script. Here’s me calling the Quick Action from the context menu,
and here’s the window that appears twice, once for each of the selected files,
So it works. And if I’d wanted to replicate Dan’s approach, I would’ve used just
echo ${1:h}
in the shell script.
This is not an improvement on Jason’s or Dan’s work. I wouldn’t disagree if you said it was worse than their Shortcuts because it’s more cryptic. Far more people know that directories are separated by slashes than know about :h
and :t
. But it was fun to reacquaint myself with modifiers.