For, Else If and Switch/Case support for Macro Language

April 24, 2021

Today Macro Language gets a major update with many new features that makes building macros easier than ever.

For Loop

Macro Language had ForEach statement to loop over an array, and While statement for any other loops. With this update, it now has support for For statement which works similar to many other programming languages. Here is an example of how this can make things easier:

Old
New
// Add numbers from 1 to 10 $sum = 0 $i = 0 ExpandWhile ($i <= 10) $sum += $i $i += 1
// Add numbers from 1 to 10 $sum = 0 ExpandFor ($i = 1, $i <= 10, ++$i) $sum += $i

Else If

Another addition to the Macro Language is Else If statement. This makes checking multiple conditions much easier compared to what was possible with the old nested If/Else statements

Old
New
ExpandIf ($width < 100) // handle small width ExpandElse ExpandIf ($width < 500) // handle medium width ExpandElse ExpandIf ($width < 1000) // handle large width ExpandElse // handle extra large width
ExpandIf ($width < 100) // handle small width ExpandElse If ($width < 500) // handle medium width ExpandElse If ($width < 1000) // handle large width ExpandElse // handle extra large width

Switch/Case

Similar to If/Else If/Else structure, you can also now use Switch/Case/Default structure if you are always comparing the same value. Note that Switch/Case requires Break statements to stop processing a Case, otherwise it moves onto the next Case:

Old
New
ExpandIf ($scope == "Current Page") // handle current page ExpandElse ExpandIf (($scope == "Current Section") || ($scope == "Current Section Group")) // handle current section or section group ExpandElse ExpandIf ($scope == "Current Notebook") // handle current notebook ExpandElse // handle all notebooks
ExpandSwitch ($scope) ExpandCase "Current Page": // handle current page Break 1 ExpandCase "Current Section": ExpandCase "Current Section Group": // handle current section or section group Break 1 ExpandCase "Current Notebook": // handle current notebook Break 1 ExpandDefault: // handle all notebooks

Increment/Decrement Operators

Yet another addition is the increment and decrement operators, which can be either pre-increment/decrement or post-increment/decrement. As in many similar languages, pre-increment/decrement operators will modify the value before returning it while post-increment/decrement operators will do so after returning the original value.

Old
New
// Pre-increment // Increment $a and then copy that to $b $a += 1 $b = $a // Pre-decrement // Decrement $a and then copy that to $b $a -= 1 $b = $a // Post-increment // Copy $a to $b and then increment $a $b = $a $a += 1 // Post-decrement // Copy $a to $b and then decrement $a $b = $a $a -= 1
// Pre-increment // Increment $a and then copy that to $b $b = ++$a // Pre-decrement // Decrement $a and then copy that to $b $b = --$a // Post-increment // Copy $a to $b and then increment $a $b = $a++ // Post-decrement // Copy $a to $b and then decrement $a $b = $a--

Operator Precedence

The final part of this update introduces operator precedence. In macro expressions, following operator precedence rules apply. The order of operations can be changed by providing parantheses. The Expression Editor automatically fully parenthesizes any expression so that what operator precedence rules were used is obvious. If you type 5 == 2 + 3 * 4 to Expression Editor it will automatically parse and convert it to 5 == (2 + (3 * 4)).

PrecedenceCategoryOperators
1Increment/Decrement++––
2Unary!+
3Product*/%
4Sum & Concatenation+&
5Comparison==!=<<=>>=
6Logical And&&
7Logical Or||
8Ternary?:
9Assignment=+=–=*=/=%=&&=||=&=

Comments

Name
Comment
robc - 2021-08-30
Nice update touches, Thanks!
Dengle - 2021-08-08
Is it possible to call shell or powershell from Macro language with an argument and get some results back and put on the page?
haruhi - 2021-07-22
Is it possible in onecalendar
Display pages on every edit  days in my OneNote  Instead of on created date, last modified date or both . So I can clearly see what work has been done in the past week

Other Posts

Show all posts