Your browser is very old. You might enjoy surfing the web more if you used something newer like:

Google Chrome

Even Firefox would be OK.

If you're being forced at gunpoint to use Internet Explorer, you should at least upgrade it. Version 8 is tolerable and 9 will be OK when it comes out.

Posts from January 2009

Javascript awesomeness with Prototype

I’ve talked a bit about the Ajax features of Prototype in a previous post. Now I want to talk about some of the other fun things Prototype can do.

The first thing to do is add the appropriate Prototype includes to your HTML file. You can do this by downloading the latest version from http://www.prototypejs.org and hosting it yourself.

Or you can let Google do the hard work by adding this to your HTML header:

    

The first thing I’ll mention is how fantastic the Prototype API Documentation is. Once you’ve covered the basics, I highly recommend spending some time reading through it.

The second thing we’re going to do is set up a handler for the window onload event. You might normally do it like this <body onload=”someFunction();”> but I like the method below because it keeps all of our javascript separate from the HTML.

Event.observe(window, 'load', function() {
    alert('Hello World!');
});

The basic format of Event.observe is Event.observe(<element>, ‘<event>’, <event-handling-function>)

We could just as easily have done this

function HelloWorld() {
    alert('Hello World!');
}

Event.observe(window, 'load', HelloWorld);

but I generally use the first version. I think having the handling function inline makes it a little easier to read.

Now let’s talk about how easy it is to get objects out of the DOM. Here’s some HTML


Let’s set up an event handler for the click event on that submit button. We just need to add a few lines to our existing window load event handler.

Event.observe(window, 'load', function() {
    Event.observe( $('dostuff'), 'click', function() {
        alert('Hello World!');
    });
});

As you can see, we’ve just added an Event.observe call inside the window load event handler. The key thing to note is $(‘dostuff’). The $ is essentially a shortcut to document.getElementById but it’s a lot less to type and the element you get back has loads of handy methods added to it by Prototype.

Now that we’ve seen how easy it is to use $ to get back a single object, let’s talk about dealing with multiple objects.

Here’s some more HTML







Let’s update our window load javascript code

Event.observe(window, 'load', function() {
    Event.observe( $('dostuff'), 'click', function() {
        $$('input[type=checkbox]').each( function(elem) {
            alert(elem.id);
        });
    });
});

The example above shows a simple example of the $$ utility method which takes a CSS rule as an argument and returns a list of Prototype-enhanced elements.

In our example, we’re using the each method to apply a function to every item in the list returned by $$.

Here are a few more simple examples of $$:

  • $$(‘input’) returns a list all input elements.
  • $$(‘td.even’) returns a list of all td elements that have a class of even
  • $$(‘table#stuff td’) returns a list of all td elements that are inside the table element that has an id of stuff

So those are few simple examples of using Prototype to retrieve DOM elements. I’ll come back and cover some other Prototype fun in a later post.

Regular expressions in VBA

I recently had to write a large amount of Microsoft Access VBA code to parse a bunch of text reports and store those results into tables in Access. I’m sure many of you out there are shuddering and/or sneering about having to write Access VBA.

My feelings are n-fold:

  1. It was a paying gig and I got to do it from home.
  2. It was a nice break from writing PHP & Python.
  3. It was an interesting intellectual exercise and I got to learn something new.

Sounds like a pretty good deal, right?

The text I had to parse lent itself pretty nicely to a set of regular expressions but I wasn’t sure how that was done (or if it was even possible) in VBA. Of course I didn’t give VBA enough credit because 30 seconds of googling lead me to the RegExp object which can be found by adding a reference to the Microsoft VBScript Regular Expressions library.

It’s dead simple to use, especially if you have something like Regex Buddy to help with building your regular expressions.

Here’s an example that does some simple testing as well as grouped matching including retrieving subgroups.

    Dim szLine As String
    Dim regex As New RegExp
    Dim colregmatch As MatchCollection

    With regex
        .MultiLine = False
        .Global = True
        .IgnoreCase = False
    End With

    szLine = "Analyzed range (from-to)	10	100"

    regex.Pattern = "^Analyzed range"
    If regex.Test(szLine) Then
        regex.Pattern = ".*?([0-9]+).*?([0-9]+)"
        Set colregmatch = regex.Execute(szLine)

        'From
        Debug.Print colregmatch.Item(0).submatches.Item(0)
        'To
        Debug.Print colregmatch.Item(0).submatches.Item(1)
     End If

Manipulating PDF files with Visual Basic .NET

There have been a number of times where I’ve needed to export each page of a large PDF to its own file. There appear to be lots of commercial products that do this but they’re expensive and more powerful than I really need. After much unsuccessful searching for a free utility, I gave up and decided to write my own using the Adobe Acrobat 8.0 Type Library. I may be wrong but I believe you only get access to the type library with the full version of Acrobat. I haven’t checked to see if it comes with Adobe Reader.

The only thing even remotely tricky about this program is the need for an existing PDF file to insert pages into. I couldn’t figure out how to create an empty PDF from scratch so I just have a file called blank.pdf which has a single blank page. Each page gets inserted into the blank file, then saved under a new name.

Here’s the basic flow of the program:

  1. Open the file containg the pages we want to extract.
  2. Loop through all of the pages.
  3. For each page, open the blank PDF file.
  4. Insert the current page from the input file into the blank file.
  5. Delete the blank page from the blank file.
  6. Save the blank file under a new name.

The main object is the AcroPDDoc object.

        Dim origDoc As New Acrobat.AcroPDDoc
        Dim blankDoc As New Acrobat.AcroPDDoc
        Dim i As Integer, numPages As Integer
        Dim outFile As String, inFile as String

        inFile = "infile.pdf"

        If origDoc.Open(inFile) Then
            numPages = origDoc.GetNumPages()

            For i = 0 To numPages - 1
                outFile = "output.pdf"
                blankDoc.Open(Application.StartupPath() & "\blank.pdf")

                blankDoc.InsertPages(-1, origDoc, i, 1, False)
                blankDoc.DeletePages(blankDoc.GetNumPages() - 1, blankDoc.GetNumPages() - 1)

                blankDoc.Save(Acrobat.PDSaveFlags.PDSaveFull, outFile)

                blankDoc.Close()
            Next

            origDoc.Close()
        Else
            MsgBox("Could not open " & inFile, MsgBoxStyle.Critical)
        End If

The type library is pretty easy to understand. Here’s a very brief summary of the methods I ended up using. There’s a lot of functionality that I didn’t touch that lets you do all kinds of exciting things with PDF files.

  • GetNumPages() Returns the number of pages in the current file.
  • InsertPages( InsertAfterPage, SourceDocument, SourcePage, NumberOfPagesToInsert, Options) Takes a page (or page range) from a document and inserts the page(s) into the calling document.
  • DeletePages( StartPage, EndPage) Deletes a page (or page range) from a document.
  • Save( SaveOptions, OutputFile). Saves a file.