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.
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
@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).
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.
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
---------------------------------------------------------------------
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 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
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.
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.
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
------------------------------------------------------------
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.