Question

I want to create an automator service that copies the full smb:// path of a folder or file (like you would get from + + + C)

The reason the shortcut isn't enough is for all those "older people" (no offense intended) that can't remember shortcuts or don't want to use them at all (the typical rightclick -> copy instead of + C users).

So far I got the POSIX path but how do I transform it to my correct smb:// path?

on run
    tell application "Finder"
         set theItem to selection as string
    end tell
    set posixForm to POSIX path of theItem
    set the clipboard to posixForm
end run

So this path /Volumes/someFolder/someSubFolder/ would become this smb://Server/someFolder/someSubFolder/

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top