Categories


Archives


Recent Posts


Categories


Applescript For Local Validation of HTML Files

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

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 uploaded_file=@/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 the_path 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

write_to_file(the_text, the_path, false)



--use curl to post file to W3c site, and get results

set the_results to do shell script "curl -F uploaded_file=@/tmp/validate.html\\;type=text/html http://validator.w3.org/check" as text



write_to_file(the_results, the_path, false)



tell application "Safari"

    open location "file:///tmp/validate.html"

end tell



--taken from

--http://www.apple.com/applescript/guidebook/sbrt/

on write_to_file(this_data, target_file, append_data)

    try

        set the target_file to the target_file as text

        set the open_target_file to ¨

            open for access file target_file with write permission

        if append_data is false then ¨

            set eof of the open_target_file to 0

        write this_data as string to the open_target_file starting at eof

        close access the open_target_file

        return true

    on error

        try

            close access file target_file

        end try

        return false

    end try

end write_to_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.

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 3rd May 2004

email hidden; JavaScript is required