Adding preview to (C#) source files in OS X

So I’ve been forced to use OS X a lot these days, what with an iOS project and teaching programming to kids (see Jono’s post). I’ve tried on a few occasions to get comfortable in OS X, and even own an old early G4-based macbook. Well, that is to say, I bought it from my sister for $50 and used it for a few months freshman year before lending it to a friend… long story short it was a nice little reliable laptop which I could carry around with me, check e-mail, browse the web, etc. Basically I used it as a netbook before netbooks were cool.

Obviously judging the OS X experience from my short time with a then-obsolete laptop is unfair, but I would say that that’s the type of use these computers are geared towards. I certainly consider myself a Windows power user–I’ve been using a Microsoft OS since DOS 5–but I’m still pretty clueless when it comes to OS X. I’m taking it slow, trying to learn about features as the need arises. I’m a fan of menu-bar activity monitors, though I haven’t settled on which is my favorite. One feature I’m very pleased with is the OS-wide quick-look preview, but I was disappointed to find that it didn’t know how to understand my .cs source files (or a few other types for that matter).  Not only that, but when it comes across a type that it can’t understand, the “default” is very minimal.  If the content is just text, why not show a text preview?  A quick Google search came up with a solution, but configuring it was a little hairy. Here’s what I did:

  • Google “OS X preview C#”
  • Discover preview feature is called “Quick Look”
  • Google “Quick Look Syntax Highlight”
  • Try just installing qlcolorcode
Alas, while there is default support for some filetypes (generally ones already recognized by OS X like .h, .m, .cpp) my .cs and .js files were still woefully un-preivew-able! As a note for those lazy users who dont’ want to read the qlcolorcode readme, the way to install a QuickLook plugin is simply to copy the binary to /Library/QuickLook or ~/Library/QucikLook
After some more digging through qlcolorcode’s FAQ and t3h internetz I found that I had to modify the system-wide UTI (always an unfortunate acronym) definition for cs and js files.  Basically, QuickLook hooks into a system-wide file-type identification system called Uniform Type Identifiers.  These identifiers are specified by some metadata included in the various applications which edit these files.  I headed over to  QLColorcode package to modify its Info.plist, which is a file that (presumably) can be found in the root of any app package.  For those total OS X noobs out there, a .app (or just an application) is actually a folder which the OS tricks you into thinking is a file.  If you control-click (or right-click) on the package and say “Show Package Contents” you can view the files inside this folder.  In this case, the QL library is just the same.
I meant to be a bit more organized about this article, citing more of the sources I found, but I did this so long ago that I don’t have all that info. Suffice to say, I “somehow” figured out that the cs entry of this info file should look something like this:
// -*- mode: XML; -*-

...

	UTImportedTypeDeclarations

		...

			UTTypeConformsTo

				public.source-code

			UTTypeDescription
			C# Source File
			UTTypeIdentifier
			com.microsoft.csharp-source
			UTTypeReferenceURL
			http://www.microsoft.com/
			UTTypeTagSpecification

				public.filename-extension

					cs

	...
So, with that done, everything works! Now when I hit space bar with a .cs script selected, I get a nice syntax-highlighted preview 🙂 My one complaint is that I can’t select text from within a preview, but oh well.

~ by Schoen on August 23, 2011.

4 Responses to “Adding preview to (C#) source files in OS X”

  1. If I’m following your solution here correctly, it didn’t work for me — I think you:
    -Installed qlcolorcode to ~/Library/QuickLook/
    -Modified the MD Info.plist as posted
    That said, my Info.plist already was as you posted. Did I miss something?

    • Damn. I was afraid of that. Like I think I mentioned, I did this a little while ago and I don’t fully remember what it was that I did to finally get it working, but I know the .plist file was the key. You may have to add this

      public.source-code

      to the .cs listing, but I can’t fully remember. Let me try this on another machine, and come back with an edit.

      Now that I look more clearly, I’m pretty sure I just added the lines above (post-edit) to the info.plist of qlcolorcode itself.

      Sorry for the confusion!

      • In my qlcolorcode info.plist (this post first referred to MD’s info.plist, no?), again I find that your posted xml is already there (a public.source-code entry for C# Source Files), so no edits necessary.
        Update on my situation: I am now indeed getting a preview for my C# files (I think that was just from rebooting since installing qlcolorcode), but alas, not color coded!

  2. thanks for this.

    you CAN select text in quick look!

    http://www.techrepublic.com/blog/mac/how-to-enable-copying-text-from-within-quick-look-previews-via-the-terminal/1850

    now everything is perfect. great!

Leave a Reply

Your email address will not be published. Required fields are marked *