Quote
MrWoo
Thanks for that great example of using if statements in macro. Actually, you might put one of those in your tutorial macro. A good complex working example is great to have around.
This is the next chapter for Training.
http://kmeleon.sourceforge.net/wiki/KmmTraining
I have this done for KM ver 1.5.x but have not completed one for KM ver 1.1.x yet. You must install Training.kmm in order to run later chapters. The current MacroLibrary is for versions 1.1.x and later. I just wonder if a lot of users are still using 1.1 versions?
Training2.kmm KM ver 1.5.x required.
# K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)
#
# ---------- Training2.kmm
# ---------- K-Meleon Macro Language Training/Example ---------------
# ---------- CHAPTER 2
#
# Dependencies : main.kmm, Training.kmm, K-Meleon ver 1.5 or later
# Resources : -
# Preferences : -
# Version : - 0.3 2009-01-09
# --------------------------------------------------------------------------------
Training2_EECode{
#-- How to end / cancel the execution of the macro based on a condition
$_Training2_Catch = "false" ;
macros(_Training2_EECodeRun) ;
}
_Training2_EECodeRun {
macros(_Training2_Ver);
$_Training2_Text0 = "There is neither an 'end' nor an 'exit' statement in the MacroLanguage. \n" ;
$_Training2_Text0 = $_Training2_Text0."In order to stop executing statements based upon a condition, the ending \n" ;
$_Training2_Text0 = $_Training2_Text0."brace '}' for the macro must be reached without executing statements which \n " ;
$_Training2_Text0 = $_Training2_Text0."would fail because of the condition. \n\n ";
$_Training2_Text0 = $_Training2_Text0."At the prompt which follows, please select a file of the type 'name.kmm'. ";
macros(_Training2_EECode_L ) ;
#-- There would normally not be any additional code here, but because this is training
#-- there are these additional lines
$_Training2_Text0 = "Normal execution of a macro would have stopped at the presentation of the user's \n";
$_Training2_Catch == "true" ? &_Training2_EECodeRunC : &_Training2_EECodeRunE ;
$_Training2_Text0 = $_Training2_Text0."When the end of a macro is reached, execution control returns to the \n" ;
$_Training2_Text0 = $_Training2_Text0."macro which called the macro, if any. \n\n"
$_Training2_Text1 = "_MacroFirst { \n ";
$_Training2_Text1 = $_Training2_Text1." <code lines> ;\n ";
$_Training2_Text1 = $_Training2_Text1." macros( _MacroSecond ) ; \n " ;
$_Training2_Text1 = $_Training2_Text1." # control returns here from completion of _MacroSecond \n";
$_Training2_Text1 = $_Training2_Text1." } \n\n " ;
$_Training2_Text1 = $_Training2_Text1."_MacroSecond { \n" ;
$_Training2_Text1 = $_Training2_Text1." <code lines> ; \n ";
$_Training2_Text1 = $_Training2_Text1." } " ;
alert($_Training2_Text0.$_Training2_Text1, "Execution control", INFO);
$_Training2_Text0 = "If you selected a file from the prompforfile this time, try the training \n " ;
$_Training2_Text0 = $_Training2_Text0."again and select to cancel rather than open a file. " ;
if ($_Training2_Catch == "true") alert($_Training2_Text0,"Catch", INFO); else alert($_Training2_Text0,"End / Exit", INFO);
}
_Training2_EECodeRunE{
$_Training2_Text0 = $_Training2_Text0."answer and the code example. In the case of a choice to cancel, execution would \n";
$_Training2_Text0 = $_Training2_Text0."have stopped because no code branch was provided for that case. \n\n";
}
_Training2_EECodeRunC{
$_Training2_Text0 = $_Training2_Text0."answer and the code example. In this case the to cancel is confirmed or the user \n";
$_Training2_Text0 = $_Training2_Text0."is returned to the point in the code where another attempt can be made. \n\n";
}
_Training2_EECode_L{
#-- Method for KM ver 1.5.0 or later ( if statement )
alert($_Training2_Text0,"End / Exit", "INFO");
macros(_Training2_EECode_L2) ;
}
_Training2_EECode_L2 {
$_Training2_Result = promptforfile($_Training2_Path, "kmm files", "*.kmm");
$_Training2_Result = basename($_Training2_Result, ".kmm");
if ( $_Training2_Result != "" ) {
$_Training2_Text0 = "The creation and presentation of these lines of text occur only \n" ;
$_Training2_Text0 = $_Training2_Text0."in the lines of code executed when the result is not missing. \n\n" ;
$_Training2_Text0 = $_Training2_Text0."Result = promptforfile($_Training2_Path, 'kmm files', '*.kmm'); \n" ;
$_Training2_Text0 = $_Training2_Text0."Result = basename(Result, '.kmm'); \n";
$_Training2_Text0 = $_Training2_Text0."if ( Result != '' ) { \n";
$_Training2_Text0 = $_Training2_Text0." Text0 = 'The creation and presentation of these lines . . .' \n";
$_Training2_Text0 = $_Training2_Text0." . \n";
$_Training2_Text0 = $_Training2_Text0." . \n";
$_Training2_Text0 = $_Training2_Text0." alert('Your selection was '. Result.'\\n\\n'. Text0, 'Name selected') \n";
if ( $_Training2_Catch == "true" ) {
$_Training2_Text0 = $_Training2_Text0." Text0 = 'You selected to cancel from the prompt for file.'; \n" ;
$_Training2_Text0 = $_Training2_Text0." CAnswer = confirm('Are you sure you wish to cancel?',Text0, YESNO, QUESTION); \n" ;
$_Training2_Text0 = $_Training2_Text0." if (CAnswer == 'NO') &_Training2_EECode_L2; else alert('Cancel is confirmed'); \n" ;
}
$_Training2_Text0 = $_Training2_Text0." } ";
alert("Your selection was ".$_Training2_Result."\n\n".$_Training2_Text0, "Name selected");
}
#-- The following statements are used only if we are training on catch
else { if ( $_Training2_Catch == "true" ) &_Training2_CatchRun ; }
}
Training2_CatchCode{
#-- Explain what is meant by catch in programming
$_Training2_Text0 = "The term catch means to provide an error handler within the code. \n\n" ;
$_Training2_Text0 = $_Training2_Text0."If an error condition is encountered, there must be a way to \n" ;
$_Training2_Text0 = $_Training2_Text0."gracefully end the program or provide a way to recover from the error. \n\n" ;
$_Training2_Text0 = $_Training2_Text0."We will use the 'End / Exit' example, but this time a method to \n" ;
$_Training2_Text0 = $_Training2_Text0."handle the condition raised by a cancel will be added. " ;
alert($_Training2_Text0, "Catch", INFO);
$_Training2_Catch = "true" ;
macros(_Training2_EECodeRun) ;
}
_Training2_CatchRun{
$_Training2_Text0 = "You selected to cancel from the prompt for file." ;
$_Training2_CAnswer = confirm("Are you sure you wish to cancel?",$_Training2_Text0, YESNO, QUESTION);
if ( $_Training2_CAnswer == "NO" ) &_Training2_EECode_L2; else alert("Cancel is confirmed");
}
Training2_BuildMenu{
# add additional lines to the training menu
setmenu($Training_Popm,"macro","[2] End / Exit","Training2_EECode");
setmenu($Training_Popm,"macro","[2] Catch","Training2_CatchCode");
}
_Training2_GetPath{
## Training.kmm will be located in one of two possible locations
## If readfile finds nothing at MacroFolder then the location is UserMacroFolder
$__Data=readfile(getfolder(MacroFolder)."\\Training2.kmm");
$_Training2_Path=getfolder($__Data==""?UserMacroFolder:MacroFolder);
}
$OnInit=$OnInit."_Training2_GetPath;";
$macroModules=$macroModules."Training2;";
################ END OF Training2.KMM ###############################