Getting the path of currently selected file in Finder


Your solution looks great, but when I implement it, the Path only reveals a subset of the entire folder path. For example, if my path is /Users/home/Dropbox/CapData/RawFileName/1994/filename.csv, I’m getting “/Users/home/Dropbox/CapD”. I’ve searched around for a solution, but cannot figure it out.


Hey ak,

Try this one. It will get the container folder of the selected item whether it be folder or file.


Copy Container Folder of Selected Item as Posix Path.kmmacros (4.5 KB)

1 Like


Thanks, this worked perfectly. For future reference, is there a place where I can learn this syntax? Thanks again


Thanks for this macro. Quick question, when I use it with Path Finder it gives me the UNIX path for the file, excluding its name. I presume this is what it’s meant to do.

So for example, running the macro a result might be /Documents/Personal/Finances/Letters/

What I would like is the filename as well e.g. /Documents/Personal/Finances/Letters/letter to bank.doc

Is it easy to modify the macro to do that?




Odd, when I get the UNIX path from Path Finder, it includes the file name.

I see the same output as duncmac - there is something in the regex that is cutting the file/folder name after the parent path. If I use the PathFinder menu directly (or delete the regex step, I get the whole thing too.

Hit send too soon on last message -

BUT, the macro name is Copy CONTAINER Folder…, so I think the macro is doing exactly what it is designed to do. If you want the entire path to the file, just delete the regex step in the macro.

Thanks rolian, that worked.

Hey Rob,

That's exactly right.   :smile:


Hey Duncan,

If you want to get the Unix path of a file/folder into the clipboard in Path Finder, all you have to do is Copy (Cmd-C).

If you want to use those paths in a macro then it's faster to get them with AppleScript.

# Get Posix Paths of Selected Items in Path Finder
# Assign a Keyboard Maestro variable to the value.
set pfSelList to missing value

tell application "Path Finder"
  set pfSelList to selection
  if pfSelList ≠ {} then
    repeat with i in pfSelList
      set (contents of i) to POSIX path of (contents of i)
    end repeat
  end if
end tell

if pfSelList ≠ missing value then
  set AppleScript's text item delimiters to linefeed
  set pfSelList to pfSelList as text
  tell application "Keyboard Maestro Engine"
      set value of variable "myVariableName" to pfSelList
    on error
      make new variable with properties {name:"myVariableName", value:pfSelList}
    end try
  end tell
end if


Hey ak,

Look in Keyboard Maestro's Help menu for the 'ICU Regular Expression Reference' item and select it.

Learning regular expressions takes some effort. It appears very intimidating at first, but simple regex is really very easy to learn. Complex regex usage though is something one never masters entirely — I'm still learning new things after over 20 years of using them.

If you're serious then you'll want to buy a book or two.

You can learn a lot about regular expressions online, but there are many flavors. They don't all work the same, and that can be very confusing to newbies.

Scope out the resources on the Keyboard Maestro wiki as a start.


Chris, you've made some very nice, very helpful additions to the wiki. :+1:

Many thanks.

Thanks Chris, looks like I need to check out Path Finder keyboard shortcuts.

Very nice and thankx. It’s sometimes so natural to go off programming something or looking for special KM actions that something as simple as this doesn’t present itself.

To get the filename without the path, just add parentheses around the second .*. You’ll see a field for a second capture group – that will be the file name with no path.

Hi, I'm trying to find a method of extracting a valid path from theDroppedItems (coming in from AppleScriptEditor). I tried reading this but have been unable to do it.

I'm hoping to use the path in theDroppedItems (the full folder-path, not the full folder&file-path) and copy it to the clipboard.

There will be only one file. Is this complex to do?

Hey Esa,

I'm not aware of any such thing, so I suspect a language issue. :wink:

Could you rephrase and be more specific?


Perhaps the context is this page: ?

Hey Rob,

I considered that, but Esa said:

That doesn't imply a droplet, so let's get a definitive answer from him before moving on.


and an example would be good :slight_smile:

Just wanted to say this old thread helped me a lot during "Ronaland."

1 Like