3

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!

1
  • The tilde is a shell construct, although Finder in my limited testing with ↑⌘G appears to substitute $HOME for it. Dec 10, 2021 at 20:25

2 Answers 2

2

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.

1
  • I like how you clip $HOME from the string! I would never have thought of that. Dec 12, 2021 at 18:39
0

You can try this, although it's not perfect. It will blow up if you have more than one file selected.

Bind it to the hotkey of your choice.

tell application "Finder"
    set temp to selection as alias
end tell
set posixPathName to POSIX path of temp
set myHome to POSIX path of (path to home folder)
set result to get replaceText(myHome, "~/", posixPathName)
set the clipboard to result
return

on replaceText(find, replace, subject)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set subject to text items of subject
    
    set text item delimiters of AppleScript to replace
    set subject to subject as text
    set text item delimiters of AppleScript to prevTIDs
    
    return subject
end replaceText

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .