Alan Storm is a human being living in Portland, OR by way of Seattle, WA by way of Portland, OR by way of Rochester, NY. He likes making websites, and talks about that here.

He also likes to make things on the web. If you need something made on the web, drop him a line.

We're a little worried about his penchant for slipping into the third person narrative form.

Follow the Feed

Follow the Twitter

Applescript For Local Validation of HTML Files

For a long time now I’ve been trying to find a way to validate my html files locally. I do most developing on my iBook, and only upload the site once I’m finished1.

Now, the W3C offers the source code for it’s validator online. Since OS X is, for better or worse, a UNIX operating system, I thought I had a good shot at getting the validator working locally and solving all my problems.

Ha.

Perl is a wonderfully flexible language. Unfortunately, to do useful things with perl (like write a programming interface for a SGML validator) you need to use CPAN modules. There’s nothing inherently wrong with CPAN modules. In fact, they’re one of the reasons perl is so flexible. They’re also one of the reasons that every computer on a planet has a different “installation” of perl, and is why an (arguably) less powerful language like PHP is so much more popular for web development.

As you’ve probably guessed, while trying to install the perl modules I ran into problems that my unix-fu and google-fu weren’t able to handle. Specifically while trying to install the Text-Iconv module, i kept getting the following error

DIED. FAILED tests 1-13
        Failed 13/13 tests, 0.00% okay
Failed Test     Stat Wstat Total Fail  Failed  List of Failed
-------------------------------------------------------------------------------
t/00_load.t        0     5     1    2 200.00%  1
t/01_charsets.t    0     5    13   26 200.00%  1-13
Failed 2/2 test scripts, 0.00% okay. 14/14 subtests failed, 0.00% okay.
make: *** [test_dynamic] Error 2
  /usr/bin/make test -- NOT OK
Running make install
  make test had returned bad status, won't install without force

Of course! I’m savvy enough to know that the libiconv application isn’t behaving like perl thinks it should (failed tests), but I don’t know enough about libiconv or the CPAN module testing process to know why or easily troubleshoot it.

So i may come back to the validator project at some point, but it’s spring outside and I need to validate my local files now, which led me to cURL.

cURL is a command line utility for transferring files over a variety of protocols. Think of it as a web browser with no interface or rendering engine. In addition to fetching files, cURL can also simulate submitting a web form, including the W3C validator.

Without getting too down and dirty, here’s the command you would enter from a terminal prompt to submit a file to the W3C validation server

  curl -F uploadedfile=@/tmp/validate.html\;type=text/html http://validator.w3.org/check

The ‘-F’ flag tells curl we’ll be submitting a form with the enctype of “multipart/form-data”, (aka: file upload). Following the ‘-F’ is a name/value pair that points to the file we want to upload.

The \;type=text/html nonsense if a bit of wrangling we need to cover a curl bug where files with a .html extension are uploaded with their Content-Type set to text/plain (which makes the validator mad). Finally, at the end of the command is the URL of the validation CGI program.

Lacking any other options, curl will simply output its results. If we were real command line monkeys, this would be enough.

Fortunately for you, we’re a primate of a different breed, and would prefer to see the results in one of those pesky web browsers we’ve been hearing so much about. Specifically Safari, because no other web browser runs faster on the iBook’s oh so chunky G3 processor. Enter AppleScript.

What we’d like to do

  1. Save the source of the front most Safari window to a file
  2. Use curl to post this file to the W3C validation service
  3. Save the results of that post to a file
  4. Open that file in Safari

Here’s our script, with self explanatory comments.

--save in /tmp directory
set thepath to (((path to startup disk) as text) & ":tmp:validate.html")

--get source of document tell application "Safari" set the_text to the source of document 1 as text end tell

--save source to file writetofile(thetext, thepath, false)

--use curl to post file to W3c site, and get results set theresults to do shell script "curl -F uploadedfile=@/tmp/validate.html\;type=text/html http://validator.w3.org/check" as text

writetofile(theresults, thepath, false)

tell application "Safari" open location "file:///tmp/validate.html" end tell

--taken from --http://www.apple.com/applescript/guidebook/sbrt/ on writetofile(thisdata, targetfile, appenddata) try set the targetfile to the targetfile as text set the opentargetfile to ¨ open for access file targetfile with write permission if appenddata is false then ¨ set eof of the opentargetfile to 0 write thisdata as string to the opentargetfile starting at eof close access the opentargetfile return true on error try close access file targetfile end try return false end try end writeto_file

Note: The support function named write_to_file was pilfered from the Apple Website. Specifically the AppleScript Guidebook, where you’ll find lots of other useful functions to help you with your struggles through the last of the natural language scripting platforms2 (assuming the rumors of Macromedia killing Lingo are true).

Footnotes

  1. Recognizing, of course, that nothing is ever finished
  2. Also known as the Bastard Children of HyperCard

Update: This may help solve my Text-Iconv problems.

Originally published May 3rd, 2004