
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
Lauren 1:26 pm on December 11, 2006 Permalink |
Cool teacups. But I’m really writing because I couldn’t help but notice the “Great Books”, Philosophy and Latin titles in your Amazon Feed (Stuff We Love).
All this jumped out at me because I’m currently taking Latin at Berkeley Extension and am reading my way through the Great Books following the St. John’s College list (I’ve made it to Aristotle).
I gathered from other stuff on this blog that you’re in SF. I live in Bernal and one of my classmates lives in the Mission/Noe. (She’s also taking Philosophy at Cal.)
If you are interested in connecting with a couple of laid back, like-minded people, send me an email. (I couldn’t find an email address on this blog, so I figured I’d just post a comment here in hopes that you’ll be able to access my email address from the comment form.)
It’s just so rare to run across someone who’s interested in this stuff, especially here in SF . . .
Vale!
Lauren
rajbot 2:54 pm on December 11, 2006 Permalink |
When (if?) we get our first check from the Amazon referral fees we should buy drinks for all the TR readers at Wild Side West!
may 9:28 pm on December 11, 2006 Permalink |
I just sent an email to Lauren, but to all the other TR readers who might happen upon this…the links in our sidebar are actually chosen by Amazon based on things it thinks *you’ll* like and not necessarily stuff we like (though chances are high that we’d like it too :-) There’s more info on Amazon’s Omakase thingy over here. Anyways, hope that clears up the confusion! :-) (also kudos to Lauren for making it through all the classics! I wish i were determined enough to do that!)