Need to automate an SMB path into a UNC path before sending to Win10 clients

applescriptautomatorNetworksmb

New here, but I'm having difficulty putting together an AppleScript that will basically take an SMB link and turn it into a UNC link before I send it over to a couple of Win10 clients.

All same network, we're all using a SNS NAS and it's for Adobe Premiere links. We can do this all manually, but the editors don't have time/patience to convert each link. We're looking for an automated script that we could integrate as a service that will convert selected text.

I found the article below, but it's the reverse to what we're looking for. I tried reversing all of the functions in it but wasn't able to get it to run properly.

https://gist.github.com/anonymous/2211017

It'd also be awesome if the 'service' that is created simple copies the entry into the clipboard so that the end-user can paste it into a couple of different things like email and Slack messenger.

Hopefully someone can help or steer me in the right direction. I've been scouring Google for this for about a week.

EDIT** Examples below

Old path
'smb://server/folder'

New path
'\\server\folder'

Best Answer

NOTE: This answer is intended only as an example when the SMB Path, aside from the colon, slashes and backslashes and without spaces, only contains alphanumeric characters in the name! If you need to account for anything else, i.e. read the comments to this answer, you'll need to modify the code according to your needs. That said, in all of my years as a Network Systems Administrator and Network Systems Engineer I never created shares containing spaces or other then the need for colons, slashes, backslashes and alphanumeric characters and therefore this simple example is all that I'd need to translate any, e.g. smb://server/folder to \\server\folder, SMB Path I ever used to a UNC Path. YMMV!


In Automator, create a new Service workflow, then add a Run AppleScript action to it while replacing the default AppleScript code with the code below:

on searchReplace(theText, SearchString, ReplaceString)
    set OldDelims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to SearchString
    set newText to text items of theText
    set AppleScript's text item delimiters to ReplaceString
    set newText to newText as text
    set AppleScript's text item delimiters to OldDelims
    return newText
end searchReplace

on run {input, parameters}
    set selectedText to item 1 of input as string
    set convertedText to searchReplace(selectedText, "smb://", "\\\\")
    set convertedText to searchReplace(convertedText, "/", "\\")
    set the clipboard to convertedText as string
    return convertedText
end run

Then save the Service as: Convert SMB Path to UNC Path

Now Convert SMB Path to UNC Path will appear on the Services menu or Services context menu when text is selected and when the Service is used, the UNC Path is placed on the Clipboard. (This of course assumes the selected text was actually an SMB Path, otherwise what's placed there is just whatever text was selected when the Service was run which will include any substitutions made.)

Example SMB Path smb://server/folder will be converted to UNC Path \\server\folder and placed on the Clipboard.

Note: Note the settings of the Service in the image below. The Output replaces selected text checkbox is unchecked. You may check it, if that's what you want, however, if the selected text is not actually replaceable then the Service will not appear on the Services menu or Services context menu.

Convert SMB Path to UNC Path Automator Service