Article
Difficulty Rating
3
Using Files With MetaCard
. . .

For file access, MetaCard uses the URL system. In keeping with this, all of MetaCard's file access commands use a "/" character as the directory deliminator, regardless of platform. To read a file from somewhere on your computer, you simply:

put url "file:c:/test.txt" into theVariable
put url "file:/Macintosh HD/Files/example" into theVariable
put line 3 of url "file:c:/example.txt" into theVariable
put "A line of text" into url "file:c:/examplewrite.txt"
                           
answer file "Select a document to open:"
put it into tPath
put url ("file:"&tPath) into theVariable
                           
ask file "Save this document as:"
put it into tPath
put field "text to save" into url ("file:"&tPath)
                           

Note the use of the HyperCard compatible ask and answer file commands. Further information on these commands is available in the MetaTalk reference stack.

All of the above examples open files in text mode. If you want to access a binary file:

put url "binfile:c:/test.txt" into theVariable

To access something from the internet, it changes slightly to the familiar:

put url "http://www.mysite.com/" into theVariable
                           

That command would download the HTML page at the site "www.mysite.com" and place it into theVariable.

If you want a file on the internet to be downloaded asynchronously, you can use the load command instead. This downloads the file and places it in a cache. The cache can contain multiple files, and can be read multiple times. It is important to make sure that you empty the cache whenever you are done using something in it, otherwise it will consume a lot of memory. The urlStatus() function can be used in conjunction with the load command to give a feedback bar which shows a file downloading. An example of this can be found in the MetaCard tools stack. To access the script, open the MetaCard Menu Bar as normal, then type:

edit script of stack "Download stack" 
                           

into the message box. To see it in action, choose either "tools.metacard.com" or "help.metacard.com" from the appropriate menu on the MetaCard menu bar.

Tip:It is possible to download images from the web then import them into MetaCard for display. Simply "put" the image data you downloaded into an image object. As ever, copying this stack and scripts for use in your own stacks is probably the best way to get started.

You can download any type of document from the web. The most common use for this feature is to load MetaCard stacks that are served somewhere on the internet.

You can put things into files by using URL too:

put "some text" into word 3 of line 2 of url "file:c/test.txt"
                           

Other File Functions

URL is the normal way of accessing files in MetaCard. Traditional (HyperCard compatible) file access functions are also supported. In almost all cases, you will find the URL function faster and more convenient to use.

Here are the basic set of HyperCard compatible file functions:

open file "c:/test.abc" for text read
read from file "c:/test.abc" until eof --eof stands for "end of file"
put it into tFileContentsVariable
close file "c:/test.abc"
                           

Of course, you can also open files for writing:

Tip: Make sure you understand all of these file modes, and pick the right one each time. Its tempting to omit the parameters (so both allowing read and write of any file you open), but this is much less efficient then specifying exactly what you want to do. Also, failing to specify binary when you want to work with binary data will not give the desired results.
open file "c:/test.abc" for write
                                       

The append mode always adds to the file at the end, not the start, which avoids overwriting the contents:

open file "c:/test.abc" for append
                                    

Both reading and writing:

open file "c:/test.abc" for update
                           

Finally, you can do all of the above in binary mode, e.g.:

open file "c:/test.abc" for binary read
open file "c:/test.abc" for binary append
                           

Once a file has been opened for writing, appending or updating, you can of course write to it:

write theVariable to file theFilePath
                           

Finally, an often forgotten command is seek:

seek to 200 in file theFilePath
read from file filePath for 5 lines
                           

In the above example, the first 200 characters in the file are skipped, then the next five lines read. The advantages of doing this rather than reading the file is that its faster, and doesn't take up memory to store the contents of the file. Its a bit like moving an invisible insertion point marker. You can also seek backwards from the end of a file, in exactly the same way you use any chunk expression backwards. For example, to seek to the character 50 characters before the end of a file:

seek to -50 in file theFilePath
                           

Always remember to close any file you've finished working with. This will make the file available to other applications, and free up memory.

Managing files: copying, deleting, and searching, compressing

MetaCard uses only a handful of commands for managing files. However, when you put them together, you can do quite a few things. For example, to copy a file you need five lines:

answer file "Select a file to copy:"
put it into tOriginalFilePath
ask file "Save the copy as:"
put URL ("binfile:"&tOriginalFilePath) into URL ("binfile:"&tNewFilePath)

To compress a file:

                           answer file "Select a file to compress:"
put it into tOriginalFilePath
put it & ".gz" into tNewFilePath --add a .gz extension
put compress(URL ("binfile:"&tOriginalFilePath)) into URL ("binfile:"&tNewFilePath)

To decompression is similar:

answer file "Select a file to decompress:"
put it into tOriginalFilePath
put it into tNewFilePath
if char -3 to -1 of tNewFilePath is ".gz" then delete char -3 to -1 of tNewFilePath
put decompress(URL ("binfile:"&tOriginalFilePath)) into URL ("binfile:"&tNewFilePath)
                           
                           
                           

On the Macintosh platform, its a little more complex. Mac files contain two forks: the data fork and the resource. In this case, you must use the getResource(),copyResource() and the URL type "resfile:" to copy the resources in the first file to the second.

Tip:

You can find out if a particular file exists using the syntax:

if there is a file someFile then...
         

You can rename a file using the rename command.

rename file "C:/example.txt" to "example2.txt"
                           

To delete a file use:

delete file theFilePath
                           

Be careful with this command!

You can use MetaCard to read in lists of files in directories, or search through directories for files on disk. The principle is simple. Use the directory function to change to the directory you want to query, e.g.:

set the directory to "C:/"
                           

Then get a list of the files in that directory using the files function:

put the files into theListOfFilesOnDriveC
                           

Now, get the all the sub-directories:

put the directories into tDirectoriesList
                           

Using files with external applications

To open a file with another application, e.g. to view an Acrobat document in Acrobat, you use the shell function:

get shell(quote & tAcrobatReaderPath & quote && quote & tAcrobatDocumentPath & quote)
                           

On MacOS systems you use the launch command:

launch "/Macintosh HD/My Application"
                           

The use of quotes is required or the function will fail on paths that contain spaces. On Windows 95, you'll need to set the hideConsoleWindows global property to true, to avoid seeing the "MS-DOS" prompt screen that runs when you use shell().

You can also use the open process command to load applications in a similar way.

You can use the shell() function to run any MS-DOS command on Windows based systems, or a command line instruction on UNIX based systems.

Tip:

To find the path to an application (such as Acrobat Reader, above) on Windows 95, use the queryRegistry function. Acrobat can be found with:

put word 1 to -2 of queryRegistry("HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AcroExch.Document\
shell\open\command\") into gAcrobatPath
                                    

Why word 1 to -2? The registry entry is stored with position of the allowable parameters at the end. Taking word 1 to -2 takes the first word up to the word second from the end, removing those parameters.

Another useful one is being able to get the default web browser on Windows systems:

put word 1 to -2 of queryRegistry("hkey_local_machine\software\classes\http\shell\open\
command\") into gWebBrowserPath

Did you find this article useful? Have any ideas for future topics? Email Us!