Monday, February 23, 2015

Creating a Reference Letter Writing App with LiveCode

Somewhat on a dare, I've created a prototype of a reference letter writing app with LiveCode. I say "on a dare" because I was eavesdropping on a conversation of a few of my faculty colleagues at the University of Georgia before a faculty meeting last week. They were talking about the number of reference letters they were having to write. One colleague remarked that she had promised letters to about 30 very deserving undergraduate students who needed the letters as part of their application to graduate school. As happy as she was to write these letters for them, the task was really taking its tolI in terms of the time she needed to do it. I casually remarked that I thought it wouldn't be too hard to create a simple app with LiveCode that would construct at least a respectable first draft. Everyone thought this would be a fantastic idea for an app though I thought I sensed a hint of doubt in their enthusiastic voices that I could pull something like this off.

So, naturally I dropped everything else I was doing and created a prototype of just such an app. (As I like to say, I'm not a competitive guy, but "I'm more competitive than you.") Seriously, though, it did sound like an interesting and worthwhile project and one that I sincerely thought wouldn't be that hard to build. I even mentioned to my colleagues that it should be very easy to create a simple student rating system, say by using a scale such as from 1 to 3 (outstanding, very good, fair) which could be used to automatically generate a suitable draft letter.

I've spent about 3 hours on this project and it was about as challenging as I suspected. However, I don't think the prototype I built is the way to approach this problem. But, I think that is an important principle of design -- one cannot fully anticipate the design specifications, or even the best approach to identifying those specifications, for a project until one begins building it. I'll return later to discuss what I think would be a better way, but let's take a look at the current prototype:


Allow me to emphasize that I and my colleagues at the University of Georgia take the task of writing reference letters very seriously. All letters require much care and attention. So, don't get the impression that I'm making this app with the intent to trivialize or cheapen the reference letter writing process. My goal was only only to explore how to make an app that would help someone write at least a simple first draft. The letter should then be customized as needed for the particular student.

As mentioned in the video, it would be very easy to have LiveCode choose a phrase from the menu choice fields at random, though this would require that the user code each phrase as being acceptable within the excellent, very good, or fair rating categories. This would allow the app to create a full draft of the letter with just two clicks (one to select the rating and the other to generate the letter). That feature obviously has some appeal.

A Better Design Approach


To be honest, I feel there are all sorts of problems with the design approach I took with this prototype, with the main one being that the user is "locked in" to the basic format of the letter. The only way to change the format is by me going in and changing it within LiveCode. For the app to be useful, the user would need to have complete freedom to change the format and contents of the letter at will. Another obvious feature that is needed is the ability to save as many letter templates as one wishes.

In hindsight, I think a better approach would be to use something called "piped text" within a simple text letter template. The idea is that as one writes the letter, one could decide on those locations in the text where dynamic text should be used. The user could then insert the dynamic text by denoting it with special characters, such as start and end braces. The dynamic elements could likewise be edited at will also. If I continue developing this app in any serious way, I'll most likely redesign it completely with this strategy in mind.

Saturday, February 21, 2015

Creating a Function in LiveCode to Capitalize a Given Word

I'm working on a little project that automatically constructs a narrative - a letter, to be precise - based on some user choices. As I started to build it I immediately found myself needing a way to occasionally capitalize a given word. For example, consider the pronouns he and she, or his and her. My program correctly identifies the gender of the subject of the letter, but sometimes a sentence begins with one of these pronouns and other times the pronoun just occurs somewhere in the sentence. I didn't want to create a variable to hold an all-lowercase version of the pronoun and another to contain a capitalized version. There will certainly lots of other times when a word will need to be capitalized.

I was hoping that LiveCode already had this function, but alas I can't find it. But, it does have the function "upper," which will convert a given text to all upper case. So, I used this to quickly build my own function to capitalize a given word:

function capitalizeWord w
   put char 1 of w into x
   put upper(x) into xupper
   put the number of chars of w into L
   put xupper&char 2 to L of w into y
   return y

end capitalizeWord

The function takes a single word as input and puts it into the local variable w. So, let's imagine that the word entered was "nowhere."

The function begins by dissecting the given word into two parts: the first letter and the rest of the word. The first line takes the first character of w and puts it into another local variable x. So, in the case of "nowhere," x is equal to "n." The next line converts x to uppercase and puts the result in yet another local variable xupper. Great, we now have the first letter of the word capitalized. We now need to join this with the rest of the word

The next line takes note of the total number of characters in the word and puts this number in the variable L. Nowhere has seven characters, so L equals 7. The next line joins xupper ("N") to characters 2 to 7 of nowhere -- N+owhere -- resulting in "Nowhere" and puts this into y, which is then returned to whatever line of code called the function.

To demonstrate how to use this, I created a small stack with two fields and one button:

[ Get the free LiveCode Community version. ]


The left field is labeled "word1" and the field on the right is labeled "word2". The idea is that you type a word into the left field, press the button, and the word is capitalized and shown in the right field. Here's the script of the button "Capitalize":

on mouseUp
   put empty into field "word2"
   
   put line 1 of field "word1" into w1
   put capitalizeWord (w1) into w2
   put w2 into line 1 of field "word2"
   
   put empty into field "word1"
   focus on field "word1"

end mouseUp

The first line simply empties out field "word2." The second line takes whatever is in line 1 of the feld on the left and puts it into the local variable w1 (think of this as word 1).

The next line calls the capitalize function, puts the result into w2, which the next line puts into line 1 of the field on the right.

I added two more lines of code to tidy things up a little. I empty the field on the left and then I make sure that the focus (the flashing vertical text bar) is put into this field.

This function will be pretty darn handy in this and other LiveCode projects!