Check HTML syntax in BBEdit (revised)

Summary:

For Mac/BBEdit users, an Applescript to check the HTML syntax of the current Safari or OmniWeb page — an easy way to validate websites still in the local development phase.

One of the many great things about editing HTML in BBEdit is the built-in syntax checking. When something is easy to do one is likely to do more of it. Having a syntax checker a keystroke away is likely to lead to fewer HTML errors.

Checking HTML syntax on a Textpattern page is a much clunkier process. When something is a pain to do one is likely to do less of it. I am absolutely guilty of this when it comes to validating Textpattern websites. Occasionally I discover an error and then wonder how many weeks or months it has been there.

Are you familiar with the following sequence?

  1. Make edit in Textpattern
  2. Switch browser tab to live site
  3. Refresh page
  4. View source
  5. Select all
  6. Copy
  7. Close source-view window
  8. Switch to BBEdit
  9. New document
  10. paste
  11. set document language to HTML (if that isn’t your default)
  12. Command-control-Y (check syntax)

Here’s an Applescript to handle steps 4 through 12. It will have BBEdit check the syntax of the page currently active in OmniWeb (see following note for Safari).

tell application "OmniWeb" to set theURL to address of active tab of browser 1

set tempFolder to (path to temporary items folder)
set tempFile to (tempFolder as string) & "temp.html"
set thisPosix to POSIX path of tempFile

do shell script "curl " & theURL & " -o \"" & thisPosix & "\""

tell application "BBEdit"
	activate
	open tempFile
	set source language of front text document to "HTML"
	check syntax of tempFile show results {true}
end tell

For Safari, substitute this for the first line:

tell application "Safari" to set theURL to URL of document 1

The “temp.html” file that opens in BBEdit is in your Temporary Items folder; once you close this window you can forget about it.

If you don’t use BBEdit, you might find this Applescript for W3C validation handy. But it only works on “live” pages.

There’s nothing particular to Textpattern about these Applescripts. I presume they would also be useful for other CMSs.

Posted 2008-02-02 (modified 2008-02-02)