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.
1 2 3 4 5 6 7 8 9 10 11 |
a_note = author: "Chris Woodall" text: "Fun Times" formatNote = (note) -> note.text + "\n\t- " + note.author ShowMessage(formatNote(a_note)) list = [1,2,3,4,5,6] res = (ShowMessage(i) for i in list) |
Which compiles to the following after running following command:
1 |
coffee -w -c -b -l test.coffee |
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// Generated by CoffeeScript 1.9.0 (function() { var a_note, formatNote, i, list, res; a_note = { author: "Chris Woodall", text: "Fun Times" }; formatNote = function(note) { return note.text + "\n\t- " + note.author; }; ShowMessage(formatNote(a_note)); list = [1, 2, 3, 4, 5, 6]; res = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = list.length; _i < _len; _i++) { i = list[_i]; _results.push(ShowMessage(i)); } return _results; })(); }).call(this); |
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.