JSONedit: Lua

JSONedit has built-in Lua script engine, working in similar way as in tSIP softphone.

Lua is extended with following commands:

display standard modal Win32 message with specified text and "OK" button
direct equivalent of VCL function with the same name, displays modal dialog allowing to take text input from the user
  local caption = "Some dialog caption"
  local prompt = "Some dialog prompt"
  local defaultText = "Default text"
  local text, isAccepted = InputQuery(caption, prompt, defaultText)
  if isAccepted then
  	ShowMessage("Dialog accepted, text = " .. text)
  	ShowMessage("Dialog was not accepted")
pause execution for specified time in miliseconds; not recommended as UI is blocked but can be used in combination with Beep() to create audible feedback
text = GetClipboardText()
text from system clipboard
set text in system clipboard
ret = ForceDirectories(dir)
Create directory structure if it does not exist; returns 0 on success
srcType, srcTypeIsSet = GetExecSourceType()
check what type of event was caused script execution; for script assigned to button srcType would be equal to 0, see ScriptExec.h for full list of event types
srcId, srcIdIsSet = GetExecSourceId()
get additional info for execution origin; depending on execution source
filename = GetExeName()
get application executable name with full path
check if script was interrupted by the user, intended to be called periodically in loop
add new json node
load text as JSON
text = GetJson()
get current document as text from editor
filename = GetCurrentFileName()
get name of file currently edited

Few additional functions - equivalent to WinAPI functions with same names - were grouped into "jsonedit_winapi" module.

hWnd = FindWindow(className, windowName)
finds handle to window, see WinAPI
SendMessage(hWnd, msg, wParam, lParam)
sends message to window with specified handle, see WinAPI
Beep(frequency, time)
generate sound through PC speaker or (on 64-bit Windows) default sound output
ret = MessageBox(message, title, flags)
standard WinAPI dialog
      MessageBox("message with just [OK] button", "message title", 0)
      MessageBox("message with ICON_INFORMATION", "message title", 64)
      local res = MessageBox("message with MB_YESNO and question icon", "message title", 4+32)
      if res == 6 then
      	ShowMessage("\"Yes\" was pressed")
      	ShowMessage("Result is other than \"Yes\"")
ret = ShellExecute(verb, file, parameters, directory)
execute another application
local winapi = require("jsonedit_winapi")
local hWnd = winapi.FindWindow(nil, "Opera Video Cache Player")
if hWnd ~= 0 then
	-- 16 = WM_CLOSE
	print("Sending WM_CLOSE\n")
	winapi.SendMessage(hWnd, 16, 0, 0) 
	print("Window not found\n")

Functions that allow passing data between different scripts or from one script execution to another are worth special mention. As scripts are running in GUI thread context they are intented to run to completion in short time (i.e. use of Sleep() should be limited even if it does not block GUI message processing) and they are mostly uninterruptible. As some uses require keeping some state data (e.g. original call target that was replaced in case of SIP originate function) following function were added:

SetVariable("name", "value")
set text "value" for variable with specified "name" (variables are holding text and are indexed by text)
value, isset = GetVariable("name")
read back variable value; function returns two variables (Lua can do this) and if variable was not set before then isset equals 0
"unset" variable (remove "name" from variables map)
clear ("unset") all variables

Back to JSONedit.

 "Cookie monsters": 7857668    Parse time: 0.000 s