Check HTML syntax in BBEdit (first version)

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.

Note:

While this script works fine, there is a revised version that does away with the GUI scripting, the requirement for the BBEdit command-line tools, and the open unsaved document that takes two keystrokes to get rid of.

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). Requires the BBEdit command-line tools (you can install these from the BBEdit’s Application menu).

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

do shell script "curl " & theURL & " | bbedit"

tell application "BBEdit"
	activate
	set source language of front text document to "HTML"
end tell

tell application "System Events"
	tell process "bbedit"
		delay 1
		keystroke "y" using {command down, control down}
	end tell
end tell

For Safari, substitute this for the OmniWeb tell block:

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

Surprisingly inelegant for such a short script, but it works for me. Undoubtedly one can avoid the GUI scripting step, given BBEdit’s extensive Applescript capabilities, and I’ll be glad to hear how.

This does leave you with a new, unsaved BBEdit document to deal with, which itself is a bit of a pain. But at least the actual syntax check is easy. No more skipping the syntax check for me.

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-01-30 (last modified 2017-02-18)