Copy Windows File Path

In addition to never understanding scripting much beyond the days of BASIC, I have two problems I need some help with.

  1. I need to be able to copy a Windows file path to the clipboard.
  2. I’d like to be able to click on a file path in Entourage and have that folder open up in the finder.

For problem #1, the path name needs to be in this format in order for the Windows people to find it useful. (Otherwise, I get panicky emails from them):
\ntfiles\mktg{Directory}{SubDirectory}{Filename.ext}

¢ Pathsnagger is close, but it leaves off the “\ntfiles” part of the path.
\mktg{Directory}{SubDirectory}{Filename.ext}

¢ An Applescript using the POSIX path is also close, but it adds a "Volumes" to the path.
\ntfiles\Volumes\mktg{Directory}{SubDirectory}{Filename.ext}

Does anyone know a way that I can copy a path to the clipboard in the format that I need? Preferably via a contextual menu when I right-click on a file.

For problem #2, I don’t even know if it’s possible. My Windows colleagues can click on the file path link, and the appropriate folder or file opens up. For us, nothing happens. Is there any way to get the file link path to automatically open in Finder? Even if we have to copy and paste the link into another script.

Thanks for your help.

Hi,

Problem #1

save the script anywhere as application and drag the icon into a Finder window next to the search field.
Then select folders / files and click the icon or drag items onto the icon.
The result will be copied to the clipboard


on run
	tell application "Finder" to set sel to selection
	if sel is "" then display dialog "Nothing selected" buttons {"Cancel"} default button 1
	open sel
end run

on open theseItems
	set resultList to {}
	set TID to text item delimiters
	repeat with oneFile in theseItems
		set text item delimiters to ":"
		set filePath to text items of (oneFile as text)
		set text item delimiters to "\\"
		set end of resultList to "\\\\" & filePath as text
	end repeat
	set text item delimiters to return
	set resultList to resultList as text
	set text item delimiters to TID
	set the clipboard to resultList
end open

If you quickly want to go to a folder in Mac OS X, then you can use the following Finder menu:

Finder > Go > Go to Folder.

The upcoming dialog only accepts Posix paths (e.g. /Users/martin/Desktop/).

If you want to quickly open both folders and files, then you might consider to create a script that utilizes the open command:


set desktoppath to quoted form of POSIX path of (path to desktop)
set command to "open " & desktoppath
do shell script command

set filepath to quoted form of POSIX path of ((path to preferences folder from user domain as Unicode text) & "com.apple.mail.plist")
set command to "open " & filepath
do shell script command

More convenient version (also expects Posix paths):


on run
	try
		set itempath to my getitempath()
		if itempath is missing value then
			return
		end if
		set itempath to quoted form of itempath
		set command to "open " & itempath
		do shell script command
	on error errmsg number errnum
		tell me
			activate
			display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop giving up after 30
		end tell
	end try
end run

on getitempath()
	try
		tell me
			activate
			display dialog "Please enter a file or folder path:" default answer "" buttons {"Cancel", "Enter"} default button 2
			set dlgresult to result
		end tell
	on error
		return missing value
	end try
	set itempath to text returned of dlgresult
	return itempath
end getitempath

StefanK - Thanks! I like having the app by the search window. Great idea.

Is there any way to make it copy a path with "\ntfiles" in front of it?

Here’s what I’m getting: \mktg{Directory}{Subdirectory}{filename.ext}
Here’s what I need: \ntfiles\mktg{Directory}{Subdirectory}{filename.ext}

I can add "\ntfiles" to the front, but I get some extra slashes.

(I probably should have mentioned that I haven’t programmed much of anything since the days of BASIC.)

Martin – I’m still having trouble getting the script to work. The first one keeps opening a mail pref file. The second results in an error message. Do the slashes used in the POSIX path need to be converted to colons for a Mac path?

Thanks again for all of your help.

Of course, change this line


set end of resultList to "\\\\ntfiles\\" & filePath as text

That’s okay :wink: The first script is just an example how to open file or folder paths with the open command and the mail prefs file is just a sample file used for demo purposes.

No, don’t convert slashes to colons, the script expects Posix paths. It should work just fine when you enter a path like follows:

/Library/Application Support

StefanK - THANK YOU! That works perfectly.

Martin - I’m still having a bit of trouble getting the script to work. I really like the option of having a window pop up to copy the path into. However, I’m still unable to make it work.

For example, I’ll receive this link via email:
\ntfiles\mktg{Directory}{Subdirectory}{filename.ext}
Where {Directory} {Subdirectory} and {Filename} can be anything from a folder to a Photoshop file on our Windows network.

Copying the whole link into the box results in an error. I think the script is expecting slashes that go in the opposite direction. How do I remedy that?

Sorry, I didn’t expect you to enter Windows-like paths into the dialog box, but thought you need a way to handle Posix paths. Of course, in case you want to process Windows-like paths, then you will need to convert the slashes:

(I don’t know about the two slashes at the beginning, you might need to remove one of them…)


on run
	try
		set itempath to my getitempath()
		if itempath is missing value then
			return
		end if
		set itempath to my searchnreplace("\\", "/", itempath)
		set itempath to quoted form of itempath
		set command to "open " & itempath
		do shell script command
	on error errmsg number errnum
		tell me
			activate
			display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop giving up after 30
		end tell
	end try
end run

on getitempath()
	try
		tell me
			activate
			display dialog "Please enter a file or folder path:" default answer "" buttons {"Cancel", "Enter"} default button 2
			set dlgresult to result
		end tell
	on error
		return missing value
	end try
	set itempath to text returned of dlgresult
	return itempath
end getitempath

on searchnreplace(searchstr, replacestr, txt)
	considering case, diacriticals and punctuation
		if txt contains searchstr then
			set olddelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {searchstr}
			set txtitems to text items of txt
			set AppleScript's text item delimiters to {replacestr}
			set txt to txtitems as Unicode text
			set AppleScript's text item delimiters to olddelims
		end if
	end considering
	return txt
end searchnreplace

Martin - I got it working. Thanks.

I had to change one line, as in order to access the network, the file path needs to be preceded by “Volumes/”

I truly appreciate everyone’s help. Thanks again.



on run
	try
		set itempath to my getitempath()
		if itempath is missing value then
			return
		end if
		set itempath to my searchnreplace("\\", "/", itempath)
		set itempath to quoted form of itempath
		set command to "open " & "Volumes/" & itempath
		do shell script command
	on error errmsg number errnum
		tell me
			activate
			display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop giving up after 30
		end tell
	end try
end run

on getitempath()
	try
		tell me
			activate
			display dialog "Please enter a file or folder path:" default answer "" buttons {"Cancel", "Enter"} default button 2
			set dlgresult to result
		end tell
	on error
		return missing value
	end try
	set itempath to text returned of dlgresult
	return itempath
end getitempath

on searchnreplace(searchstr, replacestr, txt)
	considering case, diacriticals and punctuation
		if txt contains searchstr then
			set olddelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {searchstr}
			set txtitems to text items of txt
			set AppleScript's text item delimiters to {replacestr}
			set txt to txtitems as Unicode text
			set AppleScript's text item delimiters to olddelims
		end if
	end considering
	return txt
end searchnreplace

As I said, I figured out how to add the text "Volumes" to the beginning of the path. That had me much more excited than I had any right to be.

Anyway, I’d like to set up some sort of conditional where if the path begins with either "\Ntfiles" or "\ntfiles" that it would be replaced by “Volumes/”.

Secondly - and this is a longshot - I’d like to have another conditional where if the path points to a filename, it opens the enclosing directory.
So if I put either
“\ntfiles\mktg{Directory}{Subdirectory}{filename.ext}”
or
"\ntfiles\mktg{Directory}{Subdirectory}"
into the box, it opens “Volumes/mktg/Directory}/{Subdirectory}/”

Does that make sense? Is that even doable? Is this asking far too much?

That’s what scripting is all about :smiley: Here is some quick and dirty code:


on run
	try
		set itempath to my getitempath()
		if itempath is missing value then
			return
		end if
		-- replacing \\ntfiles\ with \Volumes\
		if itempath begins with "\\\\ntfiles\\" then
			set itempath to my searchnreplace("\\\\ntfiles\\", "\\Volumes\\", itempath)
		end if
		set itempath to my searchnreplace("\\", "/", itempath)
		-- folder or file?
		set iteminfo to info for (POSIX file itempath)
		if not folder of iteminfo then
			-- displaying the container of the file in the Finder
			tell application "Finder"
				activate
				reveal container of ((POSIX file itempath) as alias)
			end tell
		else
			-- opening the folder with the open command
			set itempath to quoted form of itempath
			set command to "open " & "Volumes/" & itempath
			do shell script command
		end if
	on error errmsg number errnum
		tell me
			activate
			display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop giving up after 30
		end tell
	end try
end run

on getitempath()
	try
		tell me
			activate
			display dialog "Please enter a file or folder path:" default answer "" buttons {"Cancel", "Enter"} default button 2
			set dlgresult to result
		end tell
	on error
		return missing value
	end try
	set itempath to text returned of dlgresult
	return itempath
end getitempath

on searchnreplace(searchstr, replacestr, txt)
	considering case, diacriticals and punctuation
		if txt contains searchstr then
			set olddelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {searchstr}
			set txtitems to text items of txt
			set AppleScript's text item delimiters to {replacestr}
			set txt to txtitems as Unicode text
			set AppleScript's text item delimiters to olddelims
		end if
	end considering
	return txt
end searchnreplace

Thanks Martin. And thanks for the comments, too. I’m a bit of a “brute force” programmer, so the comments help me understand things a bit more. I appreciate that.