Triggers: Difference between revisions

160 bytes removed ,  6 October 2016
m
no edit summary
mNo edit summary
mNo edit summary
Line 232: Line 232:


== Working with files ==
== Working with files ==
With scripts you can read or write files located in the device's internal memory or on the SD card. All file operations performed by the File object, which is obtained through a method call file().
With scripts you can read or write files located in the device's internal memory or on the SD card. All file operations are performed by the File object, which is obtained through a method call file().


To work with the files for the library to be set to a resolution - on the reading and / or writing files.
To work with the files, the library should have read/write file access.


=== Global Functions ===
=== Global Functions ===
==== file(name) ====
==== file(name) ====
Open a file for read or write operations.
Open a file for read or write operations


:;Argument
:;Argument
:: '''name''' &mdash; The name and the full path to the file.<br/>For example, if the file is located on the SD card, the path should look /sdcard/example.txt, assuming /sdcard as the path on your platform to the SD card.
:: '''name''' &mdash; The name and the full path to the file.<br/>For example, if the file is located on the SD card, the path should be something like /sdcard/example.txt.


:; Result
:; Result
:: Object file
:: File object


=== Object File ===
=== Object File ===
Line 256: Line 256:


; readLine()
; readLine()
: Reads a line from the file
: Reads the next line from the file
:; Returns: The line
:; Returns: The line


Line 264: Line 264:


; readChar()
; readChar()
: Reads a character
: Reads the next character
:; Returns: The character
:; Returns: The character


Line 286: Line 286:


<source lang="javascript" line>
<source lang="javascript" line>
f = file("/sdcard/myfile.txt")
f = file("/sdcard/myfile.txt") // Open the myfile.txt file on the SD card. If the file does not exist,
f.writeLine("one");                      
                                // it will be created.
f.writeLine("one");             // Write the string "one" as a line to the file
f.writeLine("two");
f.writeLine("two");
f.writeLine("three");
f.writeLine("three");
f.close();    
f.close();                     // Close the file, saving the data to the file. Until the file is closed,
var a = f.readLines();
                                // the disk file is still empty.
var a = f.readLines();         // Read all lines of the file into the array a
</source>
</source>


<ol>
====Save an entry into a file in XML format ====
<li>Open myfile.txt file on the sdcard. If the file does not exist, it is created.</li>
The entry includes fields: id , title , date.  
<li>Write the string "one" as a line to the file</li>
</ol>
<ol start="5">
<li>Save and close the file</li>
<li>Read all the lines of the file into the array a</li>
</ol>
 
====Save a file to a record ====
Необходимо сохранить запись в формате xml. Запись имеет следующие поля: id , title , date.  
<source lang="javascript" line>
<source lang="javascript" line>
var xml = '<record id="' + entry().field("id") + '">' +  
var xml = '<record id="' + entry().field("id") + '">' + // Format XML record from the entry field values
'<title>' + entry().field("title") + '</title>' +
'<title>' + entry().field("title") + '</title>' +
'<date>' + entry().field("date") + '</date>' +
'<date>' + entry().field("date") + '</date>' +
'</record>';
'</record>';
f = file("/sdcard/" + entry().field("title") + ".xml");
f = file("/sdcard/" + entry().field("title") + ".xml"); // Open file with same name as Entry Name
f.write(xml);
f.write(xml);                                           // Save XML data into the file
f.close();
f.close();                                               // Close the file
</source>
</source>
<ol><li>Формируем xml данные из значений полей для записи в файл.</li></ol>
<ol start="5">
<li>Открываем файл, имя файла будет таким же как и имя записи.</li>
<li>Сохраняем в файл xml-данные</li>
<li>Закрываем файл</li>
</ol>


==Выполнение http запросов==
==Выполнение http запросов==