Doing so is relatively straightforward by adding a menu with two options: the Edit option should run the editor (our function edit()), and Stop should stop the script. With function ui.menu(), it is trivial to install a menu:
|
ui.menu("Service",["Edit","Stop"]) |
adds a menu with title Service and the two options:
![]() | ![]() | |
If the user picks an option, ui.cmd() will return it:
|
print ui.cmd() → Edit
|
But now we have a problem: if the user hasn't picked an option before, ui.cmd() will wait. Likewise, sms.receive() will wait until an SMS arrives. So we have two events to wait for, but we can only wait for one at a given point in our code.
There is a simple solution to this: both sms.receive() and ui.cmd() take a timeout: they do not necessarily wait forever, but optionally only for a certain period. Almost all functions in m which wait for a certain event have such timeouts. The timeout period is always indicated in milliseconds (ms, 1/1000 of a second). If the timeout expires, the functions typically return null.
For instance,
|
sms.receive(1000) |
waits one second for a new message, then simply returns null if no message arrives within this period[2].
With this simple method, we can combine the user interface and the SMS monitoring[3]:
|
ui.menu("Service",["Edit","Stop"]); do id=sms.receive(1000); if id#null then // there is a new message msg=sms.get(id); t=lower(trim(msg["text"])); if db[t]#null then print "Got",t,"from",msg["sender"]; sms.send(msg["sender"], db[t]); sms.delete(id) end end; cmd=ui.cmd(5000); if cmd="Edit" then edit(db) end until cmd="Stop" |
Remarks: