How do I save custom properties in a standalone application?
Custom properties are a very useful way to store information, and if you want that information to persist between sessions you can just save your stack and the custom properties are saved and available next time you start it up. But what if you want to use and save custom properties once you have built your application into a standalone?
In this lesson we will learn how to create a standalone application consisting of a launcher and an editable file.
This lesson only applies to desktop apps. To save custom properties or other state data in mobile apps see this lesson.
The launcher and the main stack
Firstly we need two new stacks, one will be the launcher and one our main application.
From the file menu create a new mainstack and name it Launcher, then create another main stack and name it Main Application.
Creating the launcher
The launcher stack will be built into a standalone and all we need it to do is to open our main application, it can then close itself. Add the following script to the stack script of the launcher stack.
on openStack // Set the default folder set the itemDel to "/" if the environment is "standalone application" then if the platform is "MacOS" then set the folder to item 1 to -5 of the filename of this stack else set the folder to item 1 to -2 of the filename of this stack end if else set the folder to item 1 to -2 of the filename of this stack end if open stack "Main Application.livecode" close stack "Launcher" end openStack
Firstly we set the default folder to the folder containing the standalone and Main Application stack, this allows the Main Application stack to be found.
The launcher will then open the Main Application stack, which can be modified and saved as it is not a standalone.
Adding some script to the main application
Now we need a simple test to check that our Main Application can be saved and that custom properties are preserved.
Lets use our name as an example. We will add a field called "name" to type your name into (1), a button which saves the name as a custom property (2) and another button which displays the saved name using an answer dialog. (3)
The Save Name button just stores the name in a custom property
on mouseUp set the cName of this stack to field "name" put empty into field "name" end mouseUp
The Display Name button uses an answer dialog to display the name saved in the custom property
on mouseUp answer the cName of this stack end mouseUp
Saving the stack
In order to preserve the custom property cName we need to save our Main Application stack when it is closed. Add the following script to the stack script of the Main Application stack
on closeStack save this stack pass closeStack end closeStack
This means any changes we have made to the stack, including our custom property are saved.
Building a standalone
Now build the Launcher stack into a standalone. Once the standalone is built copy the Main Application file into the same folder as the standalone. Note, the standalone is an application (.exe) file, even if the extensions does not show, and the Main Application is a .livecode file (not an app).
Testing our application
Now close both the Launcher and the Main Application file. Re-open the launcher, you should see the Main Application window open and the Launcher close. Put in your name, save it and check it displays. Now close the application, run the launcher and try Display Name again. The custom property storing your name should have been saved from the previous run.