tutorial 02: Front-end Basics
The basics of controlling the magnetron.app front-end
In this tutorial you will learn to:
- Set progress and main displayed message
- Use a timed delay
- Check if the user has pressed cancel
- Abort the process
- Quit the application
To begin, create a new recipe as you did in Tutorial 01 - Hello World.
It is important to keep the user informed while using the recipe, so instead of using the echo function echo("hello world");
from the first tutorial, you can use the setProgress()
and setMainMessage()
functions. To set the progress bar to 50%, you can use setProgress(50);
, and to display a status message to the user, you can us setMainMessage("waiting for cancel action");
To give the user time to cancel the process, you can use the sleepMs function to add a delay. In this example, you can add the line 'sleepMs(5000);' to give the user 5 seconds to cancel the process.
To check if the user has opted to cancel the process, you can use the if( isCanceled() )
method. If the user has canceled the process, you can use the abort function to stop any further execution of the script by adding
{
abort("Canceled");
}
Or use the quit("quitting");
function to fully shutdown the application instead. You can include a message with either function, which will be echoed to the log and dialog.
Finish
To finish the process, you can set the progress bar to 100% using setProgress(100);
and set the main message to "stopped normally" using setMainMessage("stopped normally");
Now you can run the recipe using (Ctrl/Cmd + Shift + S) and hit cancel to try out the functionality. Great work, you now know how to use the core app interactions!
The complete function:
function main()
{
setProgress(10);
setMainMessage("waiting for cancel action");
sleepMs(5000);
if ( isCanceled() )
{
abort("Canceled");
quit("Canceled");
}
setProgress(100);
setMainMessage("stopped normally");
}