Articles Archive
Articles Search
Director Wiki
 

Copy files in Director

January 17, 1999
by Pat McClellan

Dear Multimedia Handyman,

How do I move files from one folder to another using Director? And if possible, put a select folder to choose where I will put my files.

Carlos Arnt

Carlos,

A good question... and one I've been wanting to write about since I just did this exact thing for a CD-ROM 2 weeks ago. This is a very handy thing to be able to do. The CD-ROM program I just finished was a recap of a corporate meeting, featuring Quicktime movie clips of presentation highlights. The client wanted a button beside the Quicktime which would allow the user to download the Powerpoint file which was used in the corresponding presentation. So, I had a folder full of Powerpoint files -- named "pptfiles".

To move a file from the CD-ROM to a local drive while in Director, you need to use an xtra. There may be others available, but I have had great success using Glenn Picher's ProgressCopy Xtra. When I originally bought it a couple of years ago, it was being marketed by g/matter, but it is now available from updateStage. The Xtra is called ProgressCopy because it features a progress bar indicator as the file is moved to the user's drive. This is an important feature because, while the file is transferring, the processor is pretty busy and your Director movie performance would suffer. The progress bar prevents the user from proceding, while keeping them happy by showing how much longer they'll have to wait.

ProgressCopy Xtra comes with all the Lingo you need, so you can do what is needed with only a couple of simple lines of code. Basically you just have to tell it "copy thisFilePathAndName to thisFilePathAndName", and it does it. There are some other little features you can use. For example, as the file is copied and the progress bar is displayed, you can have 3 lines of text displayed in the dialog box. For example...

Again, a single line of code send this info to ProgressCopy. Easy. With a little Lingo string concatonation, you could insert the actual file names in the string to be displayed. That's all you need to know about ProgressCopy. Now let's figure out how we're going to allow the user to specify the destination filepath and filename.

FileIO Xtra is a great little xtra that ships (free) with Director. It is designed for reading and writing text files to and from your Director movie. For example, if your movie allows the user to input personal data, you might want to be able to write this info out to a text file on the user's drive. FileIO is NOT designed to copy files outside the Director movie to another location. In other words, it doesn't do what ProgressCopy does. So how does this help us?

One of the handlers of FileIO is the displaySave() method. When you call displaySave(), it opens a standard Mac or Windows OS "Save As..." dialog box. The user can then browse around their drive to select a location to write the file to. When they click the Save button, all that does is to tell Director the filepath and filename that the user has selected. Note that you can also specify a single line of text to appear in the dialog box. Here, I've used "Save this Powerpoint file to..." to cue the user to what they're supposed to do.

Back to my example project. Since I was going to have to apply this download capability to well over 25 files, I figured that it was a good candidate for a behavior which would allow me to specify the file name and the directory that it's in. That way, I could use the same behavior over and over. The user would be specifying the destination, so I only need to specify the filepath and filename of the file to be copied. I also wanted to build in the option to change the strings of text displayed in the FileIO and ProgressCopy dialog boxes -- though I inserted default strings that meet my immediate needs.

The getPropertyDescriptionList handler below will generate the dialog box you see here. If you want other strings to be the defaults (in case you're downloading pdf or doc files, for example), then just change the default strings in the handler. This handler takes care of assigning the filename of the file to be copied. We'll assign the filepath to that file, and let the user assign the filepath and name in the mouseUp handler.


-- SaveFileTo behavior
-- copyright © 1999, ZZP Online, LLC 
property pWhichFile, pDirectory, pFileIOstring, pString1, ¬
  pString2, pString3
on getPropertyDescriptionList me
  set pdlist to [:]
  addprop pdlist, #pWhichFile, [#comment:"Filename", #format:#string, #default:""]
  addprop pdlist, #pDirectory, [#comment:"Directory", #format:#string, #default:""]
  addprop pdlist, #pFileIOstring, [#comment:"FileIO string", ¬
    #format:#string, #default:"Save this Powerpoint file..."]
  addprop pdlist, #pString1, [#comment:"String1", #format:#string, ¬
    #default:"This Powerpoint presentation"]
  addprop pdlist, #pString2, [#comment:"String2", #format:#string, ¬
    #default:"is being copied to your drive."]
  addprop pdlist, #pString3, [#comment:"String3", #format:#string, ¬
    #default:"Please wait..."]
  return pdlist
end getPropertyDescriptionList
on mouseUp me
  set myFile = new(xtra "fileio")
  set destinationFileName = displaySave(myFile,pFileIOstring, pWhichFile)
  if the platform contains "mac" then
    set fileSeparator to ":"
  else
    set fileSeparator to "/"
  end if
  
  set filePath = the pathName & pDirectory & fileSeparator & pWhichFile
  
  -- set the dialog box strings
  progressCopyStrings pString1, pString2, pString3
  
  -- send the command to ProgressCopyXtra
  progressCopyFile filepath, destinationFileName, 1, 1, 1
  
  -- dispose of xtra
  set myFile = 0  
end 

The mouseUp script is where the "good stuff" happens, so let's look through it in sequence. Start by creating an instance of the FileIO xtra. If you've never used an xtra or created an object, this might seem strange to you. Basically, we're calling upon Director to open a copy of the FileIO Xtra. Once it's open, we can use special Lingo that only that Xtra understands.

The next line uses one of those special FileIO lingo terms: displaySave (). This line opens the Save As... dialog box and assigns the user's selection (in filepath & filename format) to the variable destinationFileName. The parameters for displaySave -- the part inside the parentheses -- include the "instance" of the xtra, which is the variable name we assigned in the first line; the string (or words) to be displayed in the Save As... dialog box; and the fileName of the original file (which we assigned to our pWhichFile property in the getPropertiesDescriptionList handler.) If you're new to all of this, I know that seems complicated. Take a moment to make sure you understand what's going on so far.

OK, now that the user has specified the filepath and filename (they can rename the file when it gets copied), we need to create the full absolute pathname to the original file. This can be a little tricky, because you (probably) don't know whether your user is on a Mac or Windows machine. So, the pathname to your file (reed.ppt in this example) will vary as follows:

Windows

d:/pptfiles/reed.ppt  

(though you don't know the drive letter for sure)

Mac

My CD:pptfiles/reed.ppt 

(where "My CD" is the name of your cd-rom)

On Windows, you don't know what drive letter is assigned to the CD-ROM drive. On the Mac, the filepath is fully predictable, but the symbol used between the levels in the filepath is a colon, whereas a slash is used in Windows. Luckily, we can deal with this pretty easily. First, we can get the first part of the filepath (up to the level where the director movie is located) by simply using the term "the pathname". On Windows, this will give us the drive letter. So, assuming that the Director movie is located at the same level of the CD-ROM as the directory called pptfiles, the pathname would give us the following:

Windows

"d:/"

(assuming that the CD drive is d)

Mac

"My CD:"

The rest of the filepath is predictable when we author the program. We can simply add the name of the directory (which is stored in our pDirectory property) and the filename. But, we don't know whether to use colons (for the Mac) or slashes. The answer is to test which platform we're on and assign the fileSeparator based on the result. That's what this does:


if the platform contains "mac" then
  set fileSeparator to ":"
else
  set fileSeparator to "/"
end if

We can build the full path name to our original file like this:


set filePath = the pathName & pDirectory & fileSeparator & pWhichFile

Now comes the easy part. We know all of the necessary information. We just have to pass it to ProgressCopy Xtra. First we pass the three text strings to be displayed, then we call progressCopyFile. Only two lines of code? Actually we're calling complex handlers which are supplied when you buy ProgressCopy Xtra. That part is done for you. The last line of our handler is to dispose of our instance of FileIO. That means you're telling Director that it can "close" the copy of the xtra -- we don't need it anymore. It's fine to open and close a copy of the xtra for each of the files you transfer. Keeps things tidy.

Good luck with your program, and don't forget to include both xtras on the CD-ROM -- otherwise none of this code will work!

Note: This behavior requires some handlers that are found in the movie "TestD5.dir" which is distributed with ProgressCopy. Copy the movie script "Cut and paste Lingo handlers" into your own movie before using this behavior.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.