Finder handling URL

Here’s an easy one . hopefully. I know how to get an URL as an item property, but how do I tell the Finder to dereference that same URL? The script below demonstrates my point.

tell application "Finder"
	set x to document file "myfile.txt" of folder "stuff" of disk "myDisk"
	set y to URL of x
	exists x
	exists y
end tell

The first exists x returns true, but the second exists y returns false. Any ideas?

Other applications, such as QuickTime Player, can handle a URL using the open location key form, but Finder chortles at that syntax.

Hi, David.

  1. Is there any particular reason why you want to tell the Finder to dereference a URL? Are you not able to use a Finder reference (as in your script) or an alias?

  2. ‘open location’ is from the StandardAdditions’ Internet suite. It “opens a URL with the appropriate program”. Normally, this will be your default Web browser or e-mail client. In the case of a file URL, the “appropriate program” appears to be the application that “owns” the file. So using ‘open location’ with a file URL has the same effect as using the Finder’s ‘open’ command with a Finder reference or with an alias.

tell application "Finder"
	open document file "myfile.txt" of folder "stuff" of disk "myDisk"
end tell

-- Or:
tell application "Finder"
	open alias "myDisk:stuff:myfile.txt"
end tell

-- Or:
open location "file://localhost/Volumes/myDisk/stuff/myfile.txt"

Thanks you, Jacques.

Nigel, FileMaker (6) creates these text URL references with its Import Folder facility.

Drat. :confused: Not so fast, folks. It did not work. Still trying to coerce this little bugger.

Take a look at my test script:


tell application "Finder"
	set x to document file "myFile.txt" of folder "stuff" of disk "myDisk"
	exists x
	set y to URL of x
	exists y
	exists y as POSIX file
	exists POSIX file y
	open location y
end tell

Then see my result log:
tell application “Finder”
get document file “myFile.txt” of folder “stuff” of disk “myDisk”
document file “myFile.txt” of folder “stuff” of disk “myDisk”
exists document file “myFile.txt” of folder “stuff” of disk “myDisk”
true
get URL of document file “myFile.txt” of folder “stuff” of disk “myDisk”
“file://localhost/Volumes/myDisk/stuff/myFile.txt”
exists “file://localhost/Volumes/myDisk/stuff/myFile.txt”
false
exists file “:.:file/::localhost:Volumes:myDisk:stuff:myFile.txt”
false
exists POSIX file “file://localhost/Volumes/myDisk/stuff/myFile.txt”
false
open location “file://localhost/Volumes/myDisk/stuff/myFile.txt”
“Finder got an error: File some object wasn’t found.”

Thanks for looking

Model: G4
AppleScript: AppleScript 1.9.3
Browser: Firefox 1.0.6
Operating System: Mac OS X (10.3.9)

This is a bit crude, but it might get you started. It works on all the disks in my Jaguar system, but isn’t guaranteed. If your URLs contain any other “%” codes, you’ll have to write more substitution code into the handler.

on urlToAlias(theURL)
	set astid to AppleScript's text item delimiters
	
	-- Edit the URL so that it looks like a POSIX path.
	set AppleScript's text item delimiters to "file://localhost"
	set u to text item 2 of theURL

	-- Replace any "%20"'s with spaces.
	set AppleScript's text item delimiters to "%20"
	set u to u's text items
	set AppleScript's text item delimiters to space
	set u to u as Unicode text
	
	set AppleScript's text item delimiters to astid
	
	try
		-- Coerce the POSIX path to POSIX file and thence to alias.
		set theAlias to u as POSIX file as alias
	on error number -1700
		display dialog "There is either nothing at the URL \"" & theURL & "\" or this script needs a few more \"%\" substitutions." --with icon stop
		error number -128
	end try
	
	return theAlias
end urlToAlias

tell application "Finder"
	set x to document file "myFile.txt" of folder "stuff" of disk "myDisk"
	set y to url of x
end tell

set z to urlToAlias(y)

You need these two lists:

set a to {}
repeat with i from 0 to 255
	tell "0123456789ABCDEF" to set end of a to "%" & item (i div 16 + 1) of it & item (i mod 16 + 1) of it
end repeat
a

and

set b to {}
repeat with i from 0 to 255
	set end of b to ASCII character i
end repeat
b

So to decode you can use:
Evolved from one of Apple’s sub-routines:

set theURL to "file://localhost/Volumes/Macintosh%20HD/Users/foo/Desktop/My%20file.txt"

decodeURL(theURL)
--> "file://localhost/Volumes/Macintosh HD/Users/foo/Desktop/My file.txt"

on decodeURL(theURL)
	set asciiList to {"", "", "", "", "", "", "", "", "", "	", "
", "", "

", "
", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "", "Ä", "Ã…", "Ç", "É", "Ñ", "Ö", "Ãœ", "á", "à ", "â", "ä", "ã", "Ã¥", "ç", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ñ", "ó", "ò", "ô", "ö", "õ", "ú", "ù", "û", "ü", " ", "°", "¢", "£", "§", "¢", "¶", "ß", "®", "©", "â„¢", "´", "¨", "≠", "Æ", "Ø", "∞", "±", "≤", "≥", "¥", "µ", "∂", "∑", "∏", "Ï€", "∫", "ª", "º", "Ω", "æ", "ø", "¿", "¡", "¬", "√", "Æ’", "≈", "∆", "«", "»", ".", " ", "À", "Ã", "Õ", "Å’", "Å“", "“", "”", """, """, "˜", "'", "÷", "â—Š", "ÿ", "Ÿ", "⁄", "€", "¹", "º", "fi", "fl", "¡", "·", "š", "ž", "°", "Â", "Ê", "Á", "Ë", "È", "Í", "ÃŽ", "Ï", "ÃŒ", "Ó", "Ô", "", "Ã’", "Ú", "Û", "Ù", "ı", "ˆ", "Ëœ", "¯", "˘", "Ë™", "Ëš", "¸", "˝", "Ë›", "ˇ"}
	set hexList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
	repeat with i from 0 to 255
		set text item delimiters to "%" & item (i div 16 + 1) of hexList & item (i mod 16 + 1) of hexList
		set theURL to text items of theURL
		set text item delimiters to item (i + 1) of asciiList --ASCII character i
		set theURL to theURL as Unicode text
	end repeat
end decodeURL

URLs are UTF8 encoded, so that handler will break on any non-ASCII chars. (Tip: Apple’s ‘essential subroutines’ are rubbish and best ignored.) Try:

on urlToPOSIXPath(theURL)
   return do shell script "python -c \"import urllib, urlparse, sys; print urllib.unquote(urlparse.urlparse(sys.argv[1])[2])\" " & quoted form of theURL
end urlToPOSIXPath

on urlToPOSIXFile(theURL)
   return POSIX file urlToPOSIXPath(theURL)
end urlToPOSIXFile

Amazing! So, short of uprading everyone in my shop to OS 10.4, the call out to Python looks like the way to go. Thank you.

For my application, however, I think I need to use a calculation field (FMP) to transform FileMaker’s URL into an old-fashioned alias string. Something I did not underscore was that, unlike the URL that Finder makes, FileMaker’s is not an absolute file path:

FileMaker -
file://myDisk/stuff/myFile.txt

Finder -
file://localhost/Volumes/myDisk/stuff/myFile.txt

Your replies have been greatly appreciated.

Humm. That particular url is garbage. I think there’s a few apps that do this, but according to RFC1738 it’s wrong: the correct form for a file url is ‘file:///’. e.g. Your Finder example gets it right.

I’m not entirely sure how FMP comes up with that dodgy non-url url. Ironically enough, it actually happens to work in some situations where its various flaws accidentally coincide to mask one another out, but it’s still wrong and will cause problems sooner or later if you try to use it where a valid url is expected (e.g. my urlToPOSIXPath command).

Anyway, IIRC these junk urls don’t bother to %- escape unsafe characters either, so you may be able to create a usable posix path just by removing the “file://” bit at the start and replacing with “/Volumes/”. Though when dealing with such patent stupidity it’s hard to be sure. Good luck.