Triggers: Difference between revisions

Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 336: Line 336:


=== Files Examples ===
=== Files Examples ===
-----
See '''[[Trigger Examples#Files Examples]]'''.
==== Writing & reading from a file ====
 
:; Add trigger(s): This script could be a part of any phase of any event.
 
:'''Trigger script:'''
<source lang="javascript">
f = file("/sdcard/myfile.txt"); // Open myfile.txt on the SD card
                                // If no file, it will be created
f.writeLine("one");            // Write "one" as a line to the file
f.writeLine("two");
f.writeLine("three");
f.close();                      // Close & save. Until closed,
                                //  the file is still empty
var a = f.readLines();          // Read all lines into array a
</source>
<br/>
-----
==== Save an entry to a file in XML format ====
The entry includes fields: id , title , date.
 
:; Add trigger(s): This script could be a part of any phase of any event.
 
:'''Trigger script:'''
<source lang="javascript">
var xml = '<record id="' + entry().field("id") + '">' +  // Format XML record
'<title>' + entry().field("title") + '</title>' +        //    from entry field values
'<date>' + entry().field("date") + '</date>' +
'</record>';
f = file("/sdcard/" + entry().field("title") + ".xml");  // File name is Entry Name
f.write(xml);                                            // Save XML data to the file
f.close();                                              // Close the file
</source>
<br/>
<br/>
-----
-----