Mac Format

Make a word processor

Create your own processor with ‘live wordcount’

-

Live wordcounts (standard in word processors today) began as a feature in Wordless, written in the mid-nineties using software much like LiveCode. We’ll recreate this here – adding in tricks for saving files – so you end up with your own word processor. It’s a useful exercise in resizing objects and handling external files, too!

Create a new Mainstack and add a scrolling field and a Text Entry field. Don’t worry about positionin­g these yourself – we’ll make them resize themselves automatica­lly whenever the window is resized.

Use the Inspector palette to name the Scrolling Field ‘work’ and the Text Entry field ‘count’. With the small ‘count’ field, uncheck the Focusable, Focus Border and Show Border options, and check Lock Text. You can’t type into this field now, but your scripts can work with it.

Choose Object > Stack Script and make an on resizeStac­k handler. This is triggered whenever the stack is resized.

on resizeStac­k put the height of this card into theH

put the width of this card into theW

set the rect of field "work" to 0,22, theW, theH-20

set the rect of field "count" to 0, theH-20, theW, theH end resizeStac­k

Above, the first two lines put the height and width of the card into a couple of variables. The next line sets the rect (rectangle) of the ‘work’ field to be zero pixels from the left of the card edge, 22 pixels down from the top. The right edge is set to the width of the card (‘theW’), and the bottom is set to the height of the card minus 20 pixels. This makes it the full width of the card and almost the full height.

The ‘count’ field is controlled in a similar way, but it’s shorter and

Building a word processor is a useful exercise in resizing objects and handling external files

positioned along the bottom of the card. Hit Apply and then switch to the Run tool and try resizing the window. Also, turn on the Live Resizing option for the stack in the Inspector palette.

Key release

Now for the live wordcount. In the same stack script area as above, make a new handler called in keyUp . This is triggered every time a key on your keyboard is released, as something is typed. You need just one line of script inside this handler:

put the number of words of field "work" && "words" into field "count"

Now, as you type, you’re told how many words are there.

The next step is to make a script to count the words in a selection, not just all the words in the field. The handler to create is on selectionC­hanged , something that is automatica­lly triggered when a selection is made. This script is slightly more involved, although it’s not at all complex.

First we get the number of words of the selection , which stores the answer in the ‘it’ variable. Then we use a trusty if-then-else structure:

if it is 0 then keyUp else put it && "words selected" into field "count" end if

So if there are zero words selected, we make our own keyUp event trigger (so the normal word count is performed). Otherwise we put the contents of ‘it’ – how many words are selected – into the field, followed by ‘words selected.’ Keith Martin

 ??  ?? Use the LiveCode Dictionary to find the full details of any command, function or property.
Use the LiveCode Dictionary to find the full details of any command, function or property.
 ??  ?? Want to resize objects when the window changes shape? It takes just a few simple lines of script instructio­n.
Want to resize objects when the window changes shape? It takes just a few simple lines of script instructio­n.
 ??  ??

Newspapers in English

Newspapers from Australia