The Cool JSFL Part
Now, let's run some JSFL commands from within the panel to dump the values of the varName and valueTxt components into the Output panel and save the contents of the Output panel to the path specified by the saveTo string (which is created by the createDestinationPath() function). To do this:
Following the closing curly brace for the if/else statements, but before the closing curly brace for the saveTheVar() function, add the following code to create the string of content that will be saved into the .txt file. This line of code creates a variable named txtFileContent and assigns it a value. The value is comprised of the text that appears in the varName component, followed by an equals sign (=) operator, followed by the text that appears in the valueTxt component, and followed by an ampersand (&), which will terminate the variable in the .txt file. (All that in one line of code!)
The next four lines of code we write will run JSFL commands from within our completed panel. Running JSFL commands from within regular ActionScript requires the use of the MMExexcute() command. To start, we'll clear the contents of the Output panel to make sure that it's empty before we dump our own content into it.
Next, we'll trace the txtFileContent string we created a moment ago into the Output panel.
Next, we'll save the contents of the Output panel. To so this, we'll call the OutputPanel.save() command and use the saveTo string we created earlier as the path. We'll also append the saveAs name to the path so Flash knows what to name the file upon saving it. Finally, we append the ".txt" file extension to the string.
Finally, we'll clear the Output panel once again, because there's no compelling reason to display the saved content.
txtFileContent = varName.text + "=" + valueTxt.text + "&";
MMExecute("fl.outputPanel.clear();");
MMExecute("fl.outputPanel.trace('"+txtFileContent+"')");
MMExecute("fl.outputPanel.save('file:///"+saveTo+"/"+saveAs.text+".txt');");
MMExecute("fl.outputPanel.clear();");
TIP
If it's difficult to tell where all the quotation marks and apostrophes go, you can simply copy and paste the code above in your project.
All we need to do now is tell the Save button to run the saveTheVar() function, publish the panel, and test it out. Accessing the Output panel through JSFL can be done only from within the Flash authoring environment. To test the panel, we cannot simply run a test moviewe have to put the panel into the install directory and open it.
To finish up:
Still on frame 1 of the actions layer, add the following code to run the saveTheVar() function from the saveBtn:
Save your work.
Open the Publish Settings dialog box. In the Formats tab, click the folder icon to the right of the row named Flash to open the Select Publish Destination dialog box. Browse to the WindowSWF folder in your Flash install directory (inside the Configuration folder for your user account) and choose it.
Uncheck HTML because we don't need to create an .html page for our panel, click Publish, and click OK to exit the dialog box.
Restart Flash (this is necessary the first time you add an .swf file to the WindowSWF folder).
Choose Window > Other Panels > External Var Creator to open our new panel, as shown in Figure 4.
saveBtn.onRelease = function (){ saveTheVar(); }
Figure 4 A cool new panel that uses JSFL to save a name=value pair to an external .txt file.
If you've followed along and done everything according to my instructions, you should now have a working panel. Enter a variable name, enter some text for the value of the variable, choose your operating system, enter your user name for your operating system, and click the Save the Variable button. You should see the Output panel pop open, but it will be empty. Although this may seem strange, we told it to do this. We told it to clear its contents, display the txtFileContent string, save it, and clear itself again. Unfortunately, there's no way to then tell the panel to close.
Now for the best part: go look at your desktop. You should see a shiny new .txt file there. It was created and saved from your panel via the magic of JSFL.
Until next time, happy Flashing!