Automatically Opening Your Own Downloaded Files With Safari

This is one of those totally basic operations that is right in your face with Firefox (“download file, or open with application ‘blah’ “), but with Safari it’s a battle with MIME types, “Universal” Type Identifiers, and file extensions. For the quick fix, copy com.apple.DownloadAssessment.plist into ~/Library/Preferences in your home directory and add to the list of file extensions that you want automatically opened after download (aka “Safe Files”).
I thought it would be straightforward to write a CGI script that emits CSV and have that automatically be opened in Excel. Good lord, what a pain. First of all, Safari has no easy way to associate a MIME type with an application. But can associate an application with a given file extension by doing a “Get Info” on the file in Finder … look for “Open with:”.
So I had to find and download a helpful app called RCDefaultApp. It installs as a System Preference and allows you to change all the crazy Launch Services business (associations MIME types, UTIs, file extensions, etc).
The official MIME types for CSV are text/csv and text/comma-separated-values. Unfortunately Safar breaks this. As far as I can tell, Safari just automatically displays “text/*” inline in the browser. So I used RCDefaultApp to defined “application/csv” (yeah, I guess I should have made that “application/x-csv”), and associated that with Excel.
Here is the perl code I used to test this whole mess:
#!/usr/bin/perl -w $result = "foo,bar,baz\n"; print "Content-type: application/csv\n\n$result";
And here is the plist file just to give an idea of what it looks like:
LSRiskCategorySafe LSRiskCategoryExtensions csv
Figuring out how to tell Safari that CSV was a “Safe File” was harder to track down. It’s totally undocumented, but a couple of hardy Mac OS X nerds have uncovered the fact that com.apple.DownloadAssessment.plist holds the key to the magic. It allows you to override the download assessments that are hard coded into Launch Services. A post over at MacEnterprise.org provides some great documentation on exactly how to set this up.
The asute reader will notice that my com.apple.DownloadAssessment.plist file has a definition for the “csv” file extension rather than a MIME type. That’s because to get things to work at all, I had to actually make my CGI script have a filename that ends in “.csv”, which is totally crazy. Otherwise Safari downloads the file alright, but it saves it with a file extension of “.pl” or “.cgi”, and then cleverly reminds us that we might be downloading a PERL executable. In fact it is simply CSV text with a “.pl” extension. So it looks like the key steps here were:
- renaming CGI script to so it downloads with the proper “csv” file extension
- use Finder to make “csv” files open with Excel
- construct and install a com.apple.DownloadAssessment.plist so Safari will automatically open the “csv” file after download.
Gosh, Mac’s are so easy to use, I can just hardly believe it :-) Sorry folks, I think this is a case where the “PC guy” wins, hands down.
Link to Modifying Safari Safe Files List
Link to RCDefaultApp for editing MIME type associations

wow, just found this little gem on the web a coupla weeks ago.
nice formatted, colored XML in hierarchy.
and you can opt to see the XML “raw” or different options and even customize it.
http://www.entropy.ch/software/MacOSX/xmlviewplugin/