How do I... ?
AnswerList of Questions
Work with Tables and Files
In AutoPlay Media Studio, tables are frequently used when working with files. Specifically, we will look at how the results of a Dialog.FileBrowse action are stored in a table, and how it is possible to work with a text file loaded into a table.
Tables, in their basic form, are simply variables with many rows. The Dialog.FileBrowse action stores one file path per row in a table. When reading a text file into a table, every line in the text file is a separate row in the table.
Example 1
Using the Dialog.FileBrowse action, we will allow the user to browse a folder, and select multiple files. We will open each file with its default application:
-- Present the user with a file browse dialog (with multiple select set to true)
tFiles = Dialog.FileBrowse(true, "Multiple Select", "", "All Files(*.*)|*.*|", "", "", true, true);
-- Ensure that tFiles contains something if tFiles then
-- Check if the user pressed "Cancel" if tFiles[1] == "CANCEL" then
-- The user pressed cancel, do nothing else
-- The user did not press cancel, traverse the table of files. for nIndex, sFile in pairs(tFiles) do
-- Open file with the default program. File.Open(sFile, "", SW_SHOWNORMAL);
end end
end
In the example above, the Dialog.FileBrowse action returns a table containing all the files that were selected. If the cancel button is pressed, the first row in the table (referenced by tFiles[1]) is set to 'CANCEL'. Otherwise, each row in the table contains the path to one file.
Example 2
Using the TextFile actions, we will load the contents of a text file into a table, and insert the text "Line two is the best line ever!!" into the second line of the text file:
-- Present the user with a file browse dialog (with multiple select set to false)
tFiles = Dialog.FileBrowse(true, "Single Select", "", "All Files(*.*)|*.*|", "", "", false, true);
-- Ensure that tFiles contains something if tFiles then
-- Check if the user pressed "Cancel" if tFiles[1] ~= "CANCEL" then
-- The user did not press Cancel, continue
-- Read the selected text file to a table tTextFileContents = TextFile.ReadToTable(tFiles[1]);
-- Insert the text into line (row) two Table.Insert(tTextFileContents, 2, "Line two is the best line
ever!!");
-- Write the altered table back to the text file selected (DON'T append)
TextFile.WriteFromTable(tFiles[1], tTextFileContents, false);
end end
How do I... ?
AnswerList of Questions
Write Text to a File
When writing information to a text file, there are two options: the text can be added on to the end of the text file, or the current information in the text file can be overwritten. In AutoPlay Media Studio, this is accomplished by changing the Append value to false in any action that writes to a file.
As an example, we will write "la de da de da" to a text file, overwriting its contents. This is accomplished with the following action:
TextFile.WriteFromString("c:\text.txt", "la de da de da", false);
S c r i p t i n g G u i d e
Contents
Introduction
A Quick Example of Scripting in AutoPlay Media Studio
Important Scripting Concepts Script is Global
Script is Case-Sensitive Comments
Delimiting Statements
Variables
What are Variables? Variable Scope
Local Variables Variable Naming Reserved Keywords Types and Values
Number String Nil Boolean Function Table
Variable Assignment
Expressions and Operators Arithmetic Operators Relational Operators Logical Operators
The Length Operator Concatenation Operator Precedence
Control Structures If
While Repeat For
Tables (Arrays)
Creating Tables Accessing Table Elements Numeric Arrays Associative Arrays
Using For to Enumerate Tables
Copying Tables Table Functions
Functions
Function Arguments Returning Values Returning Multiple Values Redefining Functions Putting Functions in Tables
String Manipulation Concatenating Strings Comparing Strings Counting Characters Finding Strings Replacing Strings Extracting Strings
Converting Numeric Strings into Numbers
Other Built-in Functions Script Functions
dofile require type
Actions
Debugging Your Scripts Error Handling Syntax Errors Functional Errors Debug Actions
Application.GetLastError
Debug.ShowWindow Debug.Print Debug.SetTraceMode Debug.GetEventContext Dialog.Message
Final Thoughts Other Resources
Help File User's Guide
AutoPlay Media Studio Web Site
Indigo Rose Technical Support The Lua Web Site