I try to hack together a Keyboard Maestro shortcut to give me the URL to a file (just like the ones Path Finder outputs).
So far I use the following AppleScript and replace the spaces with %20:
tell application "Finder"
set sel to the selection as text
set the clipboard to POSIX path of sel
end tell
Then I simply append file://localhost/
.
The problem is when it comes to special characters, for example I got the following folder on my Desktop:
My output: file://localhost//Users/patte/Desktop/#%20Old%20files
The correct output should convert the hash: file://localhost/Users/patte/Desktop/%23%20Old%20files
A solution with AppleScript or Shell script would be great since I'm capable of incorporate this. I also tried set the clipboard to URL of the first item of (get the selection)
but this hasn't worked for me - maybe I did it wrong.
Another option would be a script that encodes the special characters - I could also work with that, but I'm not sure what to convert into what - else I'd have look for it.
this is lifted almost verbatim from this answer i.e. leverage python's urllib to quote the string appropriately before adding in the file://localhost
to the start of the string
on path2url(thepath)
return do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of thepath
end path2url
tell application "Finder"
set sel to the selection as text
set the clipboard to "file://localhost" & my path2url(POSIX path of sel)
end tell
I've added parentheses around the print in order to make the python script compatible between python 2.x and python 3.