Getting path of selected item in Finder path bar

Not sure if this is even possible, but I’d like to ask just in case.

Here’s the scenario (scroll to bottom image for visual reference, unfortunately i have to consolidate everything into a single image as I am a new user…I sort of get the logic of this but it’s pretty annoying!)

So obviously I know in finder i can get the path of a selected item or folder quite simply through

tell application "Finder"
	set selItem to the selection
	set posixPath to POSIX path of (selItem as text)
end tell

For example in my case i created this test folder (part A of the only image I am allowed to upload)

I then set the clipboard to posixPath, and i have myself a quick action that copies the POSIX path of the selected item. All good.

My dilemma is this, what if i wanted to copy the path of one of the items in path bar? Is there any way in AppleScript to reference the element which is being right clicked in the pathbar? I couldn’t for the life of me find anything about it.

As a visual example, in part B of the image, i’d like the script to copy the path up to the right-clicked folder in the pathbar so the clipboard would store “/Users/Test/” (ignore that Test is the active folder in this example, my objective is to get the path of any right-clicked folder in the path bar).

Let’s also ignore for now the fact that we’d still have a selection active in finder which we’ll have to deal with, for now i’d only like to know if there’s a way have the script read the path bar “selection”.

here’s the image for reference

Many thanks for any contribution!
Cheers

What is your goal here?

There’s no inherent access via AppleScript to that item in the path bar, at least not via AppleScript.

As far as the Finder is concerned, selection always relates to the selected file(s)/folder(s), not arbitrary UI elements.

However, if you’re just trying to get the folder the selected item is in, that’s easy:

tell application "Finder"
	set selItem to (get selection)
	 if selItem is not {} then
	 	 set itsFolder to container of (item 1 of selItem)
	 end if
end tell

You can use a Run AppleScript action in an Automator Quick Action (service) for both - a list of aliases will be passed to the workflow, which the action will receive in its input parameter. Set the workflow to receive files or folders in the Finder and it will appear under Services in the path bar contextual menu, and under Quick Actions in the Finder contextual menu.

Play with this script…

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property myProps : {}

local myRow
set myRow to false
tell application "System Events" to tell application process "Finder"
	repeat while exists window 1
		try
			set myRow to false
			tell outline 1 of scroll area 1 of splitter group 1 of window 1
				repeat with c from 1 to count rows
					if selected of row c then
						set myRow to row c
						exit repeat
					end if
				end repeat
				if myRow = false then
					delay 0.5
				else
					if exists menu 1 then
						tell me to say (value of static text 1 of UI element 1 of myRow) & " Is right clicked"
					else
						tell me to say (value of static text 1 of UI element 1 of myRow) & "Is Selected"
					end if
				end if
			end tell
		on error
			beep
		end try
	end repeat
end tell

First of all thanks for your reply!

We have a software that copies the path of an item and converts it to windows format (WinShortcutter), as well as allowing us to navigate to locations shared with us in windows format, but I wanted to try to do it via AppleScript as an exercise to start learning.
I managed to do both things perfectly, and annoyingly the only limit of applescript vs that software so far is the inability to copy from the path bar, which is quite handy because at any point i can copy the location of any level of the current volume with a simple right click.

You are probably right though that this is simply a limit of AppleScript.

That looks interesting.
I only partially understand what’s happening at a glanc, but I will definitely try to decode it tomorrow.

Many thanks!

This may not be the solution you are looking for, however, this may be something you will find useful. I know I find it useful.

This following AppleScript will take the items selected in the front Finder window. It will then present you with a list to choose from, which contains the path to every containing folder, then copy your chosen path to the clipboard.

In the Shortcuts app I created a new shortcut with the AppleScript code so anytime while I’m in Finder and I have an item selected I could simply run the shortcut from the shortcut menu bar which will give me the option to copy the path of any of the containing folders of the selected item to my clipboard.

set theContainers to {}

tell application "Finder"
	set selectedFolder to item 1 of (get selection)
	set containingFolder to container of selectedFolder
	set end of theContainers to selectedFolder as alias
	set end of theContainers to containingFolder as alias
	repeat
		try
			set containingFolder to container of containingFolder
			set end of theContainers to containingFolder as alias
		on error errMsg number errNum
			exit repeat
		end try
	end repeat
end tell

activate
set theChoice to (choose from list theContainers ¬
	with title "Folder Path To Clipboard" with prompt ¬
	"Choose An Item To Place Its Path On Your Clipboard" OK button name ¬
	"OK" cancel button name "Cancel")

if theChoice = false then return

set the clipboard to POSIX path of theChoice

This following image shows how I set this up in the Shortcuts app

This following animation demonstrates the shortcut in action.

Folder Path To Clipboard

This COULD work, IF i found a way to access the pathbar items, then, even if effectively the UI only reads the name of thar folder, i could reconstruct the path based on location information from the current active finder window.
But for the life of me i can’t, i can’t even get to work the damn UI Browser on my mac :frowning:

Well, this is suuuper interesting!
It’s a nice workaround, although under the perspective of creating a script to save a couple clicks on repetitive tasks, unfortunately on its own it doesn’t hit the brief, but I thank you very much for providing a further learning experience with an approach that I hadn’t thought about at all!

Here is another solution which, if you ask me, is probably the simplest solution using the least amount of code.

In Script Editor, use this following AppleScript code and save it as an application. Then just place that application in your Dock. Now all you need to do is drag any file or folder from anywhere in the Finder window to your new AppleScript application which you placed in the Dock, and it will place that item’s Posix path on your clipboard.

on open theFiles
	set the clipboard to POSIX path of theFiles
end open

path to clipboard droplet2

if you use this following code instead…

on open theFiles
	set the clipboard to POSIX path of theFiles
end open

on run
	activate
	display dialog (the clipboard) buttons ¬
		{"OK"} default button "OK" giving up after 4
end run

After you drop an item onto the app’s icon in the Dock, which copies that items POSIX path to your clipboard, if you simply just click on that app icon in your Dock, it will display a Dialog window showing what is on your clipboard.

path to clipboard droplet2

AlDente, you never responded to my script above.

Did you see how I used whether the menu from a right-click being visible, was tested for existing using GUI scripting, to determine if it was right-clicked in the first place?

Getting a file path is fairly straightforward. :slightly_smiling_face:

tell application "Finder"
	activate
	set select_file to selection
	set the clipboard to the select_file as text
	--display dialog select_file as string
end tell

Going back to the OP, this is just a proof of concept. This script will process a path for the element of the path bar that is under your cursor.

Mouse over the path element that you are interested in getting the full path of then run the script. This may require assigning it to a voice command, having the script open in SE or SD and pressing CMD-R, Run AppleScript service item or other method to run the script without moving the cursor.

--5/21/25 https://www.macscripter.net/u/paulskinner/
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

tell application "System Events"
	tell its application process "Finder"
		tell its window 1
			tell its UI element 1
				tell its list 1
					set {n, p} to {name of every static text, position of every static text} --of every UI element
				end tell
			end tell
		end tell
	end tell
end tell

set {cx, cy} to Finder_Cursor_Position_Get()

set thePath to ""
repeat with i from 1 to length of p
	set thisEntry to item i of p
	set {x, y} to thisEntry
	if cx ≥ x then set thePath to thePath & item i of n & ":"
end repeat

thePath -->"iCloud Drive:Development:AppleScript:AppleScript Development:AppleScripts:"

on Finder_Cursor_Position_Get()
	try
		set screens to current application's NSScreen's screens
		set theScreen to (screens's objectAtIndex:0)'s frame() -- main screen
		set {screenWidth, screenHeight} to item 2 of theScreen
		set theMouseLocation to current application's NSEvent's mouseLocation()
		set {theX, theY} to {x of theMouseLocation, screenHeight - (y of theMouseLocation)}
	on error errorText number errorNumber partial result errorResults from errorObject to errorExpectedType
		error "<Finder_Cursor_Position_Get>" & errorText number errorNumber partial result errorResults from errorObject to errorExpectedType
	end try
end Finder_Cursor_Position_Get

I’m using this code in an AppleScript action of an Automator Quick Action, you can choose HFS path, POSIX path, abbreviated POSIX path (with tilde representing the current home folder) and URL representation (file://…).

on run {input, parameters}
	set selectedItem to item 1 of input
	set chosen to choose from list {"1 HFS", "2 Full POSIX", "3 Abbreviated POSIX", "4 URL"}
	if chosen is false then return ""
	set chosenIndex to (text 1 of item 1 of chosen) as integer
	if chosenIndex is 1 then
		return selectedItem as text
	else if chosenIndex is 2 then
		return POSIX path of selectedItem
	else if chosenIndex is 3 then
		set homeFolder to POSIX path of (path to home folder)
		set unixPath to POSIX path of selectedItem
		if unixPath begins with homeFolder then return ("~" & text (count homeFolder) thru -1 of unixPath)
	else if chosenIndex is 4 then
		tell application "Finder" to return URL of selectedItem
	end if
	return ""
end run

For quick access you could assign a keyboard shortcut.

I looked at this as a challenge to determine how to access the path bar UI elements programmatically. I determined a method comparing the UI elements positions and the cursor position and confirmed that it worked. I then looked at implementing the script as a contextual menu item. I then right-clicked an item in the path bar just as the OP did in picture 1.

Now I get it.

I didn’t realize this post was an elimination-by-response test. The solution is in the pic in the first post. In the contextual menu visible you can see the option “Copy “Test” as Pathname

This contextual menu selection copies the path of the right clicked item in the path bar to the clipboard.

“/Users/Test/” in this case.

|^:

The path bar selection will also be passed to a files and folders Quick Action (Service).