Triggering a Screen Capture from Lua
It’s not uncommon that users wish to take a screenshot of their application for testing purposes, automation, or even for niche UX cases.
In a few cases we’ve seen a tendency for some users to try and trigger a screendump from Lua by directly invoking gra.screendump().
This doesn’t work as anything prefixed with gra is a part of the built-in action definitions and not something that can be invoked directly in Lua.
As per the documentation:
Storyboard supports a number of standard actions which are built-in to the framework. These actions are all prefixed with gra. and can be incorporated into your application design without any plugin dependency.
Any of the gra actions are the actions available to you in Storyboard Designer via the New Action dialog.
So to actually invoke a screencapture from Lua, you will instead have to send a custom event from Lua which then triggers a screencapture action.
Here we’ve created a custom event called take_screenshot which executes a Capture Screenshot (gra.screencapture) action. As you can see the filename is set to the value of a custom variable called screenshot_title.
Both the variable screenshot_title and this new action are bound to the Application level as opposed to a specific screen. The reason for this is that it allows you to re-use the event to take screenshots of any screen in your application.
You could then trigger this action in Lua like so:
local date1 = os.date("%Y%m%d")
local date2 = os.date("%H%M%S")
local filename = string.format("screenshot.%s.%s.bmp", date1, date2)
Here we are setting the filename variable to contain the time and date and then sending the take_screenshot event to trigger the action we set up previously.