Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
I've found this "Copy file/folder path to Clipboard as text" Automator OSX Service (scroll down to the section entitled "Create a 'copy path' Service for the right-click menu). Very useful!

Basically you right click on a folder or file, then select this Service (I've called it "Copy path as text" -see screenshot below) and you'll have the entire path as text in the clipboard which you can paste (CMD-V) wherever you want to use it (very useful and time-saving for referring to specific file and folder paths within forums etc.).

screen-shot-2022-03-25-at-11-51-44-png.1980440


It works great except that it copies the full path including the username, meaning I always have to do a "search & replace" in order to change those paths to the truncated type ( ~/ ). Is there a way to modify the Service to allow for this?
(for instance, instead of /Volumes/Mac_HD/Users/johndoe/Library/Preferences/com.apple.finder.plist it would copy the path like this: ~/Library/Preferences/com.apple.finder.plist )
 

Red Menace

macrumors 6502a
May 29, 2011
583
230
Colorado, USA
The tilde is a shortcut for the user’s home folder. /Volumes is not in a user’s folder, so exactly what are you wanting to replace?
 

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
I want to replace this part: /Volumes/Mac_HD/Users/johndoe/
with this: ~/

Would that involve a simple text replace (in a script prior to the clipboard output in the Automator Service), or could the script somehow be made to adapt to work with other users or file structures as well (i.e. another username and a different drive name)?
 
Last edited:

Red Menace

macrumors 6502a
May 29, 2011
583
230
Colorado, USA
A Run AppleScript action can be used to process the paths before passing them on to the Copy to Clipboard action. If you are wanting to replace any user name, note that the tilde shortcut would not be a valid path except for the current user, assuming the file exists in their home folder.
 

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
I'm not sure what you mean.
My purpose for this tool is to easily copy the paths for certain files and folders on my Mac (which is present in every Mac) for the purpose of referring to them in web forum discussions, such as here.
So instead of having my username as part of the path, or in my case: my rather non-standard placement of my home folder (I've relocated it to a separate hard drive as opposed to the boot-SSD) they just get the usual tilde type path and can easily find the same file on their own computer.
 

Red Menace

macrumors 6502a
May 29, 2011
583
230
Colorado, USA
The tilde shortcut only applies to the current user’s home folder. Your original post gave an example path in the /Volumes folder, which looked like it might be for something else. I don't have an external user folder to test, but the following Run AppleScript action added before the Copy to Clipboard action should replace the home folder path with a tilde.

AppleScript:
on run {input, parameters}
   set output to {}
   set homeFolder to POSIX path of (path to home folder)
   set here to length of homeFolder -- index for the user part of the path
   repeat with anItem in the input
      set anItem to POSIX path of anItem
      if anItem starts with homeFolder then -- shorten path using tilde
         set end of output to "~" & text here thru -1 of anItem
      else
         set end of output to anItem
      end if
   end repeat
   return output
end run
 

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
You don't need to add a service for this. Command-option-c will copy a file's path, and this is built in to Finder.

You're absolutely right! I never knew about this and just tried it. Thanks for sharing :) 👍
I wish Apple wouldn't be hiding so many useful things like this. There are always the "10 secret features in OSX that you never knew" types of web pages around, but is there a complete listing of all the hidden stuff to be found anywhere?

That feature does however copy the entire path (as it correctly should of course), but I'm going to try to incorporate Red Menace's Applescript into the Service to see if I can get the tilde path to work.
 

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
The tilde shortcut only applies to the current user’s home folder. Your original post gave an example path in the /Volumes folder, which looked like it might be for something else. I don't have an external user folder to test, but the following Run AppleScript action added before the Copy to Clipboard action should replace the home folder path with a tilde.

Excellent! I gave it a quick tryout and it appears to work as intended :)
For others reading, this is what it looks like in the Automator app (after first going to its File-New menu and selecting "Service" as the new document you're going to create:

Screen Shot 2022-06-10 at 13.08.26.png



Screen Shot 2022-06-10 at 13.07.04.png


So now I have this service for those instances where I want to point to certain folders/files in a forum without having to disclose the full username path, then the previous original Service which copies the exact path (including the username) for other uses (or I can use the CMD-ALT-C shortcut as suggested by Chrfr -if I remember the key combination a couple of weeks from now that is).

The Services are found here: ~/Library/Services/
(hey! I just got to try out that service right here and now ;) )
Screen Shot 2022-06-10 at 13.17.43.png
 
Last edited:

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
Another thing I just realized after reading some comments here is that when following Chrfr's suggestion (selecting the file or folder in question, the pressing CMD-ALT-C which will copy the item's path to the clipboard) is that:

.......if you don't remember the keyboard combination properly you can select the file or folder, go to the Finder's Edit menu and while having the mouse-pointer there you press the different keys (various combinations of CTRL, CMD, SHIFT, ALT etc.) until the menu changes to "Copy ITEM as Pathname" (where "item" is the file or folder's name).

Untitled.png


The strange thing is that using just ALT-C worked in exactly the same way as CMD-ALT-C (and showed it as "Copy ITEM as Pathname" even though the shortcut to the right of it clearly shows CMD-ALT-C).


Oh... you may be wondering how I made the above screenshot when I was already using the CMD key to access that menu as one a screenshot of a selection is CMD-SHIFT-4 and a screenshot of the entire screen (you can crop it later if needed in Preview for instance) is CMD-SHIFT-3?
Well, I used "Grab" (/Applications/Utilities/Grab.app) for this as it has a "Timed screenshot" function meaning I can set it off, quickly go back to the Finder, select the file or folder in question and open the Edit menu which I wanted to show then wait a few seconds until the screenshot is taken. Good to know in case you need to take a screenshot of something that demands the use of those special keys.
Screen Shot 2022-06-10 at 13.27.46.png
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.