DTerm is a free app that uses the Accessibility API to figure out the current document you’re looking at. When you hit a hotkey (mine is bound to command-shift-return), it pops up a window with a shell command line with the working directory already set to the frontmost document’s folder.
DTerm works awesomely with Finder, BBEdit, TextMate, Xcode, GitX and a bunch of other Mac apps.
Protip: instead of typing return to execute your DTerm command, type command-return to open a new Terminal.app window with your command. Great if you plan a few follow-up commands or want colored output.