Tech Archives, page 41

All things technology. Very popular here on Justinsomnia.

hacking BlogThis!

update: BetterBlogThis version 2 is now available.

blogger has this great feature called BlogThis!. i use it constantly to update the neatlinks sidebar on my blog.

it’s a hunk of javascript that lives inside a link. in order to “install” it, you drag this link (BlogThis!) to your links bar, and when you click the link, it launches a popup with a mini-blogger post editor, prefilled with an html link pointing to the page you were at.

screenshot of blogthis

but BlogThis! does three things i don’t like:

instead, i’d like for the link text to be the selected text (unless no text is selected–then the title is ok) and i want to specify in advance which blog to post to. for grins and giggles, i want to change the size of the popup window, so that it’s wider and shorter (a size more befitting of a link).

turns out the original javascript was pretty hackable (it just had to be reformatted to be understood):

Q='';
x=document;
y=window;
if(x.selection) 
{
	Q=x.selection.createRange().text;
}
else if(y.getSelection)
{
	Q=y.getSelection();
}
else if(x.getSelection)
{
	Q=x.getSelection();
}
void(window.open('http://new.blogger.com/blog_this.pyra?t='+escape(Q)+'&u='+escape(location.href)+'&n='+escape(document.title),'bloggerForm','scrollbars=no,width=475,height=300,top=175,left=75,status=yes,resizable=yes'));

all the junk above the last line is trying to get at what text you’ve selected when the BlogThis! link was clicked and setting that to the variable “Q”.

the magic happens in that last line, where the query string (the part after the “?”) determines how blogger presents the BlogThis! popup. the query string can be modified to set the link text “n” to the selected text “Q” and if no text is selected, then set the link text to the “document.title”.

changing the size is easy. just modify the width and height values. i like width=800 and height=200.

making BlogThis! default to a specified blog required digging through the BlogThis! source and a little luck. every blog has a blogID which you see in the URL when you select that blog from the dashboard (http://www.blogger.com/app/blog.pyra?blogID=4126639). so i added “blogID” to the query string with the blogID number i wanted and it worked!

this is cool if you only use BlogThis! for one blog among many (like me), but i can imagine creating several custom BlogThis! links for all of your frequently updated blogs.

Here’s Better BlogThis!

After you drag it to your link bar, edit the properties and change the “xxxxxx” to your blogID.

screenshot of modified blogthis

implementing transactions in microsoft access (for the first time)

here’s the simplified scenario: i’ve got a publication table and a publication_status table. when creating a new publication, a record needs to be inserted into both tables. when updating the status of an existing publication, a record only needs to be inserted into the publication_status table. thus, there is a one to many relationship between publication and publication_status.

what if during the process of creating a new publication, inserting a record into the publication_status unexpectedly fails. i want the database to automatically rollback the inserted publication record.

here’s the pseudo-code outline:

  1. begin transaction
  2. insert publication record
  3. get new publication_id
  4. insert publication_status record
    1. if fails, rollback transaction
    2. if succeeds, commit transaction

here’s the problem: when you query the database for the max publication_id (which is bad in practice i know) you get the max id prior to the beginning of the transaction. agh!

so how do you get the id for the recently inserted record in microsoft access? apparently you query the table for @@IDENTITY. uhh…ok.

here’s what the actual vba code looks like:

' begin transaction
ws.BeginTrans

' insert publication record
db.Execute ("INSERT INTO publication (publication_number) VALUES (" & Me.txt_publication_number & ")")

' get new publication_id
Set rst = db.OpenRecordset("SELECT @@IDENTITY AS insert_id FROM publication")
int_publication_id = rst.Fields("insert_id")

' create publication_status
db.Execute ("INSERT INTO publication_status (publication_id, publication_status_type_id) VALUES (" & int_publication_id & "," & int_publication_status_type_id & ")")

' rollback transaction if there was a problem, else commit
If db.RecordsAffected <> 1 Then
   MsgBox "Error adding publication status"
   ws.Rollback
Else
   ws.CommitTrans
End If

i don’t know enough about the D.O.M.

while exploring dublin core for a final project, i came upon the elegantly simple nc echo dublin core metadata template.

in dublin core, every metadata element (like “creator” or “description”) can be used zero, one, or many times to describe a resource. eli naeher designed the template above using javascript and DOM so that any end user could modify the template itself in the browser. which makes dom especially important for accessing and modifying the elements in web user-interfaces.

in web database applications (such as my masters project), there are many places where an information object (such as an expense) needs to be described in terms of one or more instances of another class of information objects (such as activities). the problem from a UI design perspective is that there may be no maximum number of objects allowed to describe an object.

so using some modifications on the code from nc echo, I created the following functional UI mock-up, essentially a structured spreadsheet that allows dynamically growing or shrinking sets of select and text fields for reviewing and editing the many-to-many relationships between objects.

screenshot

update: i’ve added an operational version below. here’s the code.

expense1
expense2

Query Rosetta Stone

Natural Language

One person can be in one or many photos and one photo can contain zero, one, or many people. List all the people who are in photos along with the filename and the description of the photo(s) they are in. People and photos may be listed multiple times, but a person will never be in the same photo twice (assuming the photos haven’t been doctored).

Microsoft Access Query by Example (QBE)

Microsoft Access QBE

SQL generated by Microsoft Access

SELECT [tblPeople].[LastName], [tblPeople].[FirstName], [tblPhotos].[Filename], [tblPhotos].[Description] FROM (tblPeople INNER JOIN tblPeopleInPhoto ON [tblPeople].[PeopleID] = [tblPeopleInPhoto].[PeopleID]) INNER JOIN tblPhotos ON [tblPeopleInPhoto].[PhotoID] = [tblPhotos].[PhotoID] WHERE ((([tblPeople].[LastName]) Is Not Null) And (([tblPeople].[FirstName]) Is Not Null));

SQL cleaned up for readability

SELECT tblPeople.LastName,
tblPeople.FirstName,
tblPhotos.Filename,
tblPhotos.Description
FROM (tblPeople INNER JOIN tblPeopleInPhoto ON tblPeople.PeopleID = tblPeopleInPhoto.PeopleID)
INNER JOIN tblPhotos ON tblPeopleInPhoto.PhotoID = tblPhotos.PhotoID
WHERE (tblPeople.LastName Is Not Null) AND (tblPeople.FirstName Is Not Null);

Relational Algebra

join tblPeople and tblPeopleInPhoto

R1 ← tblPeople equijoinPeopleID=PeopleID tblPeopleInPhoto

join the result of the first join, R1, with tblPhotos

R2 ← R1 equijoinPhotoID=PhotoID tblPhotos

select only those R2 records where LastName is not null

R3 ← σ LastName != Null (R2)

select only those R2 records where FirstName is not null

R4 ← σ FirstName != Null (R2)

intersect (AND) R3 and R4

R5 ← R3 ∩ R4

project 4 fields (columns) in the final Result

Result ← π LastName, FirstName, Filename, Description (R5)

Note: If the characters above don’t look like arrows, sigmas or pis, try the PDF version.

drinking at the mozilla water fountain

some discussion of this came up after a question about trusted computing/palladium following cory’s talk about the eff. he described having to lie to his dumb bank to convince them that his browser of choice (ostensibly mozilla, safari, or opera) was in fact internet explorer so it would let him through to do his business.

well my browser of choice is mozilla (currently using 1.6, the latest stable version), and when i tried to make a payment using checkfree for my wachovia-branded MBNA credit card, i got this insanely stupid message:

Browser Upgrade Required We have detected that your browser is an older version of Netscape. You must upgrade to Netscape Navigator 6.2 or later.

so i had to lie to my bank by downloading the very cool User Agent Switcher to pretend to be internet explorer to get through their DUMB browser detection script to pay my credit card bill. sheesh!

in other frustrated computer user news, my beloved laptop has developed a problem: when i plug it in, it appears to short itself out and shut off. so i’m sending it in to have the system board replaced (again)—at possibly the WORST time ever to be without my laptop. suffice it to say i’m backing up the hard drive and burning a cd of all master’s paper relevant documents. *sniff sniff* word up though: ibm thinkpad support rulez. box arrived at 10am today after calling last night after 6pm and they promise a max 5 day turn-around.