Get Name of Enclosing Folder

I need to get the name of the enclosing folder for a selected file in the Finder (so that I can go back into Premiere Pro and paste that folder name into the currently selected clip, which I think I can figure out...just can't figure out the part where KM gives me the Enclosing Folder's name in the Finder).

This seems simple but I can't figure it out. thx

Here is a way to do it.
This macro sets the clipboard to have the name of the enclosing folder.

Here is a video of the macro in action:

Get name of the enclosing folder Macro (v10.0.1)

Get name of the enclosing folder.kmmacros (2.7 KB)

3 Likes

Wow, thank you so much! It would have taken me a decade to have figured that out!
The only thing I don't think I need is the last step where the System Clipboard is set to a variable, only because I am immediately pasting the name into the clip (in Premiere Pro). I just told it to output to the System Clipboard, but otherwise this is perfect (your quick reply greatly appreciated).

I will say here this level of complexity can make KM somewhat inaccessible. It would be great to just have some constants like "Parent_Folder" and so on, rather than parsing using regular expressions. KM does this for many aspects already so it's not like it couldn't be done.

1 Like

I seem to remember having seen somewhere in KM to be able to get to path segments easier.
But could not find it right away, so I choose the regex way.
And I always forget that many actions have an option to output to other than source. So output to clipboard from the search and replace is a good idea.

Hey Guys,

Just to be obstreperous...  :sunglasses:

Finder Selection ⇢ Set Clipboard to Parent Folder Name.kmmacros (5.3 KB)
Keyboard Maestro Export

-Chris

1 Like

Test Enclosing Folder.kmmacros (3.4 KB)

You can avoid regex simply by applying the Split Path action twice, like this:
KM 0 2021-11-17_22-05-58

EDIT: I’m suddenly thinking I’ve misunderstood this… apologies if I have.

2 Likes

Hey Rowan,

I'm afraid that's not likely to happen.

@tiffle beat me to the punch on this one, but mine copies to the clipboard.

:sunglasses:

-Chris


Finder Selection – Copy Name of Parent Folder to Clipboard v2.00.kmmacros (5.4 KB)
Keyboard Maestro Export

2 Likes

Take a look at the Get File Attribute Action and the Finder Selection collection.

For example, you can cycle through the selected item(s) in Finder using a For Each Action and grab the desired attribute (such as the parent path) like this:

image

2 Likes

That works also!

1 Like

Interesting. I need just the name of the Enclosing Folder of the selected file. Your example gives me the entire path.
I'll look at your two suggestions, though...thanks.

Hey Jimmy,

Just to be pedantic as well as to simplify where one can – for this task your regular expression need only be:

Find:

.+/

Replace:

Nothing.

-Chris

1 Like

Hey @rowan,

There's also a file name attribute, so you can use two filters to get there.

-Chris

Or there’s this…

1 Like

Hiya...
That also works well, thanks.

You can be that terse if your workflow allows it, or you can build in some error-handling:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/11/19 15:55
# dMod: 2021/11/19 15:55 
# Appl: Finder
# Task: Return the Name of the Selected Item (one item) in the Finder. 
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Name, @Selected, @Item
--------------------------------------------------------
property LF : linefeed
--------------------------------------------------------

try
   
   tell application "Finder"
      
      set finderSelectionList to selection as alias list
      set numberOfSelectedItems to length of finderSelectionList
      
      if numberOfSelectedItems = 1 then
         return name of parent of item 1 of finderSelectionList
      else
         error "Error:" & LF & LF & "The Number of Selected Items is: " & numberOfSelectedItems
      end if
      
   end tell
   
on error errMsg number errNum
   set errMsg to errMsg & linefeed & linefeed & "Num: " & errNum
   if errNum ≠ -128 then
      try
         tell application (path to frontmost application as text) ¬
            to set ddButton to button returned of ¬
            (display dialog errMsg with title ¬
               "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to errMsg
      end try
   end if
end try

--------------------------------------------------------

That's cool. If I understand the program logic of your script, there's error checking to determine if less than or more than 1 item is selected. My macro gets started up inside of Premiere Pro with the command "Show clip in Finder" so it can only have one selection (assuming the macro is running properly). But, hey, error checking's good!

1 Like