February 2011

Why I Loved The Social Network

I'm writing this the night before the Oscars, but that is not why I'm writing. I only saw three of the films nominated for Best Picture: The King's Speech, True Grit, and The Social Network. While I enjoyed The King's Speech and True Grit,, I haven't thought about them since I saw them. In contrast, I still think about The Social Network every day.

For some, The Social Network is just a story about how an arrogant jerk became a billionaire by screwing over his friends and business associates. I didn't see it that way. To me, it is a story about the nature of creativity and invention.

Setting Up for Use of Microsoft Symbol Server

When debugging native Win32 code, it is useful to have the debug symbols for all of Microsoft's DLLs. The easiest way to set this up is to just set an environment variable before starting Visual Studio (or other Microsoft debugging tools):

set _NT_SYMBOL_PATH=srv*c:\symbols*http://msdl.microsoft.com/download/symbols

The first time you run the debugger after setting this, it will take some time to start as it downloads symbol files from the Internet into your local symbol cache, but it will be faster after that. Whenever you update your system with patches or service packs, the new symbols will automatically be downloaded the next time you debug.

For more information, see these pages:

(This post is really for my own benefit. I have trouble finding this information whenever I set up a new development workstation, so I'm putting it somewhere I'll know to look.)

Visual Studio 2008 Code Snippet for Inserting a String.Format() call

Over the past year I've been (re-)learning how to use Visual Studio 2008. I did a lot of work in the 90's and early 00's with Visual Studios 5, 6, and 2003, but then I had a few years away from Windows development, and I've had limited experience with .NET development. So I'm constantly discovering "new things" about Visual Studio that my coworkers already know.

One nifty feature of Visual Studio is "code snippets", which are basically little bits of code that can be quickly inserted into a source file using Intellisense. The inserted code includes placeholders that can be replaced with whatever you need. For example, if you are typing away in a C# source file and type the keyword for, you'll see the Intellsense window pop up with "for" as the selected item, and if you hit the Tab key a couple of times a complete for statement with braces, a loop variable, and everything will be inserted. You can then hit Tab to move to the loop variable name (in case you don't like i) and again to go into the loop body.

(Other IDEs have similar features, calling them abbreviations, macros, etc. You don't need to tell me that Visual Studio isn't magically unique.)

Visual Studio has a whole bunch of these snippets built in, but you can also define your own by writing an XML file and saving it where Visual Studio can find it. For example, here is a snippet I wrote to quickly insert String.Format() expressions:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
      <Description>Creates a String.Format() call</Description>
      <Author>Kristopher Johnson</Author>
          <ToolTip>Replace with format string</ToolTip>
          <ToolTip>Replace with arguments</ToolTip>
      <Code Language="CSharp">
        <![CDATA[String.Format("$format$", $arguments$)$end$]]>

Just save this as a file called StringFormat.snippet in your Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets folder. (You can specify another location using the Code Snippets Manager in the Tools menu.) Then, when editing a C# source file, if you type sf and hit Tab a couple of times, this will magically appear in your code:

String.Format(" format ", arguments )

The format placeholder will be selected, so you can replace it with your format string, then hit Tab and the arguments placeholder will be selected, so you can replace it. You can keep hitting Tab to go back and forth between the placeholders. When finished, hit Return and the cursor will go to the end of the line.

Alas, snippets only work for C#, Visual Basic, and XML editing. Most of my work is in C++, so I have to keep writing code the old-fashioned way there (or use macros).

Note that I have been talking about Visual Studio 2008. "Why aren't you using Visual Studio 2010?" some will ask. Shut up.

For a more comprehensive tutorial on creating code snippets, see Switch On The Code: C# Tutorial - Visual Studio Code Snippets.

AppleScript for Bulk Conversion of PowerPoint Documents to Keynote

The instructor in one of my MBA-prerequisite classes distributed a set of PowerPoint presentations as course notes. I want to review these on my iPad, so I needed to convert them to Keynote.

This is pretty easy to do manually on a Mac: Just right-click the PPT file, select Open With... -> Keynote from the menu (which reads the PowerPoint file into Keynote), then File -> Save As... to store it where you want it. However, I am a programmer, so I would rather spend a few hours figuring out how to write a program to do a repetitive task than simply spend the five minutes needed to do it by hand.

At first I tried using Apple's Automator utility, which is supposed to make stuff like this easy, but I couldn't figure it out. So, I took the plunge into AppleScript.

As with every foray I make into AppleScriptLand, I was frustrated, annoyed, saddened, and exhausted by the experience. But I did succeed (if two hours spent futzing with AppleScript can possibly be called a success).

So, if you have need for a utility for converting a bunch of PowerPoint files to Keynote, open up the AppleScript Editor and copy and paste this script into the window:

on open droppedFiles
    set theDestinationFolder to (choose folder with prompt "Choose destination folder") as Unicode text
    repeat with theFile in droppedFiles
        tell application "Keynote"
            open theFile
            set theSlideshow to slideshow 1
            set theDestinationPath to theDestinationFolder & (name of theSlideshow)
            save theSlideshow in theDestinationPath
            close theSlideshow
        end tell
    end repeat
end open

Click the Compile button, and assuming you see no errors, choose File -> Save, set the File Format to Application, and save it to the desired place with the desired name.

Then, to convert files, just select them in the Finder and drag them onto the application icon. You will be prompted for a destination folder, then the script will do its thing.

The script looks pretty simple and straightforward, right? Well, AppleScript is a language that is easy to read, but very, very difficult to write. Every programming language that tries to "look like plain English" is a nightmare to use, because like English, the rules are illogical, arbitrary, and self-contradictory. Every application, like Keynote, has its own commands and object types, and the documentation is poor, so you end up doing a lot of experimentation and hair-pulling. The hardest part of this particular script was figuring out that I needed to add the as Unicode text conversion in order to produce a valid file path.

(This article is based upon my question and answer in the Ask Different Q&A forum.)