LUA: scripting !bangs Thread last updated on 2006-08-31 00:02:29

Posted by member 7223 on 2006-08-21 00:04:25

SCRIPTING BANGS

© 2006 Smurth ( smurth@free.fr )



Here is some usefull !bangs to use with LSLua.
No litestep module; no lua module needed; nothing but Lua.

Download the package here: http://www.litestep.net/download.php?mod=138,module



With !TextAppend, !TextReplace and !VarSave, every can be: "arg" , 'arg' or [arg]


!Exec !bang
or
!Exec [!bang1][!bang2]...

Examples:
!exec !alert %#resolutionx*10%#
!exec [!setevar newevar "This evar exist, now"][!alert "%#newevar%#"][!setevar newevar "The same evar :)"][!alert "%#newevar%#"]



!if [condition] [!bangIfTrue] [!bangIfFalse]
must be written in Lua's form: "==" for "equal" , "~=" for "different" , etc.
Example: !if ["%#resolutionx*10%#" < "8000"] [!alert "too small"]



!TextAppend



!TextReplace
use the Lua's pattern mechanism
IS CASE SENSITIVE
You can do some "TextDelete" with an empty replacement string



!VarSave []
IS NOT case sensitive
is optionnal. If ommited, the evar's value is taken

Note: if is a string, it will be surrounded by quotes ( " ) ; if it already contains quotes, it'll be surrounded by single quotes ( ' )

Then,
!varsave "$themevarfile$" HelloText 12
will write:
HelloText 12

!varsave "$themevarfile$" HelloText "Hello world"
will write:
HelloText "Hello world"

!varsave [$themevarfile$] HelloText [Hello world]
will write:
HelloText "Hello world"

!varsave [$themevarfile$] HelloText [!alert "Hello world"]
will write:
HelloText '!alert "Hello world"'

But,
!varsave [$themevarfile$] HelloText [!alert "Hello world's..."]
will write,
HelloText '!alert "Hello world's..."'


!ThemeSelect "path"
SHOULD be surrounded by quotes ( " )
Example:
*popup "Themes" !PopupActionFolder:!ThemeSelect:"$ThemesDir$*\"
*popup "Schemes" !PopupActionFolder:!ThemeSelect:"$GfxCommonDir$*\"

Notes:
- writes the evar in "$ProfileDir$themeselect.rc"
- can be use to switch schemes, too (hum, you'll need to modify the code to make it works with YOUR scheme's system)



Other Lua's functions:

string.nocase( string )
Returns a case-insensitive string to use as a pattern
Example:
string.nocase( "--aBc123" )
returns "--[aA][bB][cC]123"


string.unpack( string )
Can be used to split multiples arguments stored in one string
Example:
string.unpack( ' "word one" [word two] \'word ...\' ' )
returns unpack( { "word one" "word two" "word ..." } )


parseevar( string )
Just like the !ParseEvars from xLabel but for Lua :)
Can execute Lua's functions, like: parseevar( "resolutionx*cos(sin(0.5))" )
Non existing evars are replaced by "0"
Example:
parseevar( "10*ThisEvarDoesNotExist+20" )
returns "20"


alert( string, title )
or
alert( string )
A simple message_box that can be used for error messages
All messages are prefixed with the current date/time (with millisecondes)
You can replace lslua.message_box() with something like f:write() to use this for logging



Changes:
1.0 (060828): First public release

Posted by member 248213 on 2006-08-21 07:51:16 link

!if {"%#resolutionx*10%#" < "8000"}{!alert "too small :)"}

=
!LuaExec if evar.ResolutionX * 10 < 8000 then exec ("!alert 'too small :('") else exec ("!alert 'too big :('") end


:D

Are you going to write a mz wrapper?

Posted by member 7223 on 2006-08-21 08:06:32 link

The aim was to build a parseevar from scratch. Don't ask me why

Posted by member 212670 on 2006-08-21 14:07:48 link

The "why" is the challenge. :)

Posted by member 7223 on 2006-08-25 13:03:23 link

UPDATE: !exec added

Posted by member 7223 on 2006-08-26 00:02:02 link

UPDATE:
- added: !TextAppend, !TextReplace, !SaveEvar, !ThemeSelect
- changed: no more "require"
- changed: !if now use [] instead of {}
- fixed: stuff like !exec [!alert "[one][two]"][!alert [three]] works, now

TODO:
- any idea ?

Posted by member 299815 on 2006-08-26 07:59:26 link

I'm getting an "LSLua Error" dialog box from a direct copy-paste of the above code...
"C:\\script.rc:37: ')' expected near '('
Did I do it wrong? :( I checked it with SciTE, and the syntax is fine... it's just spitting out a bizarre error.

Posted by member 7223 on 2006-08-26 08:43:14 link

hum... too bad, the " \ " (without spce have become "" only
I guess the whole file is corrupt and I have to find a fix

Posted by member 299815 on 2006-08-26 08:44:40 link

so all that's needed is to insert a \ betwixt the quotes? gotcha
edit- I guess I was wrong, heh... calmly awaiting a fix ;)

Posted by member 299815 on 2006-08-26 08:56:48 link

One more point to bring up... While !ThemeSelect really is an ingenious idea, wouldn't this require LUA integration with the base install to really be useful?
-edit not that that would be a bad thing ;)

Posted by member 7223 on 2006-08-26 09:39:05 link

Ok, I've changed " \ " " to ' " '
I hope there won't be some error left

@SiteAdmins: it would be great if everything between [ code ] and [ /code ] was left "a is". I mean no parsing, no space/tab removing, etc
(the code is pretty unreadable without indentation)

Posted by member 299815 on 2006-08-26 09:42:17 link

No errors this time, thanks!

Posted by member 7223 on 2006-08-26 09:49:09 link

@Tim.S: May I call you "FTL" (faster than light) ?

Posted by member 299815 on 2006-08-26 10:20:36 link

heh, if you wish ;) I had the window open already, as I was working with my own theme.

Posted by member 299815 on 2006-08-27 09:25:25 link

Hmm, is sendmessage functional in LSLUA? If so, "we" (as in, anyone who understands lua enough to do it-not me ;)) could write a universal bang system for media player/winamp/itunes/etc....
Example: "!player_next (str)" where str would be MP,itunes, winamp, and so on... universal support for any music player would be a click away. ;)
edit- this could even be upgraded, easily, supporting any player which we could obtain sendmessage data from.
Now, I know it'd have to call a completely different chunk of information from a table of some sort for each player, in order to do this, but it should be possible.
(Actually, something this big would probably be best coded as a seperate lua file)

Posted by member 248213 on 2006-08-27 18:07:11 link

Like a widget perhaps?

The hardest part is knowing what message to send

Posted by member 1949 on 2006-08-27 19:15:53 link

send them all!
One will respond!

Posted by member 1 on 2006-08-27 20:11:23 link

You could always require an evar to be set somewhere, or heaven forbid you could make an assumption based upon $AudioPlayer$

Posted by member 299815 on 2006-08-28 00:55:59 link

Like a widget perhaps?
(pretty much, yeah)
The hardest part is knowing what message to send
(Well, we know the sendmessage commands for winamp, itunes, Windows media player, foobar, and a couple others... the source code of a few modules contains the information needed for all but WMP, but that information is in this very forum.)
send them all!
One will respond!
(That's a monkey-with-a-crowbar approach, but I guess it'd work. Or set off a nuclear explosion in Montana... only time will tell.)
You could always require an evar to be set somewhere, or heaven forbid you could make an assumption based upon $AudioPlayer$
(in order for the script to work, yeah... I'd think either having it as a string [ex.!Player_next WMP/Itunes/Foo/Winamp...] or $AudioPlayer$ could work too, but that would require some type of script to sift through the evars and determine which type of player is being used. Come to think of it, I'd say the latter method would be best. Not to mention idiot-proof; as long as $audioplayer$ was designated correctly[meaning the executable is using it's original name], it wouldn't require any work to universally install it on any theme using LUA. PLUS it'd be extensible, for future upgrades.
That's one issue with the current module system, only a select few have the knowhow [or the software] to make it work properly. With LUA we bring it into reach of anyone with a text editor and a little time.)
come to think of it, both options together would be best. A little more hairy to code, however.

Posted by member 248213 on 2006-08-28 18:42:56 link

Cool, universal media controller for Litestep!

Posted by member 5575 on 2006-08-28 18:43:48 link

All your bangs are belong to us?

Posted by member 299815 on 2006-08-29 00:58:25 link

heck, if someone created a working example, any one of us could copy-paste the needed bangs in...

Posted by member 7223 on 2006-08-29 02:48:03 link

UPDATE:
- parsevar can now execute lua's function like in %#resolutionx*cos(sin(0.5))%#
- arguments of !TextAppend, !TextReplace and !VarSave can be: "arg" , 'arg' or [arg]
- fixed parser's issue

Posted by member 7223 on 2006-08-31 00:02:29 link

UPDATE:

1.0.1 (060831):
- clean-up the code
- fixed some bugs (especially in parseevar)