I’m trying to create a script that lets you drop a file on it, and have a suitable path copied to the clipboard so that it can be pasted into a Mail message and sent to other people in my office as a file:// link., so that when they click the link it will take them to the file automatically. I’ve pieced together this script, and everything seems to work except the functioning link itself - it appears in mail as a link, it just fails as a link.
Anyone know why?
on open (this_alias)
try
set this_alias to this_alias as list
set unixList to {}
repeat with thisItem in this_alias
set unixpath to POSIX path of (thisItem as string) -- convert item in this_alias to unix path
if number of items in unixList = 0 then -- ad item to unixList and insert a space if more than one item
set unixList to unixList & unixpath
else
set unixList to unixList & " " & unixpath -- space used as delimeter, but might well be ":"!
end if
end repeat
set find to " "
set replace to "%20" as string
set fileText to unixList as string
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set fileText to every text item of fileText
set AppleScript's text item delimiters to replace
set fileText to (fileText as text)
set AppleScript's text item delimiters to ASTID
set the clipboard to "file://localhost" & fileText as text
on error myErr
display dialog myErr
end try
end open
This works for me to make a good file url in the clipboard:
set x to POSIX path of (choose file)
set y to do shell script "python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of x
set the clipboard to "file://" & POSIX path of y
The URL only works after I receive the email, not before it’s sent (If that is what you mean).
Thanks for the reply - but that doesn’t seem to work for me, even after emailing it - the path will come out to :path:to:me format instead of “/path/to/me”, which i believe it needs to be(?). Also, an extra “/” is placed in my path.(?) For example, the resultant path for a file called test.txt on my desktop gives:
file:///:Users:kyle:Desktop:test.txt
if i change the last line in the script to:
set the clipboard to "file:/" & y
then the path looks formed correctly, and the link will become active once emailed, and results in this:
file://Users/kyle/Desktop/test.txt
but the link then gives an error similar to this, which seems odd as the original file will open in text edit directly when clicked manually from the desktop.
“Mail was unable to open the URL “file://blah/blah/blah.pdf”. No associated application could be found.”
My last script works on 10.4, but this will probably work better on 10.3:
set x to POSIX path of (choose file)
set y to do shell script "python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of x
set the clipboard to "file://" & y
The file:/// protocol does have three slashes, I don’t know why.
thanks! - that works like a charm. I just wrapped your script with an “on open” command to make it drag and droppable:
on open (this_alias)
try
set x to POSIX path of (this_alias)
set y to do shell script "python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of x
set the clipboard to "file://" & y
end try
end open