Way back in LDC #39, we discussed the file “ApplicationInitialize.ls” and how any script functions in it are run at application startup. With GoFiler 4.21b’s release on January 16, 2018, we’ve added support for a similar file, “ApplicationShutdown.ls”. As the name suggests, this file is called when GoFiler shuts down. This is a pretty powerful feature because it lets you do things to “clean up” your environment if you’ve modified it with other scripts or if you want to return the system to a desired state after using the application.
In another blog post (LDC# 42), we discussed a script that could unregister GoFiler. With the new ApplicationShutdown file, we can set that to automatically trigger after the application exits. This way, GoFiler will automatically release its license after closing, so another user can open it on a different machine. This is very useful in production environments with multiple shifts using different computers, where you may have more than one user sharing a key. It also could be handy in an environment where you switch between desktop and laptop computers to work. The drawback here is that GoFiler will require an internet connection to open or close successfully with this script running, but that’s very rarely an issue. Here’s our modified script:
#define BAT_NAME "RegisterGF.bat"
#define TIMEOUT "timeout /t 3 /nobreak > nul\r\n"
int main() {
string temp_folder; /* temp folder */
string gofiler; /* path to GoFiler */
string bat_contents; /* contents of bat file */
string bat_file; /* location of bat file */
/* */
gofiler = "\""+AddPaths(GetApplicationExecuteFolder(), /* path to GoFiler */
"GoFiler.exe\""); /* */
bat_contents = TIMEOUT; /* add a sleep to bat file */
bat_contents += gofiler+ " /NoGUI /unregister -silent\r\n"; /* */
temp_folder = GetTempFileFolder(); /* get temp folder */
bat_file = AddPaths(temp_folder, BAT_NAME); /* get full path to bat file */
StringToFile(bat_contents,bat_file); /* write out bat file */
RunProgram("cmd.exe", "/C \""+bat_file+"\""); /* run the bat file */
return ERROR_NONE; /* return */
}
This is a very simple script with only one function, main. The script processor calls it after the application frame has closed. It writes out a batch file named “RegisterGF.bat” and then runs it. This isn’t complicated, and it gets the job done. As mentioned above, the ins and outs of this script are described in detail in the previous blog post. We’ve just refactored it slightly to work as a cleanup script instead of as part of a menu function.
When using the ApplicationShutdown file to wind down your application, the important thing to keep in mind is that not all Legato functions will work in this setting. The application frame has closed by this point, so you will not have access to things like Edit Windows, Edit Objects, etc. Message boxes work, but they will not have the full functionality that they normally do when used as part of a script. The idea behind the shutdown script is more to put the system back to a known state, so these objects shouldn’t be needed anyway. Core functionality, like file I/O, HTTP GET/POST requests, and ODBC connectivity, still function as expected.
The example given is obviously very basic, but much more complex uses aren’t hard to imagine. For example, a script solution could be written to handle checking in or out files from a revision control system. When a user closes the application, it could be set up to automatically release any locks or check back in any files that the user may have held. With the ability to manipulate the file system, connect to databases, and communicate with web pages, there are a lot of ways running Legato scripts on application shutdown can be integrated into a system.
Steven Horowitz has been working for Novaworks for over five years as a technical expert with a focus on EDGAR HTML and XBRL. Since the creation of the Legato language in 2015, Steven has been developing scripts to improve the GoFiler user experience. He is currently working toward a Bachelor of Sciences in Software Engineering at RIT and MCC. |
Additional Resources
Novaworks’ Legato Resources
Legato Script Developers LinkedIn Group
Primer: An Introduction to Legato