Triggers: Difference between revisions

From Memento Database Wiki
Jump to navigation Jump to search
mNo edit summary
 
(192 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Stamp|2016-11-09|4.0.0|1.0.6}}
{{Stamp|2023-08-12|5.0|2.0}}
{{BannerMenu}}
{{PageTitle|Triggers}}


A Trigger is a script that defines the processing of an entry based on an event that is taking place. Trigger scripts are written in [https://developer.mozilla.org/en-US/docs/web/JavaScript JavaScript]. When a trigger script is executed, it may perform actions like changing an existing entry, creating a new entry, executing an HTTP request, creating a file, performing data validation, etc.
A '''''Trigger''''' is a script that defines the processing of an entry whenever a particular kind of event takes place. Trigger scripts are written in '''[https://developer.mozilla.org/en-US/docs/web/JavaScript JavaScript]'''. When a trigger script is executed, it may perform actions like changing an existing entry, creating a new entry, executing an HTTP request, creating a file, performing data validation, etc.
-----


=== Terminology ===
== Definitions ==
So, we define the following terms:
We define the following terms:
:; Event type
:; Event type
:: One of the following:
:: One of the following:
::* Opening a library
::* Creating an entry
::* Creating an entry
::* Updating an entry
::* Updating an entry
::* Updating a field
::* Deleting an entry
::* Deleting an entry
::* Linking an entry
::* Unlinking an entry
::* Opening an Entry Edit card
::* Opening an Entry Edit card
::* Adding an entry to Favorites
::* Adding an entry to Favorites
Line 15: Line 22:


:; Phase of an Event
:; Phase of an Event
:: One of a predefined set of moments during entry processing during which the user can intervene via a trigger script. See [[#Events|the table of events and phases below]].<br/> The Event type and the Phase determine the trigger script to be run.
:: One of a predefined set of moments during entry processing for each Event type during which the user can intervene via a trigger script. See [[#Events & Phases|the table of events and phases below]].<br/> The Event type and the Phase determine the trigger script(s) to be run.


:; Trigger or Trigger Script
:; Trigger or Trigger Script
:: A script that may be defined to run when an event occurs for an entry<br/>The trigger (Event type & Phase) and the corresponding trigger script are one-to-one.<br/>When referring specifically to the script, it is called the ''trigger script''. When referring to the Event type & Phase and its listing in the trigger list, it is referred to merely as a ''trigger''.
:: A script that may be defined to run when an event occurs for an entry<br/>The trigger (Event type & Phase) and the corresponding trigger script are one-to-one.<br/>When referring specifically to the script, it is called the ''trigger script''. When referring to the Event type & Phase and its listing in the trigger list, it is referred to merely as a ''trigger''.


== Mode of script execution ==
=== Mode of script execution ===
The phase in which the trigger is activated defines its mode of execution &mdash; synchronous or asynchronous.
The phase in which the trigger is activated defines its mode of execution &mdash; synchronous or asynchronous.


; Synchronous script execution mode
:; Synchronous script execution mode: Memento suspends user interaction and then executes the script. In the case of '''Before...''' phases, there is an impending operation that will take place unless the script forestalls it by calling ''cancel()''.<br/>Time-consuming operations are not recommended in this mode.
: The application suspends user interaction and then executes the script. Time-consuming operations are not recommended in this mode.


; Asynchronous script execution mode
:; Asynchronous script execution mode: The script runs in the background; user interaction is '''not''' suspended. Usually, asynchronous scripts are used in the last phases of the action, after any save or other operation is initiated.
: The script runs in the background; user interaction is '''not''' suspended. Usually, asynchronous scripts are used in the last phases of the action.
-----
 
=== Scripting for creating and for updating entries ===
== Creating a trigger ==
If you want the same logic to be used in both your ''Creating'' scripts and your ''Updating'' scripts, it is tempting to just copy the code for one and paste it into the other, and that can be  done in '''''in most cases'''''. If you write your ''Updating'' script first, the copy will work. If you write your ''Creating'' script first, then you '''''may''''' still be able to copy, but if you needed for the ''Creating'' Script to be executed ''Before saving'', you'll need to change ''defaultEntry()'' to use ''entry()'' instead.
Each library can have a number of triggers &mdash; up to one for each Event type & Phase. To see the list of triggers, open the library, open the menu, and then select ''Triggers''.


To create a trigger, press the 3-dot icon in the upper-right corner of the screen to open the Action Menu; then press Triggers to open the list of existing triggers; then click <big>'''+'''</big>. You must then identify the Event type & Phase and write a trigger script that performs the necessary actions.
== Events & Phases ==
 
== Events ==
These are the defined ''Event types'', ''Phases'', and their corresponding modes of execution.
These are the defined ''Event types'', ''Phases'', and their corresponding modes of execution.
{| class="wikitable" style="font-size:120%"
{| class="wikitable" style="font-size:120%"
|+ THE DEFINED EVENT TYPES & THEIR PHASES
|+ THE DEFINED EVENT TYPES & THEIR PHASES
|-
|-
! Event type !! Phase !! Execution mode
! Event type !! Phase !! [[#Mode of script execution|Execution mode]]
|-
|-
| rowspan="3" | '''Creating an entry'''
|| '''[[#Opening a library|Opening a library]]'''
| '''Opening the library''' || synchronous
|-
| rowspan="3" | '''[[#Creating an entry|Creating an entry]]'''
| '''Opening an Entry Edit card''' || synchronous
| '''Opening an Entry Edit card''' || synchronous
|-
|-
Line 48: Line 54:
| '''After saving the entry''' || asynchronous
| '''After saving the entry''' || asynchronous
|-
|-
| rowspan="3" | '''Updating an entry'''
| rowspan="3" | '''[[#Updating an entry|Updating an entry]]'''
| '''Opening an Entry Edit card''' || synchronous
| '''Opening an Entry Edit card''' || synchronous
|-
|-
Line 55: Line 61:
| '''After saving the entry''' || asynchronous
| '''After saving the entry''' || asynchronous
|-
|-
| rowspan="2" | '''Deleting an entry'''
| rowspan="2" | '''[[#Linking an entry|Linking an entry]]'''
| '''Before deleting an entry''' || synchronous
| '''Before saving the entry'''  || synchronous
|-
| '''After saving the entry''' || asynchronous
|-
| rowspan="2" | '''[[#Unlinking an entry|Unlinking an entry]]'''
| '''Before saving the entry''' || synchronous
|-
| '''After saving the entry''' || asynchronous
|-
| rowspan="2" | '''[[#Deleting an entry|Deleting an entry]]'''
| '''Before deleting the entry''' || synchronous
|-
|-
| '''After deleting an entry''' || asynchronous
| '''After deleting the entry''' || asynchronous
|-
|-
| rowspan="2" | '''Opening an Entry View card'''
| rowspan="2" | '''[[#Opening an Entry View card|Opening an Entry View card]]'''
| '''Before window display''' || synchronous
| '''Before window display''' || synchronous
|-
|-
| '''After window display''' || asynchronous
| '''After window display''' || asynchronous
|-
|-
| rowspan="2" | '''Adding entries to Favorites'''
| rowspan="2" | '''[[#Adding an entry to Favorites|Adding an entry to Favorites]]'''
| '''Before the operation''' || synchronous
| '''Before the operation''' || synchronous
|-
|-
| '''After the operation''' || asynchronous
| '''After the operation''' || asynchronous
|-
|-
| rowspan="2" | '''Deleting an entry from Favorites'''
| rowspan="2" | '''[[#Removing an entry from Favorites|Removing an entry from Favorites]]'''
| '''Before the operation''' || synchronous
| '''Before the operation''' || synchronous
|-  
|-  
Line 76: Line 92:
|}
|}


== Security ==
=== Opening a library ===
Since the scripts have access to more actions than a user does, they require additional permissions.
This event occurs when a library is opened before any activity is performed on the library.
 
This event has only one phase, and it is ''synchronous''. Therefore, the script will fully execute before any other activity takes place in the library.
The user must define these permissions manually for each library.
 
To open the dialog to set permissions for scripts, open the library triggers list and click ''Shield'' on the toolbar. Permissions must be set separately on each device. Permissions are not synchronized between devices.
 
=== Permissions for scripts ===
; Library permission
: determines which other libraries can be affected by the script. You can grant access to all libraries or select only certain libraries. This authorization is required for the libByName() function.
 
; Read permission
: grants the script read access to a file
 
; Write permission
: grants the script write access to a file
 
; Network
: grants to the script the right to execute HTTP requests


== Libraries and Entries ==
=== Creating an entry ===
=== ''Library'' Global Functions ===
This event starts when a user has requested that a new entry be added to the library, normally by pressing the '''<big>+</big>''' button on the Entries List screen. It ends after the entry is saved to storage &mdash; or else not, depending on the processing of the entry.
==== Phases ====
This event has three phases. In sequence:
:; 1. Opening an Entry Edit card (synchronous): The script will be called once the Entry Edit card is open and before any data is displayed.<br/>Initial field values (defaults) may be set.


==== entry() ====
:; 2. Before saving the entry (synchronous): The script will be called after the user has pressed the '''Checkmark''' icon and before the entry has been saved to storage.<br/>The entered data may be validated; if validation fails, the save may be forestalled and control sent back to the Entry Edit card so the user may fix the errors.<br/>Once the data is validated, this phase is an opportunity for many other functions to be performed to get data to add to the entry to be saved.
: Get the entry of the Event. For example, if script is triggered by an Update Entry event, this function will return the entry being updated.
: This function is available to all Events and Phases, with the exception of '''Creating a file''' &gt; '''Opening an Entry Edit card for add'''; for this action, use the function ''entryDefault()''.


:; Result: [[#Object Entry|'''''Entry''''' object]] &mdash; the current entry
:; 3. After saving the entry (asynchronous): The script will be called once the save of the entry has been initiated and will continue in parallel with final processing of the new entry.<br/>If you want to do something whenever a new entry is added, but not before the data is saved, this is the place to do it.


==== entryDefault() ====  
==== Scripting considerations ====
: Get the Entry object containing the default field values for the entry not yet created. This feature is available for the Event '''Creating an entry''' &gt; '''Opening an Entry Edit card'''.


:; Result: [[#Object DefaultEntry|'''''DefaultEntry''''' object]]
'''=>''''' Say what here? '''''


==== lib() ====  
===== Creating an entry =====
: Get the library of the triggering event
''Creating'' scripts are different from other trigger scripts in that, since the new entry is being crafted, it is not yet stored within Memento &mdash; the Entry object does not yet exist &mdash; so it needs to be handled differently.


:; Result: [[#Object Library|'''''Library''''' object]] &mdash; the current library
Use the method ''defaultEntry()'', rather than ''entry()'', to get the DefaultEntry object (the ''un''stored template, if you will, for the eventual Entry object).


==== libByName(name) ====
Once the default entry is saved, it can be referred to by calling ''entry()'' to get the saved Entry object. So ''Before saving'' scripts use ''defaultEntry()'', and ''After saving'' scripts use ''entry()''.
: Find the library by name. Permission to use the library is required, based on security settings.


:; Argument: The name of the library to find
=== Updating an entry ===
This event starts when a user has requested that an existing entry be edited (updated) within the library, normally by pressing the '''Pencil''' button on the Entry View card. It ends after the entry is resaved to storage &mdash; or else not, depending on the processing of the entry.
==== Phases ====
This event has three phases. In sequence:
:; 1. Opening an Entry Edit card (synchronous): The script will be called once the Entry Edit card is open and before any data is displayed.


:; Result: [[#Object Library|'''''Library''''' object]] &mdash; the library identified by the argument ''name''
:; 2. Before saving the entry (synchronous): The script will be called after the user has pressed the '''Checkmark''' icon and before the entry has been saved to storage.<br/>The entered data may be validated; if validation fails, the save may be forestalled and control sent back to the Entry Edit card so the user may fix the errors.<br/>Once the data is validated, this phase is an opportunity for many other functions to be performed to get data to add to the entry to be saved.<br/>The script could be used to update some entry data based on other entry data, to keep it all coordinated.


=== Object ''Library'' ===
:; 3. After saving the entry (asynchronous): The script will be called once the save of the entry has been initiated and will continue in parallel with final processing of the new entry.<br/>If you want to do something whenever an existing entry is updated, but not before the data is saved, this is the place to do it.
This object provides access to library entries. You can work with the current library &mdash; the lib() &mdash; or any other library in the database &mdash; libByName(). This function provides the ability to update existing entries and create new ones.


==== ''Library'' Methods ====
'''<i><b>The following 2 sections are under development.</b></i>'''
===== entries() =====
: Get all the entries of the library
:; Result: Array object containing entries, sorted by the time of their creation, from newest to oldest


===== find(query) =====
=== Updating a field ===
: Search field values within entries in the library matching the given query. This search is similar to searching via Memento's user interface.
TBD
:; Argument: '''query''' &mdash; the search string
:; Result: Array object containing matching entries. Entries are sorted by the time of their creation, from newest to oldest.


===== findByKey(name) =====
=== Linking an entry ===
: Search all entries by the Entry Name. The library must be set for unique Entry Names.
This event starts when a user has added a linked entry to a Link to Entry field either by selecting an existing entry from the entries list for the linked library or by pressing '''+''' (Plus) to create a new entry in the linked library. It ends after the entry is resaved to storage &mdash; or else not, depending on the processing of the entry.
:; Argument: '''name''' &mdash; the Entry Name field value
==== Phases ====
:; Result: [[#Object Entry|'''''Entry''''' object]]
This event has two phases. In sequence:
:; 1. Before saving the entry (synchronous): The script will be called before the entry has been saved to storage after the user has either selected an entry from the linked library or pressed the '''Checkmark''' icon after having added a new entry to the linked library from within the Link to Entry field.<br/>The entered data may be validated; if validation fails, the save may be forestalled and control sent back to the Entry Edit card so the user may fix the errors.<br/>Once the data is validated, this phase is an opportunity for many other functions to be performed to get data to add to the entry to be saved.<br/>The script could be used to update some entry data based on other entry data, to keep it all coordinated.


===== create(values) =====
:; 2. After saving the entry (asynchronous): The script will be called once the save of the entry has been initiated and will continue in parallel with final processing of the new entry.<br/>If you want to do something whenever an existing entry is updated, but not before the data is saved, this is the place to do it.
: Create a new entry in the library
:; Argument: '''values''' &mdash; [https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object '''''Object'''''] containing the field values
:; Result: [[#Object entry|'''''Entry''''' object]] &mdash; the new entry in the library


==== Properties ====
The script of this event type has access to additional functions:
: '''Title''' &mdash; The name of the library
===== masterLib() ===== 
:Returns the library from which the current record was referenced.
===== masterEntry() =====  
:Returns the entry from which the current record was referenced.
===== attr(name) =====
:Get the value of the attribute for the current reference.
===== setAttr(name , value) =====
:Set the value of the attribute for the current reference.


=== Object ''Entry'' ===
=== Unlinking an entry ===
This object holds an entry of the current library, allowing the setting of field values
This event starts when a user has requested that an existing entry be edited (updated) within the library, normally by pressing the '''Pencil''' button on the Entry View card. It ends after the entry is resaved to storage &mdash; or else not, depending on the processing of the entry.
==== Phases ====
This event has two phases. In sequence:
:; 1. Before saving the entry (synchronous): The script will be called after the user has pressed the '''Checkmark''' icon and before the entry has been saved to storage.<br/>The entered data may be validated; if validation fails, the save may be forestalled and control sent back to the Entry Edit card so the user may fix the errors.<br/>Once the data is validated, this phase is an opportunity for many other functions to be performed to get data to add to the entry to be saved.<br/>The script could be used to update some entry data based on other entry data, to keep it all coordinated.


==== ''Entry'' Methods ====
:; 2. After saving the entry (asynchronous): The script will be called once the save of the entry has been initiated and will continue in parallel with final processing of the new entry.<br/>If you want to do something whenever an existing entry is updated, but not before the data is saved, this is the place to do it.
===== set(name, value) =====
: Set the value of the named field. Calling this method results in immediate writing of the value to the library.


:; Arguments
=== Deleting an entry ===
:: '''name''' &mdash; name of the field
This event starts when a user has requested that an existing entry be deleted (actually, moved to the library's Recycle Bin) within the library, normally by pressing the '''Trash Can''' button on the Entry View card. It ends after the entry is moved to the Recycle Bin &mdash; or else not, depending on the processing of the entry.
:: '''value''' &mdash; the value of the field


===== field(name) =====
==== Phases ====
: Get the value of the named field
This event has two phases. In sequence:
:; Argument: '''name''' &mdash; name of the field
:; 1. Before deleting the entry (synchronous): This script will be called after the user has requested the deletion of the entry and pressed '''Yes''' on the "Do you really want to?" card, but before taking the action.<br/>Since the user has at this point already confirmed that he or she really wants to delete that entry, what else needs to be done before doing the delete? And what would cause the delete to need to be forestalled? If the delete is to be forestalled, the script could call ''cancel()'' and inform the user as to why via ''message()'', and let the user reconsider what to do.<br/>Mainly, help the user reconfirm that the deletion should take place, and if not, call ''cancel()'' and inform the user (via ''message()'').
:; Result: The value of the field. The type of the result depends on the type of the field.


==== Entry Properties ====
:; 2. After deleting the entry (asynchronous): This script will be called after the delete operation is initiated and will continue as that operation takes place.<br/> Anything you want to do whenever an entry is deleted should be done in this script.
: '''Title''' &mdash; entry name
: '''Description''' &mdash; entry description
: '''Favorites''' &mdash; true, if the entry is in Favorites
: '''Deleted''' &mdash; true, if the record is deleted (it is in the Recycle Bin)


=== Object ''DefaultEntry'' ===
=== Opening an Entry View card ===
Template with default values for a new entry
This event starts when the user has selected an entry for view and ends after the entry had been displayed.
==== Phases ====
This event has two phases. In sequence:
:; 1. Before window display (synchronous): This script will be called before the entry is displayed. This provides the opportunity to alter or augment the data before it is seen by the user.
:; 2. After window display (asynchronous): This script will be called after the data display had been initiated, providing the opportunity to perform an action each time an entry had been viewed, such as logging or timestamping.


==== ''DefaultEntry'' Methods ====
=== Adding an entry to Favorites ===
===== set(name, value) =====
This event starts when the user has pressed an empty '''Star''' icon to make the current entry a Favorite and ends once it has been made a Favorite.
: Set the value of the field
==== Phases ====
This event has two phases. In sequence:
:; 1. Before the operation (synchronous): This script is run just before the entry is made a Favorite.<br/>Data validation could be done; perhaps only certain entries are allowed to be Favorites.
:; 2. After the operation (asynchronous): This script is called after the entry has been made a Favorite.<br/>This action could be logged, for instance, or it could trigger an action on some other app.


:; Arguments
=== Removing an entry from Favorites ===
:: '''name''' &mdash; the name of the field
This event starts when the user has pressed a filled '''Star''' icon to remove the current entry from Favorites and ends once it has been removed.
:: '''value''' &mdash; the value of the field
==== Phases ====
This event has two phases. In sequence:
:; 1. Before the operation (synchronous): This script is run just before the entry is removed from Favorites.<br/>Confirmation of the user's intent could go here.
:; 2. After the operation (asynchronous): This script is called after the entry has been removed from Favorites.<br/>This action could be logged, for instance, or it could trigger an action on some other app.


=== Libraries Examples ===
-----
-----
==== Data Validation ====
<br/>
Using scripts, you can check the correctness of input data and deny saving of data that fail the test. For example, perhaps a field integer values ​​are allowed only from 0 to 200.


:; Add a Create trigger: Set Event to '''Creating a new entry''', Phase to '''Before saving the entry'''. It will run synchronously.
== Security ==
:; Add an Update trigger: Set Event to '''Updating an entry''', Phase to '''Before saving the entry'''. It will run synchronously.
Since the scripts have access to more actions than a user does, they require additional permissions.
:'''Trigger script:'''
<source lang="javascript">
var num = entry().field("Number"); // Get value of field Number
if (num < 0 || num > 200) {        // Test for value matching allowable range
message("Wrong range");          // If value is outside range, display message
cancel();                        // Cancel the operation
}
</source>


:; Set default values: If default values cannot be set using the user interface, they can be set using a script.
The user must define these permissions manually for each library.


-----
To open a card to set permissions for scripts, open the library triggers list and click the '''Shield''' icon on the toolbar. Permissions must be set separately on each device. Permissions are not synchronized between devices.
===== Previous value of another field =====
Suppose there is a library containing daily mileage of daily walks or use of a car or bicycle. Suppose the library has a StartingMileage field and a Mileage field. When an entry is created, the field StartMileage must get data from the field Mileage in the previous entry.


:'''Trigger script:'''
=== Permissions for scripts ===
<source lang="javascript">
:; Library permission
var entries = lib().entries();                  // Get current library & array of its entries
:: determines which other libraries can be affected by the script. You can grant access to all libraries or select only certain libraries. This authorization is required for the ''libByName()'' function.
if (entries.length > 0) {                        // Check that array is not empty;
                                                //  otherwise exit,
                                                //  since there is no previous entry.
prevMileage = entries[0].field("Mileage");      // The array is sorted newest to oldest,
                                                //  so newest entry in the array
                                                //  on top with index of 0.
entryDefault().set("StartMileage", prevMileage); // Set value of field Mileage
                                                //  from the previous entry
                                                //  as default value for field StartMileage.
}
</source>
 
The script must be set for Event '''Creating an entry''' Phase '''Open the Entry Edit card'''. It will run synchronously.
 
-----
===== Beginning of the next day =====
Suppose you need to identify the beginning of a new day in the DateTime field. (The script requires connection of the JavaScript library '''moment.js''' [http://momentjs.com/ moment.js].)


:'''Trigger script:'''
:; Read permission
<source lang="javascript">
:: grants the script read access to a file
var m = moment().add(1,'d'); // Using the moment.js library function moment(),
                            //  get the current time, and add 1 day
m.hour(8).minute(0);        // Set the time to hour 8 and minute 0
entryDefault().set("Date",
    m.toDate().getTime());  // Use that as the default value for the field Date
</source>


The script must be set for Event '''Creating an entry''' and Phase '''Opening an Entry Edit card'''. It will run synchronously.
:; Write permission
:: grants the script write access to a file


:; Network
:: grants to the script the right to execute HTTP requests
-----
-----
===== New entry in one library triggers new entry in another =====
<br/>
Suppose that after a new application for membership is validated, a new member should be created. We have libraries Applications & Members. After a new Applications entry is entered and before it is saved, we want to validate the application, and if it passes, we want to create a new entry in Members.


:; Fields: Library Applications has fields '''Date''', '''Name''', City, Years in city, Type.<br/> Library Members has fields '''Name''' & Type.
== Creating a trigger ==
:; Set library permission: The Applications library must be permitted to access other libraries via libByName().<br/>Within Triggers, press the shield icon to view Permission settings.
Each library can have a number of triggers; multiple triggers may exist for each Event type and Phase. To see the list of triggers, open the library, open the menu, and then select ''Triggers''.
:; Add new trigger: Add a new trigger to the Applications library.<br/>Set Event '''Creating a new entry''', Phase '''Before saving the entry'''.<br/>(A similar trigger could be set for Event '''Updating an entry''' Phase '''Before saving the entry''', but only if a change in a field value is to trigger the add of the new member.)


:'''Trigger script:'''
To create a trigger, press the 3-dot icon in the upper-right corner of the screen to open the Action Menu; then press Triggers to open the list of existing triggers; then click <big>'''+'''</big>. You must then identify the Event type & Phase and write a trigger script that performs the necessary actions.
<source lang="javascript">
// Get the application entry that's being saved and call it e
//    (because you may use it a lot)
// With that, you can readily reference fields
//    in the new application entry about to be saved
var e = entry();
 
// If you have any checks on the values of fields the user entered,
//    you could do them here
// If any validations fail, call cancel() to forestall the save.
// The user remains in the Entry Edit card
//    and will be advised the application does not pass criteria
if (e.field("Years in city") >== 3) {  // Sample field validation
    message("Application inadequate"); // You'll want more
    cancel(); // This will abort the save, but continue the script
} else {      // Don't add a new member if application is bad
 
// From here on, the new application will be saved,
//    so we must also create the new member entry.
 
// To create the new member, we need to reference Members
    var members = libByName("Members");
 
// Start a new entry for Members
    var newMember = new Object();
 
// Set member fields from application data
    newMember["Name"] = e.field("Name");
    newMember["Type"] = e.field("Type");
    members.create(newMember);
}
</source>
 
== 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 are performed by the ''File'' object, which is obtained via a global function called ''file()''.
 
To work with the files, the library should have read/write file access.
 
=== ''File'' Global Functions ===
==== file(name) ====
Open a file for read or write operations. If the file with the specified name does not exist yet, it will be created.
 
:; Argument
:: '''name''' &mdash; The name and the full path to the file. For example, if the file is located on the SD card, the path should be something like /sdcard/example.txt.
 
:; Result
:: [[#Object File|'''''File''''' object]]
 
=== Object ''File'' ===
This object is returned by the global function ''file()'' and provides access to the requested file. After reading or writing, the file should be closed using the method ''close()''.
 
==== ''File'' Methods ====
; readAll()
: Reads all lines of the file, and then closes the file
:; Returns
:: Array containing the lines of the file
 
; readLine()
: Reads the next line from the file stream
:; Returns: The line
 
; readLines()
: Reads the remaining lines from the file stream
:; Returns: Array containing the remaining lines of the file
 
; readChar()
: Reads the next character from the file stream
:; Returns: The character
 
; write(text)
: Write string(s). Take a variable number of arguments, converts each argument to a string, and writes that string to the file stream.
 
; writeLine(text)
: Write strings and a newline to the file stream
 
; close()
: Close the file. It can subsequently be reopened.
 
==== Properties ====
:; exists: true &mdash; if and only if the file exists; false otherwise
:; length: The length, in bytes, of the file, or 0L if the file does not exist.
:; getLineNumber: Get the current line number
::; Returns: The line number, or position, in the file
 
=== Files Examples ===
-----
==== Writing & reading from a file ====
 
:<small>'''Trigger script:'''</small>
<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 of the file into the array a
</source>
 
-----
==== Save an entry to a file in XML format ====
The entry includes fields: id , title , date.
 
:<small>'''Trigger script:'''</small>
<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>
 
== Processing an HTTP request ==
Scripts can send HTTP requests to Web services through their APIs.
Processing for HTTP requests allows integration between Memento and the system. All file operations use the Http object, which works through global function ''http()''.
 
HTTP requests must fulfill two requirements:
# Script execution should be asynchronous, because processing of the request on the network can take a long time &mdash; HTTP requests of the last Phase of an Event.
# The library should have the permission '''Network'''.
 
=== Object ''Http'' ===
Interface for processing HTTP requests
 
==== Http Methods ====
===== get(url) =====
: Execute HTTP get request
:; Argument: '''url''' &mdash; HTTP address, starting with http or https
:; Result: '''HttpResult''' &mdash; Object containing the result of the execution of the HTTP request
 
=== Object ''HttpResult'' ===
Result of the execution of the HTTP request
 
==== Properties ====
: '''code''' &mdash; HTTP code of the response, if the request is successful (usually 200).
: '''body''' &mdash; The response in text form
 
=== Http Examples ===
-----
==== Currency Conversion ====
Suppose the library contains two fields: PriceUSD and PriceEUR.
The user will enter the value in PriceUSD and the value in Euros will appear in PriceEUR according to the current exchange rate.
 
We will create a Trigger on Event '''Create an entry to Add''' and Phase '''After saving the entry'''.
 
:<small>'''Trigger script:'''</small>
<source lang="javascript">
result = http().get("http://api.fixer.io/latest?base=USD"); // Use http://fixer.io/ for
                                                            // conversion rate in JSON
usdToEur = JSON.parse(result.body)["rates"]["Eur"];        // Use JavaScript object JSON
                                                            // to parse the result
entry().set("PriceEUR",
    entry().field( "PriceUSD") * usdToEur );                // PriceUSD * conversion rate
                                                            // for value for PriceEUR
</source>
 
-----
==== Creating a Task in the Todoist App ====
[https://todoist.com Todoist] — A Web service and mobile app for task management. Todoist allows task creation via API [https://developer.todoist.com/].
In the following example of task creation, text will be taken from the Memento library entry.
 
:<small>'''Trigger script:'''</small>
<source lang="javascript">
// Create a JSON command for Todoist task creation
//    using the format described in [https://develop.todoist.com/#add-an-item].
// This command should include a unique identifier
//    created using the guid() global function.
var commands =
    '[{"uuid":"' + guid() + '","temp_id":"' + guid() +
    '","type":"item_add","args":{"content":"' + entry().field("Task") + '"}}]';
 
// Execute the HTTP request. An attribute called '''token''' is used
//    for authorization in Todoist.
// It is available in the Todoist Account setting.
// Since the text of the task can include symbols
//    not allowed in a URL request,
//    use the standard function encodeURIComponent() to filter them out.
result = http().get("https://todoist.com/API/v7/sync?token=15281e8e4d499dаff817af0b14112eac3176f9dc&commands=" +
    encodeURIComponent(commands));
 
// Show the user a message indicating successful creation of the task.
if (result.code == 200)
    message('Task has been successfully created");
</source>
 
== Interaction with the System ==
=== ''System'' Global Functions ===
==== message(text) ====
: Shows the user a brief notification
:; Argument: '''text''' &mdash; Text of the notification
 
==== cancel() ====
: Stop the system operation that caused the event. Many triggers can be a result of an entry manipulation (create, update, delete, etc). The ''cancel()'' function can be used during the phases that precede the system operation. For example, this function can be used during data validation before the entry is saved.
 
==== system() ====
: Obtain information about the system
:; Result: [[#Object System|'''''System''''' object]]
 
==== log(text) ====
: Write a line to the log file
:; Argument: '''text''' &mdash; text to be written to the log
 
==== guid() ====
: Generates random text identifier
:; Result: Random string identifier
 
==== intent(action) ====
: Create an information exchange object &mdash; Intent. This function can send a request for action to another application.
: This function is available only on Android.
:; Argument: '''action''' &mdash; Line that defines standard action (eg, view, pick)
:; Result: [[#Object Intent|'''''Intent''''' object]] &mdash; Information exchange object
: After the object is received, the data will be added to it, and then sent via ''send()''.
: Android has many built-in actions. A list of these actions can be found [https://developer.android.com/reference/android/content/Intent.html '''here'''].


=== Object ''System'' ===
=== Writing a Trigger Script ===
This object contains information about the system.
Trigger scripts are in the JavaScript language. See '''''[[#JavaScript links|Links to JavaScript documentation]]''''' below.


==== Properties ====
; Event & Phase: Make sure to understand the Event & Phase you are scripting. This will dictate a number of things, such as whether data is present already or not, whether ''cancel()'' makes sense or not, whether the user is waiting for script execution or not, and so on.
: '''os''' &mdash; Name of the operating system executing the script


=== Object ''Intent'' ===
; Globals get you started: Note the global functions in the sections below; they generally get you started by providing needed information.
Information exchange object. This object is created by using the global function ''intent()''.


==== ''Intent'' Methods ====
; Inform the user: Keep the user informed. For instance, if a script is running in a synchronous phase, then a call to ''cancel()'' may make sense, but the user won't know what happened and what to do next unless you provide that information, probably via ''message()''.
===== data(uri) =====
: Define URI to reference the data
:; Argument
:: uri &mdash; URI referencing data to which the action will be applied. It can be contact ID, path to the file, phone number, etc.


===== mimeType(mime) =====
; Debug your script: Of course, you can put calls to ''message("Your message")'' to help to test your script. To further assist, the ''log("Your message")'' global function can be used to send messages to a log without bothering the user about it. By default, the log is directed to the Android developer console; to have it directed instead to a file, go to Memento Settings under Debug and turn on Triggers Logs. You can set the location for the file, but by default, it will be in ''memento/logs''. Both messages from calls to ''log()'' and system log messages, including JavaScript exceptions, go into the log.
: Define MIME type of the data
:; Argument
:: mime &mdash; MIME type of the data on which the operation will be performed


===== extra(key, value) =====
; Permissions: Certain functions require special permissions; read '''''[[#Security|above]]''''' about that. One such function is ''libByName()''. Others include the file access and HTTP functions.
: Define additional data as key-value pairs, as necessary for execution of the required action. Similar to how URI data can be required for certain actions, other actions may require extra data in this format.
:; Arguments
:: key and value
 
===== extraLong(key, value) =====
: Define additional data as key-value pairs, where data type needs to be Long
:; Arguments
:: key and value
 
=====send()=====
: Send a message
 
=== System Examples ===
-----
==== Script to open a screen for dialing a number ====
Suppose a library contains a field called Phone containing a phone number.
 
:<small>'''Trigger script:'''</small>
<source lang="javascript">
i = intent("android.intent.action.DIAL"); // Create information exchange object Intent
                                          // with the action of DIAL
i.data("tel:"+entry().field("Phone"));    // The data will be the phone number obtained
                                          // from the field Phone
i.send();                                // Send the message
</source>
 
-----
==== Script to open app to send SMS message ====
The phone number will be obtained from the field Phone and the text of the message will be obtained from the fields ContactName and Notes.


:<small>'''Trigger script:'''</small>
; No return: As of release 4.0.0 of the mobile edition, the script is executed as a top-level script and not as a called function; therefore, for instance, the ''return'' statement is not appropriate in a trigger script.
<source lang="javascript">
msg = "Dear, " +
    entry().field("ContactName") +
    "\n" + entry().field("Notes");          // Form the message from ContactName & Notes
i = intent("android.intent.action.SENDTO"); // Create intent object to open the app for sending
i.data("smsto:"+entry().field("Phone"));    // Provide the phone number in format smsto:number
i.extra("sms_body" , msg);                  // Insert the text of the message to sms_body
i.send();                                  // Send the message
</source>


==== Things to know while writing a trigger script ====
:; No implicit context: There is no implicit context for the trigger script, as there is, for instance, in a JavaScript field. Instead, there are global functions, such as lib() and entry() that must be used to set up context for the script.
:; Entry objects are clones: The Entry object associated with the Event (the one that entry() gives you) is a clone of the actual entry object. If changes are made to this object, they will be saved if the script returns normally. However, if the script calls cancel(), this clone will be discarded upon return from the script, along with any changes that have been made.
-----
-----
==== Script to insert an appointment into Google Calendar ====
<br/>
Suppose a library contains the time and name of an appointment.


:<small>'''Trigger script:'''</small>
== See Also ==
<source lang="javascript">
; [[Memento JavaScript Library]]: Memento JavaScript functions & objects<br/>
i = intent("android.intent.action.INSERT");       // Create Intent object
; [[Trigger Examples]]: Examples of trigger scripts for common needs
i.data("content://com.android.calendar/events");  // Data contains Google Calendar URI
; [[How:Write scripts in JavaScript for Memento]]: Guidelines for writing JavaScript scripts for Memento
i.extra("title", entry().field("Title"));        // Get event name from field Title
i.extra("description",
    entry().field("Description"));                // Get description from field Description
i.extraLong("beginTime",
    entry().field("Begin").getTime());            // Get start time from field Begin
                                                  // Begin is of type DateTime
                                                  // Additional parameter is of type Long,
                                                  // so extraLong() is used for conversion.
i.extraLong("endTime",
    entry().field("End").getTime());             // Get end time from the field End
                                                  // Requires same conversion as above
i.send();                                        // Send the message
</source>


== JavaScript links ==
[[Category:en]] [[Category:Spec]] [[Category:Sc]]
{{Template:JavaScriptLinks}}

Latest revision as of 08:26, 23 August 2023

« Page as of 2023-08-12, editions Mobile 5.0, Desktop 2.0 »

Wiki
Home
About
Wiki | Memento
Getting started
Wiki | Memento
Indexes
Mobile | Desktop | Scripting
Support
Wiki | Memento
Triggers

A Trigger is a script that defines the processing of an entry whenever a particular kind of event takes place. Trigger scripts are written in JavaScript. When a trigger script is executed, it may perform actions like changing an existing entry, creating a new entry, executing an HTTP request, creating a file, performing data validation, etc.


Definitions

We define the following terms:

Event type
One of the following:
  • Opening a library
  • Creating an entry
  • Updating an entry
  • Updating a field
  • Deleting an entry
  • Linking an entry
  • Unlinking an entry
  • Opening an Entry Edit card
  • Adding an entry to Favorites
  • Removing an entry from Favorites
Phase of an Event
One of a predefined set of moments during entry processing for each Event type during which the user can intervene via a trigger script. See the table of events and phases below.
The Event type and the Phase determine the trigger script(s) to be run.
Trigger or Trigger Script
A script that may be defined to run when an event occurs for an entry
The trigger (Event type & Phase) and the corresponding trigger script are one-to-one.
When referring specifically to the script, it is called the trigger script. When referring to the Event type & Phase and its listing in the trigger list, it is referred to merely as a trigger.

Mode of script execution

The phase in which the trigger is activated defines its mode of execution — synchronous or asynchronous.

Synchronous script execution mode
Memento suspends user interaction and then executes the script. In the case of Before... phases, there is an impending operation that will take place unless the script forestalls it by calling cancel().
Time-consuming operations are not recommended in this mode.
Asynchronous script execution mode
The script runs in the background; user interaction is not suspended. Usually, asynchronous scripts are used in the last phases of the action, after any save or other operation is initiated.

Scripting for creating and for updating entries

If you want the same logic to be used in both your Creating scripts and your Updating scripts, it is tempting to just copy the code for one and paste it into the other, and that can be done in in most cases. If you write your Updating script first, the copy will work. If you write your Creating script first, then you may still be able to copy, but if you needed for the Creating Script to be executed Before saving, you'll need to change defaultEntry() to use entry() instead.

Events & Phases

These are the defined Event types, Phases, and their corresponding modes of execution.

THE DEFINED EVENT TYPES & THEIR PHASES
Event type Phase Execution mode
Opening a library Opening the library synchronous
Creating an entry Opening an Entry Edit card synchronous
Before saving the entry synchronous
After saving the entry asynchronous
Updating an entry Opening an Entry Edit card synchronous
Before saving the entry synchronous
After saving the entry asynchronous
Linking an entry Before saving the entry synchronous
After saving the entry asynchronous
Unlinking an entry Before saving the entry synchronous
After saving the entry asynchronous
Deleting an entry Before deleting the entry synchronous
After deleting the entry asynchronous
Opening an Entry View card Before window display synchronous
After window display asynchronous
Adding an entry to Favorites Before the operation synchronous
After the operation asynchronous
Removing an entry from Favorites Before the operation synchronous
After the operation asynchronous

Opening a library

This event occurs when a library is opened before any activity is performed on the library. This event has only one phase, and it is synchronous. Therefore, the script will fully execute before any other activity takes place in the library.

Creating an entry

This event starts when a user has requested that a new entry be added to the library, normally by pressing the + button on the Entries List screen. It ends after the entry is saved to storage — or else not, depending on the processing of the entry.

Phases

This event has three phases. In sequence:

1. Opening an Entry Edit card (synchronous)
The script will be called once the Entry Edit card is open and before any data is displayed.
Initial field values (defaults) may be set.
2. Before saving the entry (synchronous)
The script will be called after the user has pressed the Checkmark icon and before the entry has been saved to storage.
The entered data may be validated; if validation fails, the save may be forestalled and control sent back to the Entry Edit card so the user may fix the errors.
Once the data is validated, this phase is an opportunity for many other functions to be performed to get data to add to the entry to be saved.
3. After saving the entry (asynchronous)
The script will be called once the save of the entry has been initiated and will continue in parallel with final processing of the new entry.
If you want to do something whenever a new entry is added, but not before the data is saved, this is the place to do it.

Scripting considerations

=> Say what here?

Creating an entry

Creating scripts are different from other trigger scripts in that, since the new entry is being crafted, it is not yet stored within Memento — the Entry object does not yet exist — so it needs to be handled differently.

Use the method defaultEntry(), rather than entry(), to get the DefaultEntry object (the unstored template, if you will, for the eventual Entry object).

Once the default entry is saved, it can be referred to by calling entry() to get the saved Entry object. So Before saving scripts use defaultEntry(), and After saving scripts use entry().

Updating an entry

This event starts when a user has requested that an existing entry be edited (updated) within the library, normally by pressing the Pencil button on the Entry View card. It ends after the entry is resaved to storage — or else not, depending on the processing of the entry.

Phases

This event has three phases. In sequence:

1. Opening an Entry Edit card (synchronous)
The script will be called once the Entry Edit card is open and before any data is displayed.
2. Before saving the entry (synchronous)
The script will be called after the user has pressed the Checkmark icon and before the entry has been saved to storage.
The entered data may be validated; if validation fails, the save may be forestalled and control sent back to the Entry Edit card so the user may fix the errors.
Once the data is validated, this phase is an opportunity for many other functions to be performed to get data to add to the entry to be saved.
The script could be used to update some entry data based on other entry data, to keep it all coordinated.
3. After saving the entry (asynchronous)
The script will be called once the save of the entry has been initiated and will continue in parallel with final processing of the new entry.
If you want to do something whenever an existing entry is updated, but not before the data is saved, this is the place to do it.

The following 2 sections are under development.

Updating a field

TBD

Linking an entry

This event starts when a user has added a linked entry to a Link to Entry field either by selecting an existing entry from the entries list for the linked library or by pressing + (Plus) to create a new entry in the linked library. It ends after the entry is resaved to storage — or else not, depending on the processing of the entry.

Phases

This event has two phases. In sequence:

1. Before saving the entry (synchronous)
The script will be called before the entry has been saved to storage after the user has either selected an entry from the linked library or pressed the Checkmark icon after having added a new entry to the linked library from within the Link to Entry field.
The entered data may be validated; if validation fails, the save may be forestalled and control sent back to the Entry Edit card so the user may fix the errors.
Once the data is validated, this phase is an opportunity for many other functions to be performed to get data to add to the entry to be saved.
The script could be used to update some entry data based on other entry data, to keep it all coordinated.
2. After saving the entry (asynchronous)
The script will be called once the save of the entry has been initiated and will continue in parallel with final processing of the new entry.
If you want to do something whenever an existing entry is updated, but not before the data is saved, this is the place to do it.

The script of this event type has access to additional functions:

masterLib()
Returns the library from which the current record was referenced.
masterEntry()
Returns the entry from which the current record was referenced.
attr(name)
Get the value of the attribute for the current reference.
setAttr(name , value)
Set the value of the attribute for the current reference.

Unlinking an entry

This event starts when a user has requested that an existing entry be edited (updated) within the library, normally by pressing the Pencil button on the Entry View card. It ends after the entry is resaved to storage — or else not, depending on the processing of the entry.

Phases

This event has two phases. In sequence:

1. Before saving the entry (synchronous)
The script will be called after the user has pressed the Checkmark icon and before the entry has been saved to storage.
The entered data may be validated; if validation fails, the save may be forestalled and control sent back to the Entry Edit card so the user may fix the errors.
Once the data is validated, this phase is an opportunity for many other functions to be performed to get data to add to the entry to be saved.
The script could be used to update some entry data based on other entry data, to keep it all coordinated.
2. After saving the entry (asynchronous)
The script will be called once the save of the entry has been initiated and will continue in parallel with final processing of the new entry.
If you want to do something whenever an existing entry is updated, but not before the data is saved, this is the place to do it.

Deleting an entry

This event starts when a user has requested that an existing entry be deleted (actually, moved to the library's Recycle Bin) within the library, normally by pressing the Trash Can button on the Entry View card. It ends after the entry is moved to the Recycle Bin — or else not, depending on the processing of the entry.

Phases

This event has two phases. In sequence:

1. Before deleting the entry (synchronous)
This script will be called after the user has requested the deletion of the entry and pressed Yes on the "Do you really want to?" card, but before taking the action.
Since the user has at this point already confirmed that he or she really wants to delete that entry, what else needs to be done before doing the delete? And what would cause the delete to need to be forestalled? If the delete is to be forestalled, the script could call cancel() and inform the user as to why via message(), and let the user reconsider what to do.
Mainly, help the user reconfirm that the deletion should take place, and if not, call cancel() and inform the user (via message()).
2. After deleting the entry (asynchronous)
This script will be called after the delete operation is initiated and will continue as that operation takes place.
Anything you want to do whenever an entry is deleted should be done in this script.

Opening an Entry View card

This event starts when the user has selected an entry for view and ends after the entry had been displayed.

Phases

This event has two phases. In sequence:

1. Before window display (synchronous)
This script will be called before the entry is displayed. This provides the opportunity to alter or augment the data before it is seen by the user.
2. After window display (asynchronous)
This script will be called after the data display had been initiated, providing the opportunity to perform an action each time an entry had been viewed, such as logging or timestamping.

Adding an entry to Favorites

This event starts when the user has pressed an empty Star icon to make the current entry a Favorite and ends once it has been made a Favorite.

Phases

This event has two phases. In sequence:

1. Before the operation (synchronous)
This script is run just before the entry is made a Favorite.
Data validation could be done; perhaps only certain entries are allowed to be Favorites.
2. After the operation (asynchronous)
This script is called after the entry has been made a Favorite.
This action could be logged, for instance, or it could trigger an action on some other app.

Removing an entry from Favorites

This event starts when the user has pressed a filled Star icon to remove the current entry from Favorites and ends once it has been removed.

Phases

This event has two phases. In sequence:

1. Before the operation (synchronous)
This script is run just before the entry is removed from Favorites.
Confirmation of the user's intent could go here.
2. After the operation (asynchronous)
This script is called after the entry has been removed from Favorites.
This action could be logged, for instance, or it could trigger an action on some other app.


Security

Since the scripts have access to more actions than a user does, they require additional permissions.

The user must define these permissions manually for each library.

To open a card to set permissions for scripts, open the library triggers list and click the Shield icon on the toolbar. Permissions must be set separately on each device. Permissions are not synchronized between devices.

Permissions for scripts

Library permission
determines which other libraries can be affected by the script. You can grant access to all libraries or select only certain libraries. This authorization is required for the libByName() function.
Read permission
grants the script read access to a file
Write permission
grants the script write access to a file
Network
grants to the script the right to execute HTTP requests


Creating a trigger

Each library can have a number of triggers; multiple triggers may exist for each Event type and Phase. To see the list of triggers, open the library, open the menu, and then select Triggers.

To create a trigger, press the 3-dot icon in the upper-right corner of the screen to open the Action Menu; then press Triggers to open the list of existing triggers; then click +. You must then identify the Event type & Phase and write a trigger script that performs the necessary actions.

Writing a Trigger Script

Trigger scripts are in the JavaScript language. See Links to JavaScript documentation below.

Event & Phase
Make sure to understand the Event & Phase you are scripting. This will dictate a number of things, such as whether data is present already or not, whether cancel() makes sense or not, whether the user is waiting for script execution or not, and so on.
Globals get you started
Note the global functions in the sections below; they generally get you started by providing needed information.
Inform the user
Keep the user informed. For instance, if a script is running in a synchronous phase, then a call to cancel() may make sense, but the user won't know what happened and what to do next unless you provide that information, probably via message().
Debug your script
Of course, you can put calls to message("Your message") to help to test your script. To further assist, the log("Your message") global function can be used to send messages to a log without bothering the user about it. By default, the log is directed to the Android developer console; to have it directed instead to a file, go to Memento Settings under Debug and turn on Triggers Logs. You can set the location for the file, but by default, it will be in memento/logs. Both messages from calls to log() and system log messages, including JavaScript exceptions, go into the log.
Permissions
Certain functions require special permissions; read above about that. One such function is libByName(). Others include the file access and HTTP functions.
No return
As of release 4.0.0 of the mobile edition, the script is executed as a top-level script and not as a called function; therefore, for instance, the return statement is not appropriate in a trigger script.

Things to know while writing a trigger script

No implicit context
There is no implicit context for the trigger script, as there is, for instance, in a JavaScript field. Instead, there are global functions, such as lib() and entry() that must be used to set up context for the script.
Entry objects are clones
The Entry object associated with the Event (the one that entry() gives you) is a clone of the actual entry object. If changes are made to this object, they will be saved if the script returns normally. However, if the script calls cancel(), this clone will be discarded upon return from the script, along with any changes that have been made.


See Also

Memento JavaScript Library
Memento JavaScript functions & objects
Trigger Examples
Examples of trigger scripts for common needs
How:Write scripts in JavaScript for Memento
Guidelines for writing JavaScript scripts for Memento