Command Reference - Action Examples
|
Actions used: |
In this example, we'll show you how to load text in from a text file, perform a simple search and replace on the file, and then create a new text file containing the modified text. Specifically, we'll show you how to change every occurrence of the word "Hamlet" in the Project Gutenberg edition of Shakespeare's play to the word "Hamburger". Since Hamlet weighs in at around 189 kilobytes, reading and writing the text can take a few seconds, so we'll use "busy" dialogs to let the user know what's going on.
Here's what the action list looks like:

First, we use a "Dialog - Busy" action to tell the user what's happening while our application reads in the text file.

Note that we set the Display field to "TRUE" to make the dialog appear; when we want to make the dialog go away, we'll use "FALSE" there instead.
Then we use a "Text File - Read" action to load the contents of our Hamlet.txt file (located in the same folder as the AutoPlay application, represented by the built-in variable %SrcDir%) into a variable, which we called %Hamlet%.

Note that variables can hold very long strings with no problems, and all of the carriage returns and line feeds in the text file will survive the trip just fine.
Next, we use another "Dialog - Busy" action to display a different busy message, since we're done reading the text file and are ready to do the search and replace.

Then we use a "String - Replace" action to create a new string based on the text stored in %Hamlet%, with every occurrence of the word "Hamlet" replaced by the word "Hamburger". This new string is then stored in a variable which we've called %Hamburger%.
In other words, we take %Hamlet%, replace every "Hamlet" in it with "Hamburger", and store the modified version in %Hamburger%. (Note that the original text inside %Hamlet% remains unchanged.)

Next, we use another "Dialog - Busy" to tell the user that we're writing the modified file...

...and then we use a "Text File - Write" action to write the contents of our %Hamburger% variable to a text file called Hamburger.txt.

Finally, we use one last "Dialog - Busy" action to dismiss the busy dialog. (Putting "FALSE" in the Display field makes the dialog disappear.)

Here's a link to the finished example, as an AutoPlay Media Studio 4.0 page that you can import into your own project: