Getting the path of currently selected file in Finder

I would like to get the path of a file selected in Finder.

I have seen the post. When I use it, it provides both the path and filename.

An internet produces some AppleScript such as:

tell application "Path Finder" to get POSIX path of (item 1 of (get selection))

But this also produces path and filename.

Is there a simple way to amend the AppleScript or the macro that Peter provided in his post to just get the filename alone?

1 Like

Hey Victor,

It seems to me that you've misstated your problem.

Path generally refers to the entire path of an item.

You say you want just the path — do you mean the parent-path of the file — or do you in fact want just the file name as you've stated after that?

NOTE: The AppleScript you show is for Path Finder and NOT the Finder. They are not compatible.

The AppleScript Way:

-------------------------------------------------
# Full path of selected items in Finder.
-------------------------------------------------
tell application "Finder"
  set finderSelList to selection as alias list
end tell

if finderSelList ≠ {} then
  repeat with i in finderSelList
    set contents of i to POSIX path of (contents of i)
  end repeat
  
  set AppleScript's text item delimiters to linefeed
  finderSelList as text
end if
-------------------------------------------------

Only the file name:

-------------------------------------------------
# Names-Only of selected items in Finder.
-------------------------------------------------
tell application "Finder"
  set finderSelList to selection as alias list
end tell

if finderSelList ≠ {} then
  tell application "System Events"
    if quit delay ≠ 0 then set quit delay to 0
    repeat with i in finderSelList
      set contents of i to name of (contents of i)
    end repeat
  end tell
  
  set AppleScript's text item delimiters to linefeed
  finderSelList as text
end if
-------------------------------------------------

Parent Path for Just 1 item:

-------------------------------------------------
# Parent path of 1 selected item in Finder.
-------------------------------------------------
tell application "Finder" to return POSIX path of (parent of first item of (get selection as alias list) as alias)
-------------------------------------------------

If you'll clarify what you want I'll focus some more.

--
Best Regards,
Chris

2 Likes

and in JavaScript for Applications, if you have the full path, you can just pop the filename off the end

var	strFullPath = '/Users/houthakker/Desktop/tree.png';
	
var	lstPath = strFullPath.split('/'),
	strFile = lstPath.pop(),
	strFolder = lstPath.join('/');

[strFolder, strFile]

Thank you for the replies.

It is just the parent path I need: (e.g. “/Users/Victor/Documents” from “/Users/Victor/Documents/document.md”)

Ideally from a item selected in PathFinder, which the example provided by Chris:

# Parent path of 1 selected item in Finder.

tell application "Finder" to return POSIX path of (parent of first item of (get selection as alias list) as alias)

does not work.

My knowledge of AppleScript (let alone JavaScript) is almost non-existent.

In JavaScript for Applications you could write:

var	maybeSeln = Application("Path Finder").selection(),
	strFolder = maybeSeln ? maybeSeln[0].container().posixPath() : '';
	
strFolder

in Applescript, perhaps:

tell application "Path Finder"
	set maybeSeln to selection
	if maybeSeln is not missing value then
		set strFolder to POSIX path of (container of (item 1 of maybeSeln))
	end if
end tell

strFolder
1 Like

@ccstone and @ComplexPoint have provided some great AppleScript and JXA solutions, but note that give the full path for a file you can sue the Get File Attribute action to get all sorts of bits of the path including the parent path, the file name, the extension and the base name (file name without the extension).

3 Likes

If you need the path of an item in PathFinder you can use cmd+c. This copies the full path. Then use KM to to filter so you get the path without filename.

Hey Victor,

Well, obviously a script written for the Finder won't work in Path Finder.

In your first request you said you needed to act in the Finder, although your script example was for Path Finder.

Remember — the more clarity you can bring to a help request the sooner you'll get a useful answer.

There's little point in writing this to handle only 1 selected item, when it's just as quick to manage more.

Ordinarily all parent paths will be the same, unless you have a folder open hierarchically.

The scripts will return all unique parent paths of selected items.

This one is a but long to show how everything works:

---------------------------------------------------------------------
set parentPathList to {}

# Extract Posix Paths from Path Finder.
tell application "Path Finder"
  set pfSelList to selection
  repeat with i in pfSelList
    set contents of i to POSIX path of (contents of i)
  end repeat
end tell

# Extract the parent path of every item.
set AppleScript's text item delimiters to "/"
repeat with i in pfSelList
  set pPath to ((text items 1 thru -2 of (contents of i)) as text) & "/"
  if pPath is not in parentPathList then set end of parentPathList to pPath
end repeat

# Produce a textual listing of unique parent paths.
set AppleScript's text item delimiters to linefeed
set parentPathList to parentPathList as text
---------------------------------------------------------------------

This one is fairly terse:

---------------------------------------------------------------------
set parentPathList to {}

# Extract Posix Paths from Path Finder.
set AppleScript's text item delimiters to "/"
tell application "Path Finder"
  set pfSelList to selection
  repeat with i in pfSelList
    set pPath to ((text items 1 thru -2 of (get POSIX path of i)) as Unicode text) & "/"
    if pPath is not in parentPathList then set end of parentPathList to pPath
  end repeat
end tell

# Produce a textual listing of unique parent paths.
set AppleScript's text item delimiters to linefeed
set parentPathList to parentPathList as text
---------------------------------------------------------------------

-Chris

PathFinder has a menu item to get the path in all different formats e.g.: Edit > Copy Path > UNIX, so you can do something like this:

jhein,

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.

thanks

Hey ak,

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

-Chris


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

1 Like

Chris,

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

Chris,

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?

Thanks,

Duncan

duncmac,

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:

-Chris

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"
    try
      set value of variable "myVariableName" to pfSelList
    on error
      make new variable with properties {name:"myVariableName", value:pfSelList}
    end try
  end tell
  
end if
------------------------------------------------------------

-Chris

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.

https://wiki.keyboardmaestro.com/Regular_Expressions

-Chris