BRIGHAM YOUNG UNIVERSITY
BYU LiveCode Lessons Gateway
Office of Digital Humanities
Computers & the Humanities 210
Exploring External Files Exercise
This exercise is designed to help you become familiar with using external file references in LiveCode. Mastery of this skill will make LiveCode a much more powerful tool for you when building stacks that access multimedia files.
Card 1. Referencing by File Path for Image and Player Objects
Instructions: Download the folder called "ExtFilesEx" from the Templates folder on the server. Be sure to get the entire folder and all the files inside it. Follow the steps below. This is an in-class exercise only. It does not need to be turned in.
- Open the property inspector for image "extImg". In the Basic panel, click the browse button (it is next to the Source field, and looks like a file folder) and select an image file from the image folder accompanying this stack.
- Use the message box to put the filename property of the image into field "imgResult".
- Repeat steps 1. and 2. for player "extMedia", only this time choose a movie (.mov) file or a sound (.mp3, .aif, or .wav) file.
- Note that the field labeled "Source" in the property inspector for these objects holds the fileName property for each object.
- Now open each of the property inspectors and delete everything in the Source field except the file name (the very last part of the file path.) Note what happens when you exit the source field.
Why do the contents of the objects disappear?
- What is the defaultFolder property currently set to? To find out, use the message box to put it into field "folderResult".
- Write a handler for button "ChangeDefault" that will set the defaultFolder to the folder this stack is in. See the reading for the short scripting routine that will do this. Add a line to this handler that will put the new setting of the defaultFolder property into field "folderResult".
- Now open the image object property inspector again, and enter the name of the folder that holds the external image files before the name of the image file in the Source field. Be sure to add a "/" after the folder name to separate it from the file name. The Source field should now look something like this:
images/pictureName.png. The image should reappear in the object if you have done this correctly.
- Try changing the file name in the Source field to another image file in the Images folder. Observe that the image changes. Why don't you need the entire path name to reference the image object now?
- Now we're going to do the same thing with the player object, except we are going to do it using commands in the message box.
- Enter a message box command that will put the name of the folder containing the movie or sound file before the name of the file. To do this, remember that the text in the Source field of the property inspector is the filename property.
So you will have to
set the filename of player "extMedia" to "folder name/file name". (Use the actual file and folder names in the command in place of the text in italics.)
- From the message box, put the filename of the player object into field "playerResult".
- Remember that you can also use the contents of fields in LiveCode scripting expressions. Try changing the text in field "PlayerResult" so that the file name is the name of a different movie or audio file in one of the folders. Now write a message box command to set the filename property of the player object so that the player will show the file whose name is in field "playerResult". Remember the format:
set the filename of <object> to <field>.
- Take what you learned in 10c. above and create buttons that will change what is showing in the image object and the player object to the text that is showing in the imgResult and playerResult fields, respectively.
Card 2. Putting Text from External Files into a Field
- The command for putting external text into a field is in this form:
put URL "file:path to file" into field "content"
path to file is a valid file path.
- Use what you learned on the previous card about file paths and the default folder. Enter the path to the text file in field "textURL". Now write a handler in button "getText" that will put text from one of the text files in the folder you downloaded into the text field. Try experimenting with both absolute file paths and relative file paths. Remember that if you use a relative file path it will be in relation to the current defaultFolder setting.
- Now let’s look at how to save text to an external file. Create a button called "saveText". In this button write a script for putting the field's text into an external file. This is done by simply reversing the direction of the 'put' command, like this:
put field "content" into URL "file:path to file"
path to file is a valid relative or absolute file path.
A key stack with suggested solutions will be available in the Keys folder on the server.
BYU LiveCode Lessons Gateway
Maintained by Devin Asay
Copyright © 2005 Brigham Young University