Working with Windows paths in AppleScript

Ok here’s the deal,

I get literally hundreds of Windows (gasp!) paths to file via email every day. I’m new to AppleScript and I’m trying to figure out how to copy the windows path from the email and have it automatically reveal the file in the Finder. The main problem I’m stuck on is this, Windows file paths have backslashes. Here’s an example

F:\MyProject\NewStuff\ForDelivery

I need the script to kick back the result

/Volumes/serverMount/MyProject/NewStuff/ForDelivery

So I’m playing around with text item delimiters but I keep bumping my skull up against the fact that I can’t stop the backslashes in the Windows path from acting as an escape character.

Any help would be appreciated.

This is pretty easy to do - just don’t get confused with the need to escape characters within your script.

This handler will take a Windows-based path and return a unix-style path. The input can be from anywhere - a paragraph from the email, a dialog input, or hardcoded in the script (as seen here, in which case it needs to be escaped):

on win2MacPath(WinPath)
	set my text item delimiters to "\\"
	set filePath to text items 2 through -1 of WinPath
	set MacPath to {"Volumes", "serverMount"} & filePath
	set my text item delimiters to "/"
	return "/" & MacPath as text
end win2MacPath

my win2MacPath("F:\\MyProject\\NewStuff\\ForDelivery")

Nice little code snippet, Camelot! :slight_smile:

Thanks Camelot!

This works great. So simple. Thanks a million.

I know this post is pretty old, but I was in the same boat as the original poster. I wanted to take the path from an e-mail sent by a Windows user, convert it to POSIX and then browse to the path or open the file. So I took from a couple of different scripts and came up with this solution:


on win2MacPath(WinPath)
	set my text item delimiters to "\\"
	set filePath to text items 2 through -1 of WinPath
	set MacPath to {"Volumes", "ShareName"} & filePath
	set my text item delimiters to "/"
	return "/" & MacPath as text
end win2MacPath

set xfr_clip to (the clipboard as text)

set pathText to xfr_clip

display dialog "Open File" buttons {"Cancel", "Open File"} default button 2

if the button returned of the result is "" then
	Cancel
else
	set the_path to (POSIX file win2MacPath(pathText))
	tell application "Finder"
		make Finder window
		set target of window 1 to the_path
		open the_path
	end tell
end if

The script actually takes the path from your clipboard and prompts you to “Open the File” or “Cancel”. This will look at a specific mounted share (in this case ShareName.) You could potentially add in a condition to handle different, frequently used shares based on the mapped network drive letter.

Hope this helps someone, somewhere.