-- Script to put UNC path to selected files on clipboard

-- Initialize reference lists
set serverList to {}
set shareList to {}
set mountPathList to {}

-- Build list of mounted SMB volumes
set mountedText to do shell script "df -T smbfs | cut -f 2 -d @ -s"

repeat with wLine in (every paragraph in mountedText)
	
	set AppleScript's text item delimiters to "/"
	
	copy text item -1 in wLine to end of mountPathList
	
	set AppleScript's text item delimiters to " "
	copy text item 1 in wLine to scratch
	
	set AppleScript's text item delimiters to "/"
	set tempList to text items of scratch
	
	set AppleScript's text item delimiters to "\\"
	set scratch to (tempList as text)
	
	copy scratch to end of shareList
	set AppleScript's text item delimiters to ""
	
end repeat

tell application "Finder"
	-- Get list of selected files or folders
	copy selection to selectedList
	
	set outputPathList to {}
	
	-- Iterate over the list of items, constructing paths to each
	repeat with anItem in selectedList
		-- Get Unix style path, e.g. /Volumes/tempbox/dir/file
		set currentPath to POSIX path of (anItem as alias)
		
		-- Split path into words at / delimiters, store in list
		set AppleScript's text item delimiters to "/"
		set partsList to text items of currentPath
		set AppleScript's text item delimiters to ""
		
		-- Test share name against list of mounted smb shares; if in that list and mounted under /Volumes , build UNC path; if not, copy POSIX path
		ignoring case
			if (item 3 of partsList is in mountPathList) and (item 2 of partsList is "Volumes") then
				
				repeat with i from 1 to the count of mountPathList
					if item i of mountPathList is (item 3 of partsList) then
						set indexRef to i
					end if
				end repeat
				
				-- Build server and share prefix. Use actual share name in case of "-1" mount point renaming phenomenon on user's machine
				set prefix to "\\\\" & (item indexRef of shareList) & "\\"
				
				-- Reassemble path, stripping Volumes off front and adding prefix
				set AppleScript's text item delimiters to "\\"
				set workingPath to (items 4 through -1 of partsList as text)
				set workingPath to prefix & workingPath
				
				-- Replace any spaces in path URL with %20
				set AppleScript's text item delimiters to " "
				set partsList to text items of workingPath
				set AppleScript's text item delimiters to "%20"
				set workingPath to partsList as text
				set AppleScript's text item delimiters to ""
				
				-- Put completed path on list
				copy workingPath to end of outputPathList
				
			else -- copy POSIX path
				
				-- Replace any spaces in POSIX path with "\ "
				set AppleScript's text item delimiters to " "
				set partsList to text items of currentPath
				set AppleScript's text item delimiters to "\\ "
				set currentPath to partsList as text
				set AppleScript's text item delimiters to ""
				
				copy currentPath to end of outputPathList
				
			end if
		end ignoring
		
	end repeat
	
	-- Convert list to text and place on clipboard
	set AppleScript's text item delimiters to return
	set outputString to outputPathList as string
	set AppleScript's text item delimiters to ""
	set the clipboard to outputString
end tell