Copy path

Attempting to create a way for users to select a file, copy the path and then email that path. We have been working with this for a while, but it now work sporadically after upgrading team to Mavericks. Sometimes the script runs and sometimes it does not. Also on the receiving end, sometimes clicking the link works and sometimes it does not. I am running the script as as service via automator so the user simply right clicks to select ‘email path.’ Is there something inherently wrong with this script?

[tell application “Finder”
set theWindow to window 1
set thePath to (POSIX path of (target of theWindow as alias))
set fileAlias to the selection as alias
set fileName to name of fileAlias
set fileExtension to name extension of fileAlias
set the clipboard to “file://” & thePath & fileName
end tell
tell application “Mail”
make new outgoing message with properties {content:“Right click and select Open URL” & "
" & (the clipboard), subject:“file link”, visible:true}
end tell]


this is a solution without involving the clipboard and with considering multiple files.
The first line is necessary to avoid a bug that the selection property is not updated.
It might work also without the first line, check it out

activate application "SystemUIServer" -- works around a selection bug
tell application "Finder"
	set theSelection to (get selection)
	if theSelection is {} then
		display dialog "Nothing selected" buttons {"Cancel"} default button 1
	end if
	set urlList to {}
	repeat with anItem in theSelection
		set end of urlList to URL of anItem
	end repeat
end tell
set {TID, text item delimiters} to {text item delimiters, return}
set urlLines to urlList as text
set text item delimiters to TID

tell application "Mail"
	make new outgoing message with properties {content:"Right click and select Open URL" & return & return & urlLines, subject:"file link", visible:true}
end tell

Thanks Stefan this seems to work great.

Anyway to simply test the link for ‘spaces?’ The link won’t work if there are spaces. Ideally there would be a warning message ‘there are spaces’ and allows them to adjust them if necessary. We had automator replace the spaces with underscores but this caused other issues.

spaces are percent escaped (%20). This is URL standard