Convert a PC path to Mac path with AppleScript

applescriptfinderpath

I am a lone Mac user in a company that uses PCs…I often get file paths that I need to manually navigate. I have found some scripts, but they don't seem to be working for me. I'd like to be able to right click on a highlighted path and click on a script that directs me to Finder and the associated file. Here is an example of my PC path vs Mac path:

PC Path:
P:\city\projectname

Mac Version of the same path:
smb://perkinswill.net/projects/city/projectname

The code I found thus far is the following:

Script: “Convert Windows to Mac path and open it”

 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 myClip to the input
            set mytext to searchReplace(myClip, "<", "")
            set mytext to searchReplace(mytext, ">.", "")
            set mytext to searchReplace(mytext, ">", "")
            set findIt to "\\"
            set replaceIt to "/"
            set mylocation to searchReplace(mytext, findIt, replaceIt)
            set mylocation to "smb:" & mylocation
            tell application "Finder"
                            open location mylocation
            end tell
            return input
 end run

 -- Thanks to: http://apple.stackexchange.com/questions/144916/how-to-change-filepath-structure -using-automator-windows-to-mac --

I appreciate your help!

Best Answer

From what I can see, there's nowhere that P: gets replaced by your Mac path smb://perkinswill.net/projects. Adding another replace for that seems to get the script working for me, see below. If you have other servers with other paths, you can add those in in a similar fashion.

Note that by changing the actual opening command from tell application "Finder" to do shell script it also handles spaces in paths, at least in my environment (which is slightly different from yours).

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 myClip to the input
    set mytext to searchReplace(myClip, "<", "")
    set mytext to searchReplace(mytext, ">.", "")
    set mytext to searchReplace(mytext, ">", "")
    set findIt to "\\"
    set replaceIt to "/"
    set mylocation to searchReplace(mytext, findIt, replaceIt)

    -- Add these three lines for any path you wish to be able to open
    set winPath to "P:"
    set macPath to "smb://perkinswill.net/projects"
    set mylocation to searchReplace(mylocation, winPath, macPath)

    do shell script "open " & quoted form of mylocation

    return mylocation
end run