This script reads data and puts it into a field in the card where the button is. It moves to the next field when it encounters a tab, and makes a new card after a carriage return. This script requires that the number of fields in your card equals the number of data items you want to import.
on mouseUp
ask "Import text from what file?" -- Ask user for name of source file.
if it is empty then exit mouseUp
put it into fileName -- "put" and "open" open the text file for reading.
open file fileName
repeat -- Data is imported in the "repeat" loops.
doMenu "New Card" -- Make a new card.
repeat with i = 1 to the number of fields - 1
read from file fileName until tab -- Read text from the file until Tab
-- character is encountered.
put empty into last char of it -- Remove Tab.
put it into field i
end repeat
read from file fileName until return
if it is empty then -- end of file
if i = 1 then doMenu "Delete Card"
close file fileName
exit mouseUp
end if
put empty into last char of it -- remove Return.
put it into field (i + 1)
end repeat
end mouseUp
When the first tab is encountered, the tab is removed from the string and the data is placed in field one. This is done until the next to last field, because we have to look for a carriage return at the end of the last field. We then read in data until a carriage return occurs, and the data is placed into the last field of the card.
This script has been used to import data files 500K and larger into HyperCard. It may have to be changed to work with a specific data file or format.
You can also EXPORT text from HyperCard fields into a text file. You can tab delimit the fields, or use any character you want:
on mouseUp
ask "Export text to what file?" -- Ask user for name of source file.
if it is empty then exit mouseUp
put it into fileName -- "put" and "open" open the text file for reading.
open file fileName
repeat with i = 1 to the number of cards -- go through all the cards
go to card i
repeat with j = 1 to the number of fields - 1
write field j to file fileName -- get the field and write it out
write tab to file fileName -- put a tab after it,
-- or any character you want
end repeat
write field (j + 1) to file fileName -- write the last field
write return to file fileName -- and a carriage return
end repeat
close file fileName
end mouseUp
This would export the data you had just imported with the first script.
NOTE: This second script has NOT been extensively tested.
ANOTHER NOTE: This is NOT the only way to import & export data: use it as a guide for your own scripts. These examples were chosen because tab-delimited data is a common format. HyperCard offers a large amount of flexibility in reading data in and out, and it would be impossible to describe all the different methods in this article. There are various sources of information regarding the HyperTalk scripting language.
For more information:
- HyperCard's Help stack (look for "ask", "put", "open", "write", "repeat", and "doMenu")
This part of HyperCard is often overlooked. The built-in help has descriptions of the HyperTalk commands available to you, and is on-line.
- "HyperCard Script Language Guide" (APDA)
This is the definitive guide to the HyperTalk scripting language. It contains descriptions to all the HyperTalk commands available, and also discusses the HyperCard environment.
- Danny Goodman's "The Complete HyperCard Handbook" (Bantam)
This was the first book available as a reference for HyperCard, and covers the tools available in HyperCard, as well as how to use the scripting abilities. It also has example scripts.
- Additional third-party books.
You should be able to find various other books on HyperCard at a local computer bookstore.
- The User Groups Icon on AppleLink.
This icon is a meeting place for Apple dealers and user groups to exchange information relating to Apple products.There is also a HyperCard folder, devoted to questions and answers and public domain and shareware utilities and stacks for HyperCard.