AppleScript POSIX path to smb path

You may need to URL Encode some characters and spaces, however, here is some example AppleScript code that you may find helpful, although it only encodes spaces:

tell application "Finder" to ¬
    set theItem to the first item of ¬
        the (selection as alias list)

set posixForm to POSIX path of theItem

set {TID, AppleScript's text item delimiters} to ¬
    {AppleScript's text item delimiters, " "}
set foo to text items of posixForm
set AppleScript's text item delimiters to "%20"
set foo to foo as string
set AppleScript's text item delimiters to "/"
set foo to text items 3 thru -1 of foo
set foo to foo as string
set smbForm to "smb://server/" & foo as string
set AppleScript's text item delimiters to TID

set the clipboard to smbForm

Notes:

Have a look at Encoding and Decoding Text in the Mac Automation Scripting Guide.



Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.