Using CoffeeScript To Write Altium Scripts

Altium is a powerful program which can be scripted. There is a built in scripting engine and API which is mostly exposed using an Object Model. You can read more about Altium Scripting here, but it turns out the main languages you can use are DelphiScript, VisualBasic and Javascript (called JScript in the Altium documentation). This version of Javascript implements what appears to be an ECMAScript 5 compliant version of javascript, and as a result you can apply javascript design techniques to your Altium Scripts.

I have not seen many articles which reference this, and I find it interesting, because it means you can use Javascript transpilers to write your scripts in languages such as CoffeeScript. These languages compile to valid Javascript, and can result in more maintainable scripts (assuming you can maintain the compiler version).

The Tools

For this project I used a pretty simple set of tools. However, the versions of the tools may be useful to know, as they may dictate how effective this method is.

  • Altium Designer 2013
  • CoffeeScript, version 1.9.0

Testing It Out

To test this out I decided to write a simple script that would show a message in Altium when run. This script uses the ShowMessage(str) function, which is the Altium version of alert(str). Below is a script which defines a javascript object, a function to format that object and then shows the formatted alert. It also includes a list comprehension, a feature not natively present in javascript, to count from 1 to 6 and show a message for each count.

Which compiles to the following after running following command:

.

Believe it or not, this is a valid Altium script!

Why?

To be honest I am not quite sure why you would do this; however, it does open the door of doing more complex Altium script development for self contained libraries, since CoffeeScript can actually join multiple ".coffee" files into one at compile time. You also have the power of an expressive language at your fingertips. However, the largest value of this experiment is that it show that you can recycle work done by the general javascript community to writing Altium scripts. I will be inspecting other such opportunities as time goes on.

Leave a Reply

Your email address will not be published. Required fields are marked *