Let Qlab run other applications with Applescript (with ToneGenerator as example)

TL;DR:
Use Qlab to control every application on your Mac, so you can focus on your show. Also, ToneGenerator is great for soundcheck and room tuning.

The Story:
By now, you know that I am a big fan of using Qlab for everything. Part of that is the unified interface with the shows I work on – it can help reduce the feeling of multiple personality disorder, when you are switching between a sound design one day, video later that afternoon, running a light board the next day, and so on. Because Qlab can run Applescript – which is a simple language that can control all of the macOS (aka OSX) – you don’t have to worry about switching windows or getting confused, or losing “focus” (focus is basically the application that is open and “on top” – the one you are currently in, even if you are multitasking).

So, in the interest of teaching a person to fish, we’re going to look at the details of some of the scripts I have shown in past posts and why they work. You can use even these simple commands in your own work to automate things.

The Esoteric Bit:
I use an app called “Tone Generator” for sound check and room tuning. Does what you expect – generates white noise, pink noise, sine waves, etc. But I don’t want to look for that, even if it’s in the Dock on my Mac. I want to add opening and starting that to other things I do at sound check – maybe launching a video, or clearing up files that were created during the last show run. So, I write a script in Qlab:

Applescript entered into Qlab’s script editor

Let’s look at each line. (Sorry, there are no line numbers in the script editor…that would be great, Figure 53, if you’re reading this… 🙂

tell application “ToneGenerator”

This is simply instructing macOS that we are going to send commands to an application called “ToneGenerator”. Spelling is important. This is the name as it appears in your Applications folder. I have had applications that I kept multiple versions of – in that case, I called them “Name 2” and “Name 3” – denoting the versions. The exact name has to be reflected in this line. Spaces count.

–loads Tone Generator, http://www.nchsoftware.com

This is a comment. Putting two dashes means it is a comment written into the Applescript, and not executable code. You can also put comments on the same line as code – so I could have just as easily put this on the line above.

activate

This is simply having macOS to load the application, which then puts it in the foreground (gives it “focus” – this can be important).

delay 1

Simply adding a delay of one second before transmitting the next line of code to macOS – giving the application time to load. You may find you need to adjust your delays, depending on what you are doing.

tell application “System Events” to key code 49

This line is commanding a part of macOS, called System Events, to type a key on your keyboard for you. That’s right, you can have your computer type for you.

This is a cool part of Applescript. Every key on your keyboard has a corresponding code. Here is a great reference page of all of the codes for your entire keyboard: https://eastmanreference.com/complete-list-of-applescript-key-codes. In this case, I am telling macOS to hit the space bar. Additionally, you can also use the term “keystroke” instead of “key code” – in that case you would just put the name of the key in quotes. But in this example, it doesn’t work as well for the space bar, and so key codes are better. Regardless, System Events does the keyboard work for you. Why am I sending a space bar command to this application? Because that is the Play button in ToneGenerator – my last selected noise will start to play.

end tell

This says to macOS that you are done with your script – you’ve closed up the “tell” command above.

You can mix and match all sorts of commands with Applescript. This is just a basic example of loading the application ToneGenerator and having it start to play back sound.

If you have ever heard of API, or programming hooks, this is basically what we are doing here. (If you haven’t heard of API, skip it for now. It’s ok if some part of your computer is a magic black box…)

Next post I will talk about how I used some of these basic commands with Logic and Evernote at my front of house desk on a rock tour!

Cheers!
-brian

P.S. If you really like working in Applescript and want to know more, there is an easy to use tool called “Automator” that you’ve probably seen on your hard drive. Looks like a little robot:

Automator, your helpful little robot friend.

It provides you with a simple drag and drop interface to program your own lines of code. Give it a try! Then paste the results into your script editor and see how it does in Qlab.