Sample Maplet Application: Get File
This worksheet demonstrates how to write Maplet applications that function similarly to the Get File Maplet application available in the Maplets[Examples] package. It is designed for experienced Maple authors.
Simple Example
restart:
# Invoke the Maplets Elements subpackage. with(Maplets[Elements]): # Define the get file Maplet application. Define an abnormal # shutdown as one that returns FAIL. maplet1 := Maplet( 'abnormalshutdown' = "FAIL", FileDialog['FD1']( # Specify the Maplet application title. 'title' = "Read File", # The text appears on the approve button. 'approvecaption' = "OK", # Return the value referenced by FD1 # when "OK" is clicked. 'onapprove' = Shutdown( ['FD1'] ), # Return FAIL when "Cancel" is clicked. 'oncancel' = Shutdown( "FAIL" ) ) ):
# Assign result to the value returned by the Maplet application. result := Maplets[Display](maplet1);
# Check that result is a string. If it is a string, # result is opened. if type( result, ['string'] ) then read result[1]; end if;
Complex Example
The following example does not use any of the simplifications that have been included to make writing Maplet applications easier which leads to longer code but faster execution and for more complex Maplet applications, greater control of the Maplet application appearance and behavior. The required changes are:
1. An onstartup action must be specified. This action must be referenced and contain a RunDialog element which starts the FileDialog element.
2. Actions associated with onapprove and oncancel must be given by a reference and located outside the FileDialog element. The command elements must be wrapped in Action elements.
3. All option names must be specified, including reference and value.
with(Maplets[Elements]): # Define the "onstartup" option to be referenced by A0. maplet2 := Maplet( 'abnormalshutdown' = "FAIL", 'onstartup' = 'A0', FileDialog( 'reference' = 'FD1', 'title' = "Read File", 'approvecaption' = "OK", 'onapprove' = 'A1', 'oncancel' = 'A2' ), # Start the FileDialog. Action( 'reference' = 'A0', RunDialog( 'dialog' = 'FD1' ) ), # Define the resulting action if "OK" is clicked. Action( 'reference' = 'A1', Shutdown( '`return`' = 'R1' ) ), # Define the resulting action if "Cancel" is clicked. Action( 'reference' = 'A2', Shutdown( 'value' = "FAIL" ) ), # Return the value of "R1" to Maple session. Return( 'reference' = 'R1', ReturnItem( 'item' = 'FD1' ) ) ):
result := Maplets[Display](maplet2);
if type( result, ['string'] ) then read result[1]; end if;
Comparison
Maplet applications can be viewed by using the Maplets[Tools][Print] function. This function prints the XML data structure of the Maplet application.
Compare the resulting output of the two Maplet applications. Note that the only major difference is that the first example replaces the name references with _Maplets_reference_# where # is a unique number and the second example leaves the string references unchanged.
Maplets[Tools][Print](maplet1);
Maplets[Tools][Print](maplet2);
Maplets[Examples][GetFile]
The Maplets[Examples][GetFile] function displays a Maplet application similar to that of the previous examples. The function, however, allows the user to modify the approvecaption and title options.
For help on this Maplet application, see:
?Maplets[Examples][GetFile]
To view the source code, enter:
print( Maplets[Examples][GetFile] );
See Also
Maplets[Examples], Maplets[Examples][GetFile], Maplets[Elements][FileDialog]
Return to Index for Example Worksheets
Download Help Document