Place the path to a disk item on the clipboard

There are several built-in methods by which the path to a disk item can be placed on the clipboard, and most users will need nothing more. I use the script included below because:

  • I can save the script as an app and place an icon to the script on the Finder toolbar.
  • The script presents the user with a dialog and allows the user to select either a POSIX or HFS path.
  • As written, the script encloses the path within quotes for easy pasting into a script.
on main()
	tell application "Finder"
		try
			set selectedItems to selection as alias list
			set itemCount to (count selectedItems)
			if itemCount = 0 then set end of selectedItems to (target of front Finder window) as alias
		on error
			display alert "An error has occurred" message "An item is not selected or a path is not available for a selected item" as critical
			error number -128
		end try
	end tell
	
	if itemCount < 2 then
		set pathOption to button returned of (display alert "Copy 1 item to the clipboard as a pathname" buttons {"Cancel", "HFS", "POSIX"} default button 3 as informational)
	else
		set pathOption to button returned of (display alert "Copy " & itemCount & " items to the clipboard as pathnames" buttons {"Cancel", "HFS", "POSIX"} default button 3 as informational)
	end if
	
	if pathOption = "Cancel" then
		error number -128
	else if pathOption = "HFS" then
		repeat with anItem in selectedItems
			set contents of anItem to quote & (anItem as text) & quote
		end repeat
	else
		repeat with anItem in selectedItems
			set contents of anItem to quote & (POSIX path of anItem) & quote
		end repeat
	end if
	
	set text item delimiters to {linefeed}
	set the clipboard to (selectedItems as text)
	set text item delimiters to {""}
end main

main()

Hi. I’ve been using this in Catalina and it’s been very solid and helpful. Thanks!

kerflooey. Your most welcome. It’s nice to hear when a script is helpful or works well.