General :  K-Meleon Web Browser Forum
General discussion about K-Meleon 
MACRO- question, how do you exit on error ?
Posted by: MrWoo
Date: January 06, 2009 11:39PM

How would you handle an error in a macro. As in, you expect a value to be returned from something like a prompt, but get a "" instead. Usually you have an exit or return or end. I do not see this in the macro language. So, how do you achieve this? I know I could structure the functions of the macro so that it could be done, but still I wish to know if I am missing something.

Thanks.

MrWoo.

Options: ReplyQuote
Re: MACRO- question, how do you exit on error ?
Posted by: desga2
Date: January 06, 2009 11:57PM

For example: (in PSEUDOCODE)
Main_function{
$VALUE = prompt( MESSAGE [, TITLE [, DEFAULT_STRING]] );
$VALUE=="" ? &ConditionTrue : &ConditionFalse;
}

ConditionTrue{
# response == ""
}
ConditionFalse{
# response != ""
}

Or you can use if/else sentences:
$VALUE = prompt( MESSAGE [, TITLE [, DEFAULT_STRING]] );
if($VALUE=""){
# response == ""
} else {
# response != ""
}

Documentation about if(){} else{}.

K-Meleon in Spanish



Edited 1 time(s). Last edit at 01/06/2009 11:58PM by desga2.

Options: ReplyQuote
Re: MACRO- question, how do you exit on error ?
Posted by: MrWoo
Date: January 07, 2009 09:08AM

Oh, so the ampersand & would be what to use to tell it to 'goto' or 'call' a function. That is easy then, you just have a function that does nothing. But still there is no actual macro exit perse. So, in psuedocode, something like

Func{
$x = 1
$y = 2
$answer = prompt()
$answer != "" ? &Good : &Bad
}

Good{
$z = $x + $y
# some command here to do what the macro does
}

Bad{
# do nothing, macro ends
}

Is that a good example?

Thanks.

MrWoo.

Options: ReplyQuote
Re: MACRO- question, how do you exit on error ?
Posted by: JamesD
Date: January 07, 2009 01:38PM

@ MrWoo

Take a look at the bit of code below. If the user does a cancel then the return is "" so an if statement executes only when the return != "". There is no else part to the statement so in that case no more code is executed when a cancel is done.

 _ChmView_RunCode{
$_ChmView_DefaultOut = getpref(STRING, "k-meleon_chmview.general");
$_ChmView_CFolder = promptforfolder( "Select the folder where the chm file is located." );
if ($_ChmView_CFolder != "") {
	$_ChmView_CHMPath = promptforfile($_ChmView_CFolder, "CHM files", "*.chm");
	if ($_ChmView_CHMPath != "") {
		$_ChmView_CHMbase = basename($_ChmView_CHMPath, ".chm");
		$_ChmView_DefaultOut == "" ? &_ChmView_Default_Out1 : &_ChmView_Default_Out2 ;
		if ($_ChmView_OFolder !="") {
			exec($_ChmView_Path." -menu \"".$_ChmView_CHMPath."\" \"".$_ChmView_OFolder."\"");
			alert("Wait until files are written before doing OK.", "Waiting for chmdecoder.exe to finish", EXCLAIM);
			open($_ChmView_OFolder."\\".$_ChmView_CHMbase.".html");
			}
		}
	}
}


Options: ReplyQuote
Re: MACRO- question, how do you exit on error ?
Posted by: MrWoo
Date: January 07, 2009 03:55PM

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.

I understand what you mean, that is fairly standard for if/else. But I mean, maybe something like this:
myfunc{
checking values
call a function
getting data
call another function
getting user info
If user info ok Then
    do something else
Else
    exit macro - now
EndIf
some more operations
maybe some more function calls
}

See, where normally you can exit a script at any point. What you guys seem to be eluding to is that I will have to structure my macro so that in order to use an IF to end it, the IF should be the last thing checked. Correct?

MrWoo.

Options: ReplyQuote
Re: MACRO- question, how do you exit on error ?
Posted by: desga2
Date: January 07, 2009 06:14PM

Quote
MrWoo
Oh, so the ampersand & would be what to use to tell it to 'goto' or 'call' a function. That is easy then, you just have a function that does nothing. But still there is no actual macro exit perse.

Yes, you can use &<macroname>; instead macros( MACRO1 [,MACRO2 [,MACRO3 [...]]] ); to call macro functions in macro files.
You can found this in Macro documentation:
Quote
Macro Documentation:
macros( MACRO1 [,MACRO2 [,MACRO3 [...]]] );

Executes another macro. Multiple macro execution since 1.1.

&MACRO;

Equivalent to macros( MACRO ).

In your example code, this is same effect:

Func{
$x = 1;
$y = 2;
$answer = prompt();
$answer != "" ? &Good : 0;
}

Good{
$z = $x + $y;
# some command here to do what the macro does
}

Remember that 0 (zero) is do nothing.

Really there isn't a way to stop a macro, a macro code continue with the next code line to the end of code. In your second example, this must be to work fine;

myfunc{
checking values
call a function
getting data
call another function
getting user info
If user info ok Then
    do something else
    some more operations
    maybe some more function calls
EndIf
# else; user info not ok, do nothing.
}

K-Meleon in Spanish

Options: ReplyQuote
Re: MACRO- question, how do you exit on error ?
Posted by: JamesD
Date: January 07, 2009 06:17PM

Quote
MrWoo
myfunc{
checking values
call a function
getting data
call another function
getting user info
If user info ok Then
do something else
Else
exit macro - now
EndIf
some more operations
maybe some more function calls
}

Using your example the statements for 'then' will execute if the data is good. The statements for 'else' will execute if the data is bad. The statements after 'endif' are going to execute in both cases. To exit then you must reach the closing } without executing any statements which cannot be done with bad data from the user.

I was just setting down to plan out how to put this into "Training". Remember that, depending on your user's KM version, you might have to use the conditional instead of if-then.

Options: ReplyQuote
Re: MACRO- question, how do you exit on error ?
Posted by: MrWoo
Date: January 07, 2009 08:40PM

Quote
JamesD
Remember that, depending on your user's KM version, you might have to use the conditional instead of if-then.

Oh. I had not thought of that.

I suppose on the exit question then it will depend on the macro. It is helpful thought to know that, so that I can try to code them with those kind of checks at the end when possible, else I will have to use the other functions.

Thanks.
MrWoo.

Options: ReplyQuote
Re: MACRO- question, how do you exit on error ?
Posted by: JamesD
Date: January 10, 2009 03:09AM

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  ###############################


Options: ReplyQuote


K-Meleon forum is powered by Phorum.