Copy relative file path in Finder

How can I copy a file path in Finder without the username (i.e. make it a relative file path)?

In our office we're working with Dropbox as our file server. Meaning we all have a directory /User/username/Dropbox/.. We're sharing a ton of file paths every day and I'm looking for a way to speed this up.

I know that there is the shortcut cmd + option + c for copying a file path, but that will always include /Users/username/ in the path, so when I share that path with a colleague they can't open it (because their username is differenct, obviously).

-> Is there a way to copy the file path as a relative path, e.g. ~/Dropbox/filename?

-> If not, is it possible to modify the shortcut cmd + option + c so that it automatically replaces /Users/username/ with ~?

Any help or creative ideas are most welcome!


Solution 1:

The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

It can be used in an Automator Quick Action/Service or by any third-party application that can run AppleScript code with a keyboard shortcut.

This was also tested as an Automator Quick Action/Service with a keyboard shortcut of ⌃⌘C assigned to it in: System Preferences > Keyboard > Shortcuts > Services

Example AppleScript code:

set tildeNamesList to {}
set homeFolder to POSIX path of (path to home folder)
tell application "Finder" to set selectedItemsList to (selection as alias list)
if selectedItemsList is {} then return
repeat with thisItem in selectedItemsList
    set thisItem to POSIX path of thisItem
    if thisItem does not start with homeFolder then return
    set AppleScript's text item delimiters to homeFolder
    set end of tildeNamesList to "~/" & text item 2 of thisItem
end repeat
set AppleScript's text item delimiters to linefeed
set tildeNamesList to text items of tildeNamesList as text
set AppleScript's text item delimiters to ""
set the clipboard to tildeNamesList

Notes:

The example AppleScript code, as coded, does the following:

  • Replaces e.g. /Users/you/ with ~/ in the pathname.
  • Works with a single file/folder and or multiple files/folders from within one's Home folder.
  • Includes appropriate error handling to silently stop processing as appropriate.
  • Places the ~/… pathname(s) on the Clipboard when pressing the assigned keyboard shortcut to the Automator Quick Action in Finder.

If you'd like to backslash escape and spaces in the pathnames add the following lines of AppleScript code:

set AppleScript's text item delimiters to " "
set thisItem to text items of thisItem
set AppleScript's text item delimiters to "\\ "
set thisItem to text items of thisItem as text

Between:

if thisItem does not start with homeFolder then return

And:

set AppleScript's text item delimiters to homeFolder

For the Automator Quick Action/Service configure it as shown in the image below replacing the default code in the Run AppleScript action with the example AppleScript code.

enter image description here

Note that while the Automator Quick Action was named Dropbox Pathnames to Clipboard it was done more so one would know what it was for, however it will work on any selected files within one's Home folder.