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.
Thanks @JMichaelTX. But I think you’ve assumed too much KM knowledge on my behalf.
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…
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
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
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.
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.
The signal-to-noise ratio in this forum is off the charts!