Part Two: You are now a Programmer

Welcome back. Yesterday, we gave you an introduction to what Myo Scripts are and what kind of tools you’ll need to write them. Today, we’re going to actually write our first script.

Your First Function

To define a function in Lua, you just write function <function name>(<argument 1>, <argument 2>, <… etc>) on a new line, replacing the things in angle brackets with your own names for things. Put whatever logic you need indented on the next few lines, and end the function with end on it’s own line. Unlike some languages you don’t need semicolons at the end of each line (though it’s not going to break if you put them in by force of habit). You also don’t need to specify what type of variables your arguments are, or what you’ll be returning.

onForegroundWindowChange has two arguments, app and title. Since this is a callback defined by Myo Script, we can’t change this at all. So below your scriptDetailsUrl assignment you will want to put something (exactly) like this:


function onForegroundWindowChange(app, title)


And your text editor will have something like this:

First script with blank onForegroundWindowChange2

The job of onForegroundWindow is to let you determine if your script should be active or not. The idea is that each script is targeted at a certain app (or set of apps), so onForegroundWindow fires every time a new app is in the foreground. app is the bundle identifier on Mac OS X or the name of the .exe file on Windows and title is the actual title of the foreground window. For example, if you are reading this in a web browser chances are it will be something like < Title of this web page > – < Browser you are using >, and you’ll see that up at the top of the window or tab. You could write a script that works on a specific web page, on the browser in general, or any other app. If you detect an app your script supports, return true.

Let’s just return true for now. What we’re really going to do is to output the app and title arguments to the Myo debug window. This is usually the first thing you want to do when supporting a new app so you get an idea of how you can actually identify the app in question. So how do we print debug output?

Myo scripts have a global myo object defined. This is where all the myo-specific functions live. One of these is debug, which accepts a string to output to a special console window that will pop up as soon as you start writing to it, but ONLY if you have “Developer Mode” enabled in Myo Connect’s Preferences. Make sure that is on, and then let’s output the name of the window in the foreground. You can give it a try yourself, but if you get stuck put this above your return statement:


myo.debug("onForegroundWindowChange: " .. app .. ", " .. title)`  

.. concatenates (i.e. joins) two strings together in Lua, and <object>.<function name>(<arguments>) calls a function.

Your full script should look something like this:


scriptId = 'com.thalmic.examples.myfirstscript'  
    scriptTitle = "My First Script"  
    scriptDetailsUrl = ""

    function onForegroundWindowChange(app, title)  
        myo.debug("onForegroundWindowChange: " .. app .. ", " .. title)
        return true

Save it, then add it via the Application Manager. Make sure you re-add it every time you change (and save) something in your script. You should see something like this pop up:

Debug console with onForegroundWindowChange: , Load Custom Script and onForegroundWindowChange: ,  Debug Console

Congratulations! You just wrote your first Myo Script!

Go ahead and see what kind of output you get when you put different apps in the foreground. Next week, we’ll get to the fun stuff and detect our first poses.


Enter your email address and get all latest content delivered to your inbox every now and then.