p2 / quicklook-csv

A QuickLook plugin for CSV files
Other
289 stars 22 forks source link

[patch] Stop using dynamic UTIs and instead import the standard/system UTIs #12

Closed p2 closed 11 years ago

p2 commented 11 years ago

Original author: smokey.ardisson (January 03, 2013 03:08:43)

QuickLookCSV (v1.2)'s current plist references both CSV and TSV files with various dynamic UTIs that have been created on various users' Macs over time. This is bad because 1) the dynamic UTI used on a given Mac might not be in the list of UTIs in the generator's plist, causing the generator not to preview the file, and 2) a dynamic UTI in use on a given Mac for some other file type might match one of the UTIs listed in the generator's plist, causing the generator to try (and fail) to generate a preview for a file it doesn't understand.

Also, 3) the current .plist double-declares TSV, the second time around claiming that a TSV can be any file that is a traditional plain-text file (com.apple.traditional-mac-plain-text), which causes the generator to be called for any plain-text file, which produces less-than-desirable results for almost all plain-text files.

Instead, the generator should import[1] the standard and/or system-provided UTIs so that the proper UTIs are available on any Mac with the generator installed, regardless of the OS, and then only reference the standard UTIs when declaring supported file types in the CFBundleDocumentTypes keys.

I've had the CFBundleDocumentTypes cleanup hacked into my copy of v1.2 since October, when I ran into all three issues described in the first two paragraphs.

I mostly encounter .tsv files, but today I realized my .csv files weren't previewing and discovered they had dynamic UTIs. So I added the UTImportedTypeDeclarations array to the generator's plist. I then moved the generator from ~/Library/QuickLook to the Desktop, then back (to cause LaunchServices to reparse the updated plist), and then ran qlmanage -r to make quicklookd update the generators. With those changes, Spotlight (mdls) now reports that old and new .csv files now[2] have a UTI of public.comma-separated-values-text, and .csv files finally preview as expected using the generator.

Note this is tested on 10.5, which has no system-provided UTIs for CSV and TSV files; simply importing the declarations in the generator's plist and causing LaunchServices and Quick Look to update themselves after updating the plist is sufficient to get this working without the random dynamic UTIs, while not regressing 10.5.

I'm attaching a diff of my changes, which can be grouped as follows:

1) Flesh out the CFBundleDocumentTypes with MIME types for both CSV and TSV 2) Remove dynamic UTIs from CFBundleDocumentTypes for both CSV and TSV 3) Remove second TSV section from CFBundleDocumentTypes 4) Add UTImportedTypeDeclarations, with declarations for both CSV and TSV

1 and 3 are what I'd call "optional" changes; 2 and 4 are the core changes that fix the "missing UTI" problem.

I don't have any FileMaker .tab files (Issue 5 comment 9), but I don't see why, if they have the right extension (.tab/.tsv), they would not be handled correctly by the initial TSV section in CFBundleDocumentTypes. Even if for some reason they do need the any-plain-text-file-type com.apple.traditional-mac-plain-text UTI, that UTI still should be added to the main TSV section in CFBundleDocumentTypes instead of creating an extra, almost-duplicate TSV section. At any rate, if in the absence of test files for this change, you want to be extra-cautious to not regress them and therefore omit change 3, it's not the end of the world. It's not ideal, but it's still easy enough for people to delete that extra section or the com.apple.traditional-mac-plain-text UTI and get LaunchServices and quicklookd to take note.

[1] https://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/understanding_utis/understand_utis_declare/understand_utis_declare.html#//apple_ref/doc/uid/TP40001319-CH204-SW2

[2] New files get the correct UTI on creation/download; old files don't get the new UTI until they're moved or updated, but they now preview correctly regardless of UTI (and mdimport correctly decides old files should have the new UTI if you attempt to reimport).

Original issue: http://code.google.com/p/quicklook-csv/issues/detail?id=12

p2 commented 11 years ago

From p2 on February 01, 2013 13:58:51 This is great, thanks for putting all this work into this (and sorry for the late reply, I don't get email notifications on this??).

I'll give it a go over the weekend. And probably should move this to GitHub so people can create pull requests. Thanks!

p2 commented 11 years ago

Your change to UTImportedTypeDeclarations is interesting as back when I created the plugin, those were ignored for QuickLook plugins (they are still not mentioned in the programming guide).

p2 commented 11 years ago

I have accepted all your changes into 1.3, the download for 1.2 is still available in case this breaks for some FileMaker people. Thanks!