How To Get A Flash Projector To Select A File On Disc

April 8th, 2008

I recently completed a dvd-rom project that inspired me to write this quick tutorial. A client wanted to create a library of all their television promos - a disc they could distribute to their sales force containing all of the promos in a format that could be used in Power Point presentations. The disc needed to have an interface that allowed the user to search through the contents of the disc, play flv versions of the promos within the interface, and click a button that would reveal the location of the video file on the disc. It was this last part that proved trickiest - getting Flash to communicate with the operating system and open folders and select files wasn’t as easy as I thought it would be. If you need to do something similar, hopefully this tutorial will help.

My specific project involved hundreds of video files that needed to be categorized and search-able. So I created an xml database that the Flash app used to create a menu structure. But we’ll simplify things for this example. Let’s just say we’ve got some images on a disc, and we want to include a simple Flash projector that will display them all, and provide a link to the individual image files.

(One note: The technique I’m sharing here was cobbled together using many different bits of code found all over the web. I should have kept better track. I will mention I got a good deal of help on the Applescript side from the folks contributing to the MacScripter boards.)

The basic elements of this technique are:

  • Display a button next to each image
  • The button triggers the fscommand function, which in turn executes a script located on the same disc as the Flash projector
  • The script that is executed tells the operating system to open a directory and select a specific file.

The “script” I’m referring to is either an Applescript file for Mac, or a batch file (.bat) for Windows. You’ll probably want the disc to be cross-platform, so you’ll need to create both.

Unfortunately, you can’t pass any variables using fscommand, so you have to create a separate script for each file that needs to be revealed on the disc. If you only have a couple files that’s no big deal. But when you have hundreds of files, you have to automate it somehow. I won’t go into a ton of detail about that in this article, but I’ll mention the basics at the end.

Here’s a simplistic visual example of what you would have going on in the Flash file:

Create event handlers for the buttons that look something like this:

this.btn1.onRelease = function() {
var platform = substring(_level0.$version,0,3);
if(platform == "WIN") {
fscommand("exec", "SOME_IMAGE.bat");
} else {
fscommand("exec", "SOME_IMAGE.app");
}
}

Pretty basic stuff here. You’re checking to see which platform you’re running on, and calling the appropriate fscommand based on the result. Note that in a more common application you might have imported some xml and would be creating a menu structure based on that. You would then create a function that would assign event handlers with variables as the path parameter.

Important: The batch files and Applescript files and any other file that is to be executed by fscommand must reside in a directory named “fscommand” - and it must be in the same directory as the projector file. Also, to test the functionality you’ll need to publish the projector and run it - it won’t work if you’ve simply chosen Test Movie within Flash.

Let’s look at these Applescript and Batch files. Mac is better, so we’ll start with the Applescript ;)

property fileName : "gif:SOME_IMAGE.gif"
set myPath to (path to me as string)
set AppleScript's text item delimiters to ":"
set the parentFolder to �
((text items 1 thru -3 of myPath) & "") as string
set AppleScript's text item delimiters to ""
set targetFile to alias (the parentFolder & fileName)
tell application "Finder"
activate
select file targetFile
end tell

The script simply specifies the path to the file we need selected relative to itself. This way, when the application is running off of the DVD-ROM it will be able to find the specified file. NOTE: You need to save the Applescript as an executable file, an application (.app) - use Script Editor for this.

Here’s the contents of the Batch file:

set driveLetter=%~d0
EXPLORER /select,%driveLetter%\gif\SOME_IMAGE.gif

If you need to automate the creation of the Applescript and batch files, there is more than one way to approach it. I decided to create a very simple mysql database - literally just a list of file names, and then wrote a php script that created one Applescript file and one batch file for each entry in the database. A little beyond the scope of this article, but I’ll go into it more in a later article if there’s interest. That’s it!

Leave a Reply