Learning to Code in JW Lua | Part 1: Introduction to Lua

JW Lua is quickly becoming a popular plug-in for Finale because of it’s unbelievable potential for improving your Finale workflow. It’s over 300 times faster than FinaleScript, more flexible than your macro program of choice, and connects directly to Finale’s codebase. Which means it can save you lots of time while achieving better results, regardless of what “better” means to you.

There’s just one problem:

Because it’s written in a language not known by all programmers, and is a highly customized version of that language, it can appear daunting to learn how to code with it. It can seem even harder if you’ve never coded before.

So today we’re going to start a series of how to code with JW Lua, even if you’ve never coded before. We’re going to walk through, step-by-step, every aspect of JW Lua that you need to code in JW Lua.

Intro to Lua Programming

To code with JW Lua, we have to know some Lua. And that’s where we’re going to start.

Note, this is going to be one of the few posts in this series where we don’t code with the plug-in, but that’s so we can get a bit of a grasp on the lua language first.

In your web browser of choice, go to Repl.it – Online Lua Editor and IDE – Fast, Powerful, Free. This is a site that allows you to code Lua and run it in your browser, so you don’t have to worry about installing anything. You should see something like this.

The process of what we’re going to do is simple. We’re going to type code into the middle section, hit “run”, and it will print an output in the right, darker section.

So in the middle section, now write:

print('Hello, world!')

If you hit run, on the right it should now say Hello, world! Exciting! You’ve written your first line of Lua!

Let’s look at what’s going on here. print is a command that tells lua to put whatever is within the parentheses into the output.

Try typing each of these lines into the editor.

print('I can code lua!') -- output: I can code lua!
print('This is exciting') -- output: This is exciting
print('Pizza') -- output: Pizza

Introduction to Data Types

“But why’”, you may ask, “do I need the single quotes around the text?”

Well, let’s try it. In the editor, try typing print(Pizza) and run it. Output:

nil

Wait, nil??? Did you expect Pizza?

Strings

When we put single quotes around the text, it told Lua, “Hey, this is some text. Treat it as such”. So, Lua treated the text as a string.

In programming, a string is basically a fancy name for any piece of text. And a string is an example of a data type. Data types being the different ways computers can understand data.

Let’s look at some other data types.

Note: In Lua, you can define a string with either single quotes or double quotes. Most programmers use single quotes because it’s easier to type (no need to hit shift).

Numbers

print(1) -- output: 1

Numbers are anything that you can type with the digits 0-9 and a decimal.

  • Whole Numbers (0, 1, 2, 3)
  • Negatives (-1, -2, -3)
  • Decimals (0.1, 3.14, 2.71828, -143.23)

These are all valid numbers. Put any of these into the print function and it will output correctly.

Unlike strings, there isn’t a special way to define a number. You just type it normally.

There aren’t fractions, though. Only decimals. However, you can do some mathematical operations with two numbers.

print(1/2) -- output: 0.5
print(2*4) -- output: 8
print(5-11) -- output: -6
print(5+11) -- output: 16
print(5^2) -- ^ means exponent, output: 25

Boolean

Booleans are either true or false. That’s it.

To define a boolean, all you need to do is type true or false. They may seem simple, but incredibly powerful as you’ll find out later on.

print(true) -- output: true
print(false) -- output: false

Nil

nil is a unique data type. It actually means the lack of any data or data type. Remember, 0 is a number. Nil is nothing.

So then, when we did print(Pizza) without the quotes, why did it output nil?

Introduction to Variables

Having a bunch of data is pointless if we don’t have a place to store it. That’s why programming languages have variables. Variables are a simple way to keep track of all your data.

Here’s how we define a variable.

local name = 'Hello, world!'

Let’s break this down:

  • local: tells Lua that we’re defining a variable
  • name: this is the name we use to identify the variable
  • =: the equals sign does the actual assignment
  • ’Hello, world!’: the data assigned to the variable

A variable can hold any of our data types. For instance

local my_string = 'I can code now'
local my_num = 1234
local my_bool = false
local my_nil = nil

And to use a variable, we just need to use the name.

local my_string = ‘I know Lua variables’
print(my_string) -- output: I know Lua variables

In the editor, now, try defining one variable for each data type, and then printing out the results. The practice will be good for you.

Doing Something Awesome in Lua

Ok, now we’re on the final stretch to actually doing something awesome in Lua. Because up to now, you may be thinking, “well, this is nice. But what use is this? I want to the code to actually do something”. Now here’s our chance.

And we’re going to do it with the for loop.

For Loops

A loop is a block of code that we can run multiple times. And a for loop is one of the most common (and powerful) types of loop. Here’s an example:

for i = 1, 10, 1 do 
    print(i) 
end

Output:

1
2
3
4
5
6
7
8
9
10

In the for loop, there are 3 parts.

  • i = 1: This sets a variable to any number
  • 10: This is the min/max. When I goes above/below this, exit the loop
  • 1: The increment, in this case add 1 to i each time through the loop

Then, do what ever is inside the code block. Here’s another loop:

local sum = 0
for num = 1, 100, 1 do 
    sum = sum + num
end
print(sum) -- output: 5050

This loop adds every number from 1 to 100.

So before the loop, we defined a variable sum, and each time through the loop, we add the current number to sum. After the loop, we print out sum.

And here’s one last example:

print('T-minus...')
for count = 10, 1, -1 do 
    print(count)
end
print('Blastoff!!!')

Output:

T-minus...
10
9
8
7
6
5
4
3
2
1
Blastoff!!!

Ready to Learn More?

Half the battle of learning is reading the information. The other half is actually trying it out for yourself. So here’s some challenge problems for you:

  1. Make a loop that prints out the first 10 perfect squares
  2. Make a loop that prints out the first 50 even numbers
  3. Print out the first 50 powers of 2 (e.g., 1, 2, 4, 8, etc.)
  4. Print out your favorite flavor of ice cream

Go ahead, share your solutions in the comments below. Let’s make this a great community of people learning JW Lua together.

And stick around for next week, where we’ll dive right into JW Lua and learn how to change the noteheads in every note of a selected region. It will be exciting! See you then.

Nick

P.S. If you’re a visual learner, definitely check out the corresponding videos on my YouTube channel. Each week, I’m creating a video that lines up to what we’re going over in this blog. Same information, different ways of learning.

SEE ALSO

Finale: Getting Started With JW Lua

Moonlighting with Lua: The powerful tool for music notation you never hear of


Nick Mazuk is a composer and trombonist in Los Angeles. He mainly freelances by creating arrangements and original compositions for orchestra. In Finale, he specializes in automating tasks so he can achieve great results fast. Nick’s YouTube channel provides more tips and tricks for speeding up your Finale workflow.

7 Replies to “Learning to Code in JW Lua | Part 1: Introduction to Lua”

  1. This was a great introduction, and very clearly written.
    One small point I noticed as a programmer is that “–” indicates the start of a comment (to the end of the line), and this is not documented.

    Otherwise great stuff!

    1. Great note about the comments!

      Literally the only reason why I didn’t include it here was because I was waiting to comment on comments (pun intended) for the third and fourth articles in an effort to keep everything as clear as possible. Always good to have this kind of feedback, though.

  2. Also, Lua is the underlying macro language for Dorico, and hopefully will be the basis of a future plugin language, so learning this for use in Finale could pay future dividends as well.

  3. Thanks, from Italy, for the help (also video): I found the following solutions

    soluzione 1

    for num = 1, 10, 1 do
    print (num^2)
    end
    ——————–
    soluzione 2

    for num = 2, 100, 2 do
    print (num)
    end
    ———————-
    soluzione 3

    for num = 1, 50, 1 do
    print (2^num)
    end

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.