Remove a specific string from Copy Path of Finder Selection output

I’d love a simple modification to a macro Peter Lewis posted for copying the path of a selection in Finder, but I’m not sure how to go about it. It is is something I’d use regularly so it would be great if somebody could chip in…

I’m constantly messaging colleagues about files I’ve added to Dropbox, and as best I can tell there is no way native to Dropbox to link to a file on their computer (as distinct from the web URL).

So what I’d like to be able to do is change what is copied to the clipboard to strip /Users/Michael/ from the start of the string /Users/Michael/Dropbox/folder1/…/folderN/file.txt

So this could be as simple as removing that specific string (which I’m not sure how to do), or perhaps something a bit cleverer and more robust which stripped everything before a nominated string, and thus made the macro more broadly useful to other users.

You might try something like this.
Your Dropbox file path is stored in the KM Variable "Source_Text".

It uses this RegEx:

Example Results

RegEx Explanation

Thanks @JMichaelTX. But I think you’ve assumed too much KM knowledge on my behalf. :wink:

I’m flat chat working on other stuff at the moment, so I’ll come back to this when I’ve got more time to read the documentation and play with your code. I’m not yet sure how to work your example (which I’m presuming is not meant to stand alone, but is a - necessary - lesson in regex for me) into a usable macro.

In an ideal world I think I’d like to add an IF statement to Peter’s original copy path macro which used a variation of your regex to identify paths which included my Dropbox folder, and then to strip out /Users/Michael as required. That way I could have a single shortcut for copying paths, regardless of where the file was located. But as I don’t understand how Peter’s code works yet I’ll have to play around…

Hey Michael,

This sort of stuff is pretty confusing UNTIL you wrap your head around Keyboard Maestro's collections.

Once you learn that a collection is just a textual list of items then light begins to dawn.

Looping through a list of items is something computers excel at.

Loop syntax on the other hand can be fairly confusing in a number of programming languages.

You have to get used to the syntax Keyboard Maestro uses, and once you do you're off to the races.

Here's how I'd attack your problem with Keyboard Maestro:

Finder-Selection → Get POSIX Paths → $HOME-based path option available.kmmacros (7.7 KB)

Keyboard Maestro also supports plain string substitution, so you don't really need to know regex to solve this particular problem (regex just makes the macro a little more flexible).

Now then – if I was doing this job on my own system I'd prefer an AppleScript method:

# Auth: Christopher Stone
# dCre: 2016/11/23 02:23
# dMod: 2016/11/23 02:28
# Appl: Finder
# Task: Return POSIX Paths of selected items as a text-list.
#     : Remove user-prefix of items in Dropbox.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Finder, @Selection, @POSIX_Path, @Path, 
use AppleScript version "2.4"
use framework "Foundation"

tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"

repeat with thePath in finderSelectionList
   set contents of thePath to POSIX path of (contents of thePath)
end repeat

set AppleScript's text item delimiters to linefeed
set finderSelectionList to finderSelectionList as text

set newStr to its cngStr:"(?m)^/Users/[^/]+/Dropbox/" intoString:"/Dropbox/" inString:finderSelectionList

on cngStr:findString intoString:replaceString inString:dataString
   set anNSString to current application's NSString's stringWithString:dataString
   set dataString to (anNSString's ¬
      stringByReplacingOccurrencesOfString:findString withString:replaceString ¬
         options:(current application's NSRegularExpressionSearch) range:{0, length of dataString}) as text
end cngStr:intoString:inString:


On my own system I'd add a little more code for error-reporting, and I'd use the Satimage.osax instead of AppleScriptObjC – but the above script should work fine in Yosemite and onwards.


Hey Chris (@ccstone),

Rather embarrassingly whilst I have been back to the forum a couple of times in the last six months I hadn’t noticed that you had posted this extensive response and macro.

It’s perfect for my purposes (and particularly appreciated given that you obviously had already written an AppleScript to do something similar for your own purposes.)

I’ve given it a minor tweak so that with a keyboard shortcut very similar to the OS’s Get Info shortcut it copies the (truncated, if appropriate) path to the clipboard. Already committed to my muscle memory. :wink:

The signal-to-noise ratio in this forum is off the charts!

1 Like