Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@phillymjs
Last active March 30, 2017 17:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillymjs/11b8551a875c9506ed0eeb56cd724fcb to your computer and use it in GitHub Desktop.
Save phillymjs/11b8551a875c9506ed0eeb56cd724fcb to your computer and use it in GitHub Desktop.
Select a file/folder on a mounted server, then run this script. It will ask you if you need the Mac path, the Windows, path, or both, and then put the path(s) on the clipboard. Updated 7/25/2016 to correctly deal with paths for CIFS connections, instead of treating them as SMB.
global mac_path, win_path
tell application "Finder"
try
set theFilePOSIX to POSIX path of (selection as string)
on error
display dialog "Please select a file on a server volume before running this script." buttons {"OK"} default button "OK" with icon 0 with title "Error - Nothing Selected"
return
end try
set TempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
if (text item 2 of theFilePOSIX) is not "Volumes" then
display dialog "Please select an item on a network volume before running this script." buttons {"OK"} default button "OK" with icon 0 with title "Error - Local Item Selected"
return
end if
set theShare to (text item 3 of theFilePOSIX)
set theURL to "/" & ((text items 3 through (count of text items of theFilePOSIX)) of theFilePOSIX) as string
set AppleScript's text item delimiters to TempTID
tell application "System Events"
--We assume SMB and then check for AFP and CIFS
set theProtocol to "smb://"
set theMountString to do shell script "mount | grep " & theShare
if (offset of "afpfs," in theMountString) > 0 then
set theProtocol to "afp://"
else
set SMBtype to do shell script "smbutil statshares -m /Volumes/" & theShare
if (offset of "SMBV_NEG_SMB1_ONLY" in SMBtype) > 0 then
set theProtocol to "cifs://"
end if
end if
set theServerString to do shell script "mount | grep " & theShare & " | awk '{ print $1 }'"
set theFileServer to (characters ((offset of "@" in theServerString) + 1) through (count of characters of theServerString) of theServerString) as string
set theFileServer to (characters 1 through ((offset of "/" in theFileServer) - 1) of theFileServer) as string
end tell
set theFileServer to (characters 1 through ((offset of " " in theFileServer) - 1) of theFileServer) as string
--Use this if you prefer mounting the enclosing folder as the share (new Finder behavior)
--set mac_path to theProtocol & theFileServer & theURL
--Use this if you prefer mounting the root level of the share ("classic" Finder behavior)
set mac_path to "file:///Volumes" & theURL
-- Call subroutine to replace Mac path characters
my replace_mac_character(" ", "%20")
set win_path to "\\\\" & theFileServer & theURL
-- Call subroutine to replace Win path characters
my replace_win_character("/", "\\")
my replace_win_character(" ", "%20")
set pathChoice to button returned of (display dialog "Which path(s) do you need?" buttons {"Mac", "Windows", "Both"} default button "Both" with title "File Path Options")
set macClipboard to "Mac Path" & return & "1. Click this to mount the share: " & theProtocol & theFileServer & "/" & theShare & return & "2. Click this to go to the folder: " & mac_path
set winClipboard to "Win Path" & return & win_path
if pathChoice is "Mac" then
set the clipboard to macClipboard
else if pathChoice is "Windows" then
set the clipboard to winClipboard
else
set the clipboard to macClipboard & return & return & winClipboard
end if
end tell
-- Subroutine which replaces old Mac characters with new Mac characters
on replace_mac_character(oldChar, newChar)
set TempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to oldChar
set mac_path to text items of mac_path
set AppleScript's text item delimiters to newChar
set mac_path to mac_path as string
set AppleScript's text item delimiters to TempTID
end replace_mac_character
-- Subroutine which replaces old Win characters with new Win characters
on replace_win_character(oldChar, newChar)
set TempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to oldChar
set win_path to text items of win_path
set AppleScript's text item delimiters to newChar
set win_path to win_path as string
set AppleScript's text item delimiters to TempTID
end replace_win_character
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment