I want to create a KM macro to transform a Finder path to something a little more readable to humans. For example, I'd like to turn this:
/Volumes/GoogleDrive/My Drive/Company Drive/Teams/STU - Studio Team/Livestreaming/Webinars/Walk Away/Script/2022-01-06 - Walk Away and Watch It Grow.tp3
Volumes > GoogleDrive > My Drive/Company Drive > Teams > STU - Studio Team > Livestreaming > Webinars > Walk Away > Script > 2022-01-06 - Walk Away and Watch It Grow.tp3
Idealy, I would simply select the file, then invoke the macro with a shortcut key. This would copy the path to the system clipboard, search and replace the text, and leave the transformed path on the system clipboard, so I could just paste it where I want.
How can I do this?
Others will have a more elegant/efficient way to do this with some regex black magic, but here is what I would do:
EDIT: I should note we are getting the selected file, removing the
/ at the beginning of the path, then replacing all subsequent
> (space, closed bracket, space).
Beautiful! That worked.
I don't know regular expressions. In the first expression, I'd like to remove, not just the beginning slash character, but everything up to Company Drive. I suspect I'll have to use a wildcard character of some kind because Google assigns that long number to every path.
In other words, I want to transform this:
/Volumes/GoogleDrive-103930667378793848215/My Drive/Company Drive/Teams/STU - Studio Team/Livestreaming/Webinars/Walk Away/Scripts/2022-01-06 - Walk Away and Watch It Grow.tp3
Company Drive > Teams > STU - Studio Team > Livestreaming > Webinars > Walk Away > Scripts > 2022-01-06 - Walk Away and Watch It Grow.tp3
By deleting this:
How would I fornat the expression? Thanks!
Will it always be
/Volumes/GoogleDrive-103930667378793848215/My Drive/ exactly?
If so, you can skip regex and just search for that string and replace it with nothing. Like so:
On second thought, it looks like the long number assigned by Google may be random. So if that's the case, this is wipe out everything up to
Remember this assumes the selected file will always be formatted the same, and doesn't include checking or handling for other cases.
Does that work?
Brilliant! That worked beautifully. Thanks so much. I would have never figured that out on my own.
That's great, Michael! I've learned plenty from you in your writing and podcasts, so I am more than happy to help
Agh! Learn some...
A relatively small investment in time and effort goes a long way, and you don't stay dependent upon others for very simple tasks like this.
Here's one way to do the job with vanilla AppleScript:
tell application "Finder"
set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
set theItem to item 1 of finderSelectionList
set itemPath to POSIX path of theItem
set AppleScript's text item delimiters to "/My Drive/"
set itemPath to text item 2 of itemPath
set AppleScript's text item delimiters to "/"
set itemPath to text items of itemPath
set AppleScript's text item delimiters to " > "
set itemPath to itemPath as text
set the clipboard to itemPath
The text-processing is all done with literal text, although most people struggle at least a little bit when wrapping their head around AppleScript's text item delimiters.
As usual, I agree with Chris. Learning regex is life-changing, and in a decidedly positive way. Many others have posted sites that are excellent for TESTING regex, but for LEARNING regex, I prefer the following:
It is well worth the effort to study why regex works the way it does rather than just trying to find a regex that works for a specific use.
I've been a fan of Jan Goyvaerts for well over a decade...