HyperTalking...Getting Text Into and Out of
GIF of HyperCard IIGS iconHyperCard IIGSGIF of HyperCard IIGS icon

HyperCard IIGS has a number of text handling features that allow you to manipulate text files on your hard disk, as well as text within fields, and in this installment of HyperTalking, I'd like to show you how to take advantage of them to get text files out of and into your stacks. We'll begin with a new stack, which we'll call "WordSmith". After you create a new stack, create a new field using the "New Field..." command in the "Objects" menu. When you choose this command, HyperCard IIGS chooses the field tool, creates a new field in the center of the screen, and selects it. Take the field and make it the full height of the card, and half the width, by doing the following steps:
  1. Click in the center of the field and drag the field so that its top left corner is in the top left of the card;
  2. Click on the lower left corner of the field at the very edge and drag down and to the right until the field is the shape that you want;
  3. Or cheat...press command-M to bring up the message box and type into it: Set rect of card field 1 to "10,20,190,180", then press return.
  4. Next set the style of the field to scrolling. Do this either by double clicking on the field while the field tool is chosen to bring up the Field Info dialog box, and choosing the style from the pop-up menu there, or type "set style of card field 1 t o scrolling" into the message box and press Return.
Once you have the field in the position you like (don't worry if it isn't perfect, it won't affect the basic goal of our work here), use the "New Button" command in the "Objects" menu to create a new button. Like the "New Field" command, this will create a new button, select it and choose the button tool all in one move. Drag the button to the left of the card, then double-click on it. This brings up the "Button Info" dialog box (you could also choose "Button Info..." from the Object menu to get this). At the top is an entry field where we can type in the name of the button. Let's call it "Open Text File" and choose an icon for it by clicking on Icon. Pick an icon that makes sense...the generic text file icon might be good for this. After deciding, do uble-click on the icon, and you'll return to the Button Info box. Now click on the "Script" button, and we'll type in the script that will make this button do its magic. After clicking on the script button you have a field in which to type your script, w hich, if you've tried some of the earlier projects in this series, should be familiar. The script we'll use is:

on mouseup   
  answer file "Which file would you like?" of type text	
                               --gives us an 'Open File' dialog box   
  put it into filename         --puts our selection into a variable   
  open file filename           --opens the file we've selected   
  read from file filename for 16000	
                               --reads the first 16000 characters   
  put it into card field 1     --puts the characters into our field   
  close file filename          --closes the file 
end mouseup 

After you've typed the script above (you do not need to type in the comments following the double hyphens), click on close and click on "Yes" to the query of whether you want to save the changes. Now close the Button Info box, and try it out. Click on th e button, and you should get the standard 'Open File' dialog box, which will let you open a text file for reading into your card's field. We only read the first 16000 because that is a limit of HyperCard IIGS, which it inherited from its Macintosh predec essor. Now that you have a working import button, we need to create a button that will help us save text. Begin by creating another new button, and place it under the "Open Text File" button. Double-click the button to bring up the Info box, and name thi s button "Save Text File". Choose an icon as before, then click on the script button and type in this script:

on mouseup                            --this handler exports text
  ask file "Name your text file"      --brings up 'Save As' dialog  
  if it is not "" then                --if name isn't blank, puts
                                      --name into variable "exName"
    open file exName                  --opens a file with the name 
                                      --stored in variable
    write card field 1 to file exName --puts contents into new file   
    close file exName                 --closes file (thus saving it)
    exit exportText                   --if name's blank, exit
  end if     
  choose browse tool                  --picks browse tool
end mouseup 

This script will give you a typical 'Save As' dialog box which you can use to name a file. HyperCard then opens the file (when HyperCard opens a file that doesn't exist, it creates it first, then opens it for reading/writing) and writes the contents of y our text field to the file. The file is then closed, and thus you have used HyperCard to create and save a text file. Whoa! you may be thinking, does this mean I didn't need to go out and buy AppleWorks GS after all? I could have had it all in HyperCard? Well, not exactly. You can make a perfectly good text editor with HyperCard, able to open and save files, do right, left or center justification (only one though), with a font and size of your choosing. But you have to deal with the restrictions of only one font/size and justi-fication per field, and small file size. However, you may find that certain stacks lend themselves to these types of use, and being able to have these buttons is a great time saver. For example, a class-room might want to create a digital yearbook. Each card could have a field with a biography of each student, which they might write on their own in Appleworks or FrEdWriter. Saving the files as text, the kids can give their disks to the yearbook editor, who can import the story to a card in the HyperCard stack, where it can be further custom-ized with a photograph, a sound clip of the student, artwork, etc.

One of the great advantages of HyperCard is that it is object-based. Having created these import and export buttons, we can select the button tool, select the button, and choose "Copy Button" from the "Edit" menu, then paste the button into an entirel y different stack. Not only will the visual button be copied, but so will the script associated with it. When HyperCard Mac's creator Bill Atkinson called HyperCard a software erector set, this is just what he had in mind. Individual modules that can be assembled piece by piece into unique software solutions for your needs. Almost everything you see on a HyperCard IIGS card can be copied: buttons, fields, background graphics, so as you create useful buttons and scripts you can transfer them to other sta cks without having to create all of your work from scratch. So use these two buttons in your stacks and give your stacks the ability to move text around. Posted to Genie and my Web site are copies of "HC.Word" my own implementation of these concepts. HC.Word has text in and out buttons, as well as justification buttons and a choose font button, for a pretty functional text editing stack. The stack is not locked, so you can freely browse and use the HyperTalk s cripts in your own stacks. If you don't have access to the Web or Genie, you can order a disk from me that has HC.Word, along with other HyperCard IIGS stacks on it for $5.00, which includes the shipping. The full contents of the disk are:

If you are interested in the disk, send a check or money order for $5.00 made out to me at:

Steve Cavanaugh
The Apple Blossom
8 Ardsley St.
Brockton, MA 02402.

*Note: This is the third HyperTalking issue that I've run in The Apple Blossom this year. The first article dealt with using HyperTalk to control the built-in paint tools and other elements of HyperCard, and the second dealt with creating custom menus fo r HyperCard IIGS stacks, including the Home Stack.
Return to Blossom 8 Index
Return to Apple Blossom Home Page