Scripts and Events
Last Reviewed: 2026-10-26
Scripts are small pieces of JavaScript that run when something happens in Secluse: a record is saved, a button is pressed, a schedule comes round.
- Writing scripts: open Scripts
in the sidebar. - Saving: like a record's form, the editor saves when you leave it (the back arrow, Android's back, or closing Secluse). Cancel, next to Edit Script, leaves without saving after asking. A script needs a name before it can be saved.
- Running them: bind a script to an event in Settings → Automation (see Binding a script).
- Scripts and their bindings reach every device.
The language
Scripts are ordinary JavaScript: Math, JSON, Date, string, array and object methods,
regular expressions, if, loops and functions all work as usual. For the language itself, see
MDN's JavaScript reference.
Secluse adds the commands below. There's nothing else: no console.log, no calling other
scripts, and no internet unless you allow it (see Web requests). A script
runs for at most 5 seconds and can't wait for anything (no setTimeout, no promises).
Changes a script makes are saved only if it finishes without an error.
When a script run by an event fails or runs out of time, a message opens naming the script and showing the error. It stays until you close it. You can select the text, or click Copy to put the script's name and the whole error on the clipboard, ready to paste into a note or a report.
What a script can do
// The record the script was started from (a form, a button or a record event);
// null for a scheduled script, a view bar button, or a button with no record.
record.get('Field Name') // read a field
record.set('Field Name', value) // change a field
record.save() // save the changes
record.delete() // delete the record, as the grid's trash can does
record.fields() // the fields to copy into a new record, as an object:
// skips fields with "Carry value forward" turned off; includes id
record.fields({all: true}) // every field
record.nextOccurrence('Start', 'Timeframe', 'When', 'End')
// {start, end} of the next occurrence, or null if there isn't one;
// the End field is optional (see Reminders and Timers);
// skips missed occurrences unless the record's When says not to,
// or you add {skipMissed: false} (or true) as the last argument
// Any table, by the name shown in the sidebar.
table('Table Name').all() // every record, as objects
table('Table Name').find({Status: 'open'}) // records whose fields equal these values
table('Table Name').create({Name: 'x'}) // add a record (saved when the script finishes)
table('Table Name').update(id, {Name: 'y'}) // change these fields of the record with this id,
// leaving the others as they are (saved when the script finishes)
// Only in a view bar button's script (null in every other script).
view.records() // the records the open view shows, in its order, after its Filter Set
notify('message') // a message on screen, or a Windows/Android notification
// when Secluse is in the background
navigate.to('Table Name') // open a table
navigate.toRecord(record) // open a record's form
closeForm() // close the form the script was started from, when it finishes
confirm('Question?') // ask Yes or No: true for Yes; the script waits for the answer
confirm('Question?', {yes: 'Cancel the series', no: 'Just this one'})
// with your own button names
deviceId() // this device's name, e.g. "OFFICE-PC"
localTime() // the local time, e.g. "2:53:03 PM"
localTime.iso() // the same moment as "YYYY-MM-DD HH:MM:SS"
readFileFirstLine('C:\\some\\path.txt') // a file's first line
readFileLines('C:\\some\\path.txt', 5) // its first 5 lines, as an array (0 = all)
writeFile('C:\\some\\path.txt', 'one\ntwo') // replace a file's contents
httpGet('https://example.com/data') // a web page or service's reply, as text
httpGet(url, {headers: {Authorization: 'Token abc'}}) // with request headers
Tables and fields by name
- Tables and fields are named the way you named them: the table's name in the sidebar,
and each field's name on the form. Capitals and spaces around the name don't matter:
record.get('last modified')reads Last Modified. - Records come back as objects with a property per field, named the same way, plus
id, the record's number. For a name with a space in it, use brackets:rows[0]['Last Modified'], orrows[0].Statusfor one without. - Dropdowns read and write the text you see, not a hidden number:
record.get('Status')gives'closed', andrecord.set('Status', 'open')picks open (capitals don't matter). The same goes forfind(),create()andupdate(). A value that isn't one of the dropdown's choices stops the script with "… isn't one of the choices for …". - True/False fields are real booleans:
record.get('Paid')istrueorfalse, soif (record.get('Paid'))works.set(),find(),create()andupdate()taketrue/false(or1/0). - Calculated fields (formulas, values shown from a linked record, totals from child or
linked records) read the same as in the grid, and
find()can match them. A script can't set one: Secluse works it out. Afterrecord.set(), reading a formula gives the new result. - Fields that link to other records (Belongs to, Link to several records) hold the
linked record's
id(or a list of them). - A name that doesn't exist stops the script with "This table has no field called …" (or "No table named … found"). Names are unique within a table and among tables, so a name always means one thing. If two devices create the same name while apart, the script stops with "More than one … is called …" instead of guessing: rename one, and Health warns about it until then.
Example: stamp the time every time a record is saved, from a Record saved binding:
record.set('Last Modified', localTime.iso());
record.save();
Example: change many records at once, from a button or a schedule. update() takes the
id that all() and find() give each record; an id that isn't there (or was deleted)
stops the script with "No record …", and nothing it changed is saved:
var rows = table('Tasks').find({Status: 'open'});
for (var i = 0; i < rows.length; i++) {
table('Tasks').update(rows[i].id, {Status: 'closed'});
}
notify('Closed ' + rows.length + ' task(s)');
Use localTime() for the current time, never new Date() or Date.now(): the script
engine doesn't know your time zone, so they can be hours out. Date is fine for calculations
on a date you already have.
deviceId() lets one script behave differently per device, for example
notify('Ran on ' + deviceId() + ' at ' + localTime()).
Files
The three file commands use exactly the path given, on the device the script runs on, with the same permissions as any other program there.
-
writeFilealways replaces the whole file (it never adds to it). To add a line, read the lines withreadFileLines, add yours, and write them all back joined with\n. -
On success,
writeFilereturns nothing. On any failure (a missing file, no permission, a missing folder…), all three return the error message inside<<and>>, for example<<PathNotFoundException: …>>:var result = writeFile(path, text); if (result) { // failed: result is the error message } else { // succeeded }For the read commands, check
result.startsWith('<<'). An empty file gives''or[], not an error.
Web requests
httpGet asks a web address for its reply, for example a price service for today's stock
prices, and gives the script the reply as text (use JSON.parse on a JSON reply).
- Off until you allow it, on each device: Settings → Automation → Allow scripts to use
the internet on this device. Scripts and their bindings reach every device, but the
switch doesn't, so a script can use the internet only where you've turned it on. With it
off,
httpGetreturns<<Scripts can't use the internet on this device: …>>. - Only where the script says: Secluse requests exactly the address written, with the
headers given, and adds nothing about you or your data. A redirect is followed only to the
same site.
http://andhttps://addresses work; withhttp://, anything in the request (a key, say) crosses the internet unencrypted, so usehttps://when the service has it. - Errors come back as text, like the file commands:
<<HTTP 401 Unauthorized: …>>(the service's own explanation follows the colon),<<Timed out after 20 seconds>>, or the network's error. Checkresult.startsWith('<<'). - Waiting doesn't count toward the 5 seconds: a script waits for each reply as long as it takes, up to the limits below.
- Limits, per run of a script: each request 20 seconds, 25 requests and 60 seconds of waiting in all, and a reply of at most 2 MB.
- A key from a service (most price services give one free) can go in the address or a header, as the service says. It's part of the script, so it reaches every device with it.
- Scheduled scripts can use it too, on devices with the switch on. On Android, a request can fail while the phone is asleep; it comes back as an error like any other.
Example: a button that sets a record's price from a service that answers with JSON:
var reply = httpGet('https://prices.example.com/quote?symbol=' + record.get('Symbol') + '&key=abc');
if (reply.indexOf('<<') === 0) {
notify('No price: ' + reply);
} else {
record.set('Price', JSON.parse(reply).price);
record.save();
}
Binding a script
In Settings → Automation
- Manage events: for one table. Choose the table, then the event; for Field changed also the field, for Button clicked the button field, and for Timer started or Timer stopped the Timer field. For a View bar button, a label and an optional icon (see View bar buttons).
- Scheduled events: not tied to a table.
- When Secluse opens (
app_launch), or - A repeating schedule: every N minutes, hours, days or weeks (at least 5 minutes), optionally from a starting date and time (for example every day starting at 8:00). Without a start, it counts from when the binding was created or last ran.
- When Secluse opens (
- Each scheduled binding has a device checklist: All, or a box per device. A binding with no device ticked does nothing anywhere. This is on purpose: otherwise a script that changes data would run once on every device, instead of once.
- If a schedule falls far behind (the device was off), it doesn't run every missed time: it jumps to the current one and tells you it skipped ahead.
When each event happens
| Event | Happens when | Notes |
|---|---|---|
Form opened (form_opened) |
A record's form opens | Every time, before anything is changed |
Form closed (form_closed) |
The form closes | Saved or not |
Record created (record_created) |
A new record is saved for the first time | Only for new records |
Record updated (record_updated) |
An existing record is saved | From the form or a grid edit; even if nothing changed |
Field changed (field_changed) |
A field's new value is saved | Per field. In the form, only when the value really changed; in the grid, whenever you edit that cell. For a new record, every field that has a value (a default value counts), since empty to a value is a change. With Save as soon as this changes, it happens the moment you pick a value (for a typed date, when you finish typing it) |
Record saved (record_saved) |
Any save | After created or updated: the one event every save has |
Record deleted (record_deleted) |
A record is deleted, from the grid or the form | After the deletion |
Button clicked (button_clicked) |
A button field is pressed | Unsaved changes in the form are saved first, so the script sees them; a new record is created first. If a required field is empty, the script doesn't run |
Timer started (timer_started) |
A Timer field's Start is clicked, in the form or the grid | After the start is saved |
View bar button (view_bar_button) |
Its button in the view bar is pressed | record is null; view.records() gives the records the open view shows |
Timer stopped (timer_stopped) |
A Timer field's Stop is clicked, in the form or the grid | After the stop is saved, so the Timer's total already includes this session. Reset fires neither |
When Secluse opens (app_launch) |
Secluse starts | A scheduled event |
| A repeating schedule | The interval comes round | A scheduled event |
View bar buttons
A View bar button binding puts a button in the table's view bar, after its views and before its Filter Sets, in every view of the table (Grid, List and Kanban). Pressing it runs the script once, for the whole view rather than one record:
view.records()gives the records the open view shows, in its order: after its Filter Set (in the Grid, after its column filters), the rows inside groups included.recordis null.- The button shows a spinner while the script runs, and can't be pressed again until it's done.
- To ask before changing anything, use
confirm()in the script:
const shown = view.records();
if (confirm('Update ' + shown.length + ' records?')) {
for (const r of shown) table('Stocks').update(r.id, {Checked: localTime.iso()});
notify('Updated ' + shown.length);
}
Label and icon (the pencil beside the binding in Manage events) changes the button. Right-click (Windows) or long-press (Android) the button to open Manage events.
A turned-off button stays in the bar, dimmed. Pressing it shows the script it runs, with Turn on and run and Cancel. A template pack's buttons arrive like this, so you read the script once before it runs.
The order, when one save sets off several: saving an existing record gives Record updated, then Field changed for each changed field (in field order), then Record saved. Saving a new record gives Record created, then Field changed for each field that has a value (in field order), then Record saved. A copy counts as a new record, so its filled fields run their Field changed scripts too.
Running in the background
Scheduled scripts and recurring reminders also run when Secluse isn't open. That needs a one-time setup on each device:
- Android: reminders use an exact alarm, to the second. The permission for it is asked for the first time Secluse starts; on most phones it's already allowed and nothing appears. If an Alarms & reminders screen opens, allow it. (Some phones don't show the small alarm icon in the status bar that normally appears; that's only cosmetic.)
- Windows: Settings → Automation → Background processes → Run scheduled events while Secluse is closed. It creates a Windows scheduled task, for your account only, that checks every minute, so a Windows event can be up to about a minute late. If Secluse is moved or updated to another folder, it points the task at the new copy by itself (or shows Fix).
- With a hub: Stay connected to the Hub (on the same screen) keeps the device in sync with Secluse closed, so a reminder set on another device fires on time. On Windows it needs the background task above. See Hub and Sync.
- Alerts: with a hub, the Hub shows a Windows notification when a device's background checks fail twice or more in a row (see the Hub's Alerts card). Without a hub, look at Background processes now and then.
Checking that it works
Settings → Automation → Background processes shows each device's recent background checks: the last attempt, the last success, how many bindings it ran, and any error. 0 bindings applied is normal: nothing was due. Each device shows one of three states:
- Healthy: it ran recently and succeeded.
- Failing: it ran and failed repeatedly, or stopped running while something was due.
- Idle: no scheduled binding targets this device, so there's nothing to check. Its history can look old; that's expected.
On Android, a separate check every 12 hours makes sure the alarm is still set, and sets it again if not.