Encoded file URL to Mac or POSIX path...

Assuming a file named “éøü¥” exists on the desktop, getting a file URL for it is useful for some purposes…

tell application "System Events"
	set target_file to "Macintosh HD:Users:username:Desktop:éøü¥" as alias
	set target_URL to URL of target_file
end tell

… returns “file://localhost/Users/username/Desktop/e%CC%81%C3%B8u%CC%88%C2%A5”.

Anyone know how to easily coerce that back to an unencoded Mac or Posix file path?


Peter B.

This is a hard one. Right off the bat, the é and the ü don’t look right, but the url works!


If you drop the file://localhost/ link into an HTML page with ‘x-mac-roman’ as a charset, the characters will appear properly.

Peter B.

Hi Peter,

I copied hhas script from here:


set theURL to "file://localhost/Users/kel/Desktop/e%CC%81%C3%B8u%CC%88%C2%A5"
set posixPath to do shell script ("python" & " -c 'from sys import argv; from urlparse import urlparse; from urllib import unquote; print unquote(urlparse(argv[1])[2])' " & quoted form of theURL)

I tried to do it with urllib.unquote earlier and it didn’t work. Wonder how that other stuff made it work.

Edited: I think I was using unquote wrong. Yeah, I was using the whole url, but you need the parts of the tuple I think. Anyway,

Edited: Here’s the uncrunched version if you want. Still don’t understand the argv part.

set f to choose file
tell application "Finder" to set theURL to url of f
set py_text to "'
from sys import argv
from urlparse import urlparse
from urllib import unquote
print unquote(urlparse(argv[1])[2])' "
set py_com to ReplaceText(py_text, return, ASCII character 10)
set u_path to (do shell script "python -c " & py_com & quoted form of theURL)
set item_ref to u_path as POSIX file as alias
on ReplaceText(t, s, r)
	set utid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to s
	set temp_list to text items of t
	set AppleScript's text item delimiters to r
	set temp_text to temp_list as string
	set AppleScript's text item delimiters to utid
	return temp_text
end ReplaceText

Edited: oh it’s the ‘print’ I was missing earlier. You could have just done this if you had the last part of the local file url (the name).

python -c ’
import urllib
print urllib.unquote(‘e%CC%81%C3%B8u%CC%88%C2%A5’)’

Now I can go to sleep. Goodnight.


Thanks for the effort…

I’m saving the thread, but I’m lousy with shell scripting and I’m completely in the dark when it comes to Python (even though I know has ‘knows best’).

Sleep well.

Peter B.

Hi Peter,

I’m wondering where do you need this in practice?

StefanK wrote:

I have a script that ‘receives’ the file URL to open the location… but I can’t do anything more with it without decoding.

has also offers this osax:


… that includes the command ‘decode URL’… which I haven’t tried yet… but will get around to when I can.

In fact, once again, I’m trying to satisfy my curiosity as much or more than I’m seeking utility.

Peter B.

argv is an array of arguments from the command line (in this case, the URL).

I’ve used unquote before, but your method above to seems work better. I’ve added this to my collection:

on decodeURL(someURL)
	return do shell script "/usr/bin/python -c '" & ¬
		"from sys import argv; " & ¬
		"from urllib import unquote; " & ¬
		"from urlparse import urlparse; " & ¬
		"print unquote(urlparse(argv[1])[2])' " & quoted form of someURL
end decodeURL

--> "/Users/username/Desktop/éøü¥"

Kel & Bruce:

Sorry I dropped out of the thread…

Just wanted to thank you both… I’ve got something I can ‘play with’ now, which is a whole lot more than I had before. I wouldn’t have had a prayer of working out a solution on my own… most shell scripting is entirely opaque to me… and probably always will be.

So, thanks.

Peter B.

Hi Bruce,

I found out what was puzzling me about the script. I was thinking the [2] was argv[2], but there was only 1 passed argument. I did some review with Python and now see clearly that it was a slice, the third item of the tuple, the path.