Check HTML syntax in BBEdit (revised)


For Mac/BBEdit users, an Applescript to check the HTML syntax of the current Safari or Chrome 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 CMS-driven 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. 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 to a CMS template
  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 OmniWeb1 Chrome (see following note for Safari).

tell application "Google Chrome" to set theURL to URL of active tab of first window

 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"
     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.

1. Ah, OmniWeb, a web browser I liked well enough to pay for, back in prelapsarian times.

Posted 2008-02-02 (last modified 2017-02-18)