The Coding Part
I know this series of articles is called "10 Minutes with Flash," but there is quite a bit of code to write for our panel, so this session might run a little long.
We'll get to the cool JSFL part very soon, but first we need to write some code to handle the panel settings, import some classes, instantiate some variables, and write a couple of functions. It'll be good times. To get started:
First, add a few lines of code to align the panels contents, prevent scaling, and hide the context menu. Select the first frame of the actions layer, open the Actions panel, and add the following code (all the code in this project will be attached to frame 1):
Next, import the classes we need to control the components we added to the UI (remember, ActionScript 2.0 is case-sensitive, so watch out for capital letters):
Next, instantiate variables for each of the components and assign data types to them, and also instantiate a variable that will eventually contain the path to the user's desktop. We'll leave the values empty because the user will be adding values by filling out the form on the stage:
stop(); Stage.align = "LT"; Stage.scaleMode = "noScale"; Stage.showMenu = false;
import mx.controls.TextInput; import mx.controls.Button; import mx.controls.ComboBox;
var varName:TextInput; var valueTxt:TextInput; var osUserName:TextInput; var osType:ComboBox; var saveBtn:Button; var saveTo:String;
Now we need to create a function to handle all the data collection and save the .txt file. Several things need to happen in the function. First, we need to check the osUserName and osType components to see whether they have content. If they do, we'll determine which type of operating system was chosen by the user, and use that information to tell Flash where to save the .txt file. To do this:
Use the following function to create a string that represents the path to the user's desktop. This code creates a variable called saveTo, and then assigns its value based on which operating system was specified by the user. Each possible value is a string that is the path to the user's desktop for the chosen OS.
The next thing we need is a function that checks the osUserName and osType components to make sure that they have values, and if so, runs the createDestinationPath() function. This is a conditional statement as well, so the saveTo string is never created unless these two components have values.
function createDestinationPath(){ if(osType.text == "Win XP"); saveTo = "C|/Documents and Settings/"+osUserName.text+"/Desktop"; } else if (osType.text == "Win 2000") { saveTo = "C|/Documents and Settings/"+osUserName.text+"/Desktop"; } else if (osType.text == "Mac OS X") { saveTo = "Macintosh HD:/Users/"+osUserName.text+"/Desktop"; } }
function saveTheVar(){ if (osUserName.text !=- "" && osType.text !+ ""){ createDestinationPath(); }
Finally, we can see how to use JSFL to save the contents of this form to an external .txt file.