The curl command example I submitted the other day is OK, but it’s got some drawbacks: If the URL is for a site that requires login, it’ll fail because curl just naïvely gets the URL without attempting to log in. Or maybe the page you want has some cookies on you that makes what you’re seeing in your browser different from what curl sees (unless you pass the cookie data to curl and blah blah blah).
So here’s something you can do if you want a command in TextMate that gets the source of the frontmost tab/window in Safari (yes, it only works with Safari because it relies on Safari’s AppleScript-ability):
osascript -e 'tell application "Safari" to (source of current tab of window 1)' | awk '{ sub(/\r$/,""); print }'
Breakin’ it down: osascript is a command that interprets AppleScript. The -e option tells it to execute the following line. That line, in turn, tells Safari to return the page source of the frontmost tab. The result is then piped (|) to the awk command, which replaces CRLF (Windows-style) line-endings with Unix line-endings, and prints the result to stdout.
You can either run that command in the terminal, or in a TextMate document using ^R, and get the source right there, or you can make a new command in whatever bundle you choose (maybe a new one for your own custom commands?), and paste the line in there. Since it’s bash, the new command can be just that line and nothing else. Set the command’s input to Nothing and its output to Create New Document. And… disco.
It may not be a command that you’ll use very much, but it’s a neat example because it utilizes a bunch of TextMate and Mac OS X features in one go.
But: It ain’t perfect. Besides being Safari-only, the source you get won’t contain markup that was added/changed via JavaScript, so very Ajax’y/Web 2.0-ish pages will have trouble. Second, if the AppleScript line fails (which it can) you’ll get a new document just containing an ugly error message. So there are a bunch of improvements that can be made to the user-experience (e.g. show a tool tip if there’s an error instead of creating a useless document). But that’s some homework for you.