HyperCard Basics: Import or Domestic?

By Greg Raven
Volume 6 no. 6 (June 1988), page 16

One of by best friends got into computers about the same time I did. Eventually we both ended up with FileMaker, and for one reason or another I got good enough at programming FileMaker that whenever my friend needed something done he would give me a shout rather than attempt to wade through the coding himself.

Now we are both old-time Mac users, although he resisted HyperCard for the longest time. But, he finally did call the other day to ask a question about scripting, so I figured we were back to the old arrangement.

After I answered his question, I asked if there was anything else with which he needed help. Nope, he replied. I seem to be able to do just about everything else I need in HyperCard. Bye!

And so it goes. Another non-programmer bites the dust, replaced by someone who has discovered one more empowering aspect of the Macintosh.

In answering his question, I came up with a button that has since turned out to be pretty handy. HyperCard is fun for programming, and it is above-average as a flat-file database as long as you are either entering information as you go or are using one of the file importing stacks that are available to create a brand new stack.

But what happens when you have an existing stack, and existing external text file, and you want to put the two together? If you look up the solution in Danny Goodman’s HyperCard book you will never get your external text into your stack, because the script he shows does not work very well.

The answer is to create a button on your existing stack that will bring tab-separated text into your stack and put the information into the proper fields.

For the sake of example, let us say that you have a stack with six fields. Let us say you also have a text file that looks like:


Finally, let us say that the order of the information in the text file is the same as the order of the fields into which you wish the text to go in the stack.

Call up the stack, go to the background, and create a new button. Call up the script window for that button and type in the following script:

	on mouseUp
		ask Import text from what file?
		if it is empty then exit mouseUp
		put it into fileName
		open file fileName
		repeat until it is empty
		doMenu New Card
		repeat with i=1 to 5
		read from file fileName until tab
		delete last char of it -- kill tab
		put it into field i
		end repeat
		read from file fileName until return
		delete last char of it
		put it into field 6
		end repeat
		close file fileName
	end mouseUp

Here is what this script does:

First it asks the name of the text file from which you will be importing information. I use Microsoft Word 3.01, and I prepare the text with all the tabs there, eventually saving the file as a text file using the Save As… option in the File menu.

After you type in the name, HyperCard stores the name of the text file in the variable fileName (if you hit <RETURN> without specifying a file, you will exit the script).

The script then sets up a loop that will repeat until HyperCard tries to read text from the file and comes up empty. Because we want to put the information somewhere once it is found, we ask HyperCard to create a new card for the soon-to-be-imported information.

In this case, we have six fields in the text file, so we want HyperCard to go through the loop five times looking for text that ends with a tab character. It reads from the file until in finds a tab character and then stops. The next line kills the tab character so that will not be imported into your stack.

In this example, the loop continues on for five repetitions before it drops through to the next instruction. If you had more fields, you would change the script for more loops. If you had fewer fields, you would decrease the number of loops. Either way, the number of loops is determined by subtracting one from the total number of fields you wish to import.

HyperCard is now ready to import the last field, so we tell it to read from the text file until in encounters a return character. Then we delete the return character and put the text into the last field. Again, if the number of fields is different from the example, you must change the script to reflect your specific needs.

HyperCard will automatically stop when it runs out of text to import, and then it will close the file and exit the script.

After you have created this script, the best way to test it is on a copy of your stack. That way, if something goes wrong you will not have corrupted your original stack information. Because we have not added a lot of sophisticated file handling here, when you get all done you will have a blank card or two that you can remove using the Delete Card command in the Edit menu.