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
into 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
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.
EDIT: I should note we are getting the selected file, removing the / at the beginning of the path, then replacing all subsequent / with > (space, closed bracket, space).
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
To this:
Company Drive > Teams > STU - Studio Team > Livestreaming > Webinars > Walk Away > Scripts > 2022-01-06 - Walk Away and Watch It Grow.tp3
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
end tell
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: