I have a trigger that matches on a string, and sends the string to a notepad window. Works fine, except I'd also like to append the time of the line that it was trigged on. Would I have to make it a script? What would I use for the time?
Trigger; appending time of line
Posted by Zeno on Wed 15 Mar 2006 12:29 AM — 14 posts, 37,544 views.
A small bit of scripting would to it. This example uses Lua as the script language, but other languages would have similar facilities:
<triggers>
<trigger
enabled="y"
match="^.*$"
regexp="y"
send_to="12"
sequence="100"
>
<send>AppendToNotepad ("mylog", "%0 " .. os.date ("%H:%M") .. "\\r\\n")
</send>
</trigger>
</triggers>
Hm, I'm getting an error. I match on this:
(With no regexp)
It sends:
I get the error "Expected ')'".
Using commas, I get:
* says (*) '*'(With no regexp)
It sends:
AppendToNotepad ("AllSays", "%0 %1 said in room %2: %3" os.date ("%H:%M") "\r\n")I get the error "Expected ')'".
Using commas, I get:
Cannot use parentheses when calling a Sub
This is VBscript, right? - judging by the error message.
In my Lua version I used ".." to concatenate strings. In VBscript you need to use "&" to do that. Also, in VBscript the date function isn't os.date - that is Lua.
You need to look in the VBscript manual for the function that converts the current time to printable. A possibility is simply:
Now ()
In my Lua version I used ".." to concatenate strings. In VBscript you need to use "&" to do that. Also, in VBscript the date function isn't os.date - that is Lua.
You need to look in the VBscript manual for the function that converts the current time to printable. A possibility is simply:
Now ()
Whoops, forgot to change to Lua. If I try to just run this:
I get this error:
AppendToNotepad ("mylog", "test " .. os.date ("%H:%M") .. "\r\n")I get this error:
Error number: 0
Event: Run-time error
Description: [string "Command line"]:1: attempt to index global `os' (a nil value)
stack traceback:
[string "Command line"]:1: in main chunk
Called by: Immediate execution
Later versions of MUSHclient have changed the default "Lua sandbox" in global preferences. Change the line:
to:
os = nil
to:
do
local a, b, c, d, e = os.date, os.time,
os.setlocale, os.clock, os.difftime
os = {} -- make new table
os.date, os.time, os.setlocale,
os.clock, os.difftime = a, b, c, d, e -- restore
end
You probably sand-boxed your scripts to remove the os table. I think this is the default, actually.
The sandboxing is in either global preferences or in the world preferences. You'll see something like:
os = nil
What you want to do is something like:
local os = os
os.date = _G.os.date
_G.os = nil
That should make your script work. If it doesn't, try:
local os_date = os.date
os = nil
Then, change your script to call os_date, not os.date.
(The first solution is somewhat more elegant, though, since you keep the os table, you just remove the stuff you don't like.)
EDIT: Or.... do what Nick said, posted while I was writing up mine. :)
The sandboxing is in either global preferences or in the world preferences. You'll see something like:
os = nil
What you want to do is something like:
local os = os
os.date = _G.os.date
_G.os = nil
That should make your script work. If it doesn't, try:
local os_date = os.date
os = nil
Then, change your script to call os_date, not os.date.
(The first solution is somewhat more elegant, though, since you keep the os table, you just remove the stuff you don't like.)
EDIT: Or.... do what Nick said, posted while I was writing up mine. :)
I did what Nick said:
Still get an error:
Or do I have to restart MC?
[EDIT] Ah, I had to restart MC. *grunt* Had it open for 7 days. =P
I'm looking for docs on this date function, since I'd like to use month and day as well.
loadlib = nil -- no loading DLLs
io = nil -- no disk IO
do
local a, b, c, d, e = os.date, os.time,
os.setlocale, os.clock, os.difftime
os = {} -- make new table
os.date, os.time, os.setlocale,
os.clock, os.difftime = a, b, c, d, e -- restore
endStill get an error:
[string "Command line"]:1: attempt to index global `os' (a nil value)
stack traceback:
[string "Command line"]:1: in main chunkOr do I have to restart MC?
[EDIT] Ah, I had to restart MC. *grunt* Had it open for 7 days. =P
I'm looking for docs on this date function, since I'd like to use month and day as well.
Here you go:
http://www.lua.org/manual/5.0/manual.html#5.7
All the documentation for the os.xyz functions.
http://www.lua.org/manual/5.0/manual.html#5.7
All the documentation for the os.xyz functions.
Thanks, although it's odd. Doesn't mention anything about %H or related things.
In that case it probably uses the standard formats, like in strftime or whatever the C function is called. In fact:
Quote:
If format is not *t, then date returns the date as a string, formatted according to the same rules as the C function strftime.
If format is not *t, then date returns the date as a string, formatted according to the same rules as the C function strftime.
Thanks, now I got it.
AppendToNotepad ("AllSays", os.date ("%a %b %d %H:%M:%S %Y") .. " :: %1 said in room %2: %3\r\n")
Date formats are documented in the help file under the Lua "os" functions section, which is exactly the same as this page:
http://www.gammon.com.au/scripts/doc.php?general=lua_os
Must be very reliable then *wink*.
As a matter of fact, reloading scripting would have done it. The sandbox is processed every time the script engine is instantiated.
http://www.gammon.com.au/scripts/doc.php?general=lua_os
Quote:
Ah, I had to restart MC. *grunt* Had it open for 7 days.
Ah, I had to restart MC. *grunt* Had it open for 7 days.
Must be very reliable then *wink*.
As a matter of fact, reloading scripting would have done it. The sandbox is processed every time the script engine is instantiated.
Ah, I glanced over your Lua docs but didn't see it.
Indeed. It takes a few minutes to open World Properties (300k lines+), but it rarely crashes. Actually I'm surprised Windows has stayed stable that long. =P
Quote:
Must be very reliable then *wink*.
Must be very reliable then *wink*.
Indeed. It takes a few minutes to open World Properties (300k lines+), but it rarely crashes. Actually I'm surprised Windows has stayed stable that long. =P