Customize Your Console Environment

Both SwaN-MR and iNMR have never been scriptable for a simple reason. The logical scripting language for a Mac application is Apple Script, but the average chemist can't write in Apple Script. In the years the situation has always been so clear, therefore I came to the conclusion that Apple Script is too difficult. I have also found a more intrinsic explanation. While all programming languages are something in between the English language and math expressions, Apple Script is, intentionally, as far as possible from the math language. The effect is that its syntax is very difficult to grasp (impossible even for many scientists).

Lua has too many advantages. It has been created just to be embedded into stand-alone applications (like iNMR). It can be modified or pruned and still works great. The syntax is, by design, as similar as possible to that of other existing computer languages. Lua contains many levels of sophistication, but the user is not forced to learn them. He can start writing correct scripts from day one. In most cases, there is no need to even know that there is an interpreter. Most of the time the user will simply enter single commands. The typical example is when you want to send the same command to all open windows. You don't have to write a loop. All you have to do is to check the option “For All Documents” on top of the console! Before version 2.2.5 the option was named “For All Windows”. With the same version there was also a more substantial addition: it became possible to mix Apple Script and Lua expressions. More exactly, iNMR is scriptable through Apple Script, and therefore driven from an external scripts (something impossible with Lua). The two new Apple Script commands that iNMR responds to (the standard "open", "print"... have always been handled) are: "Lua" to send Lua code directly and "dofile" to tell iNMR to run a Lua file. The consequences are: first, that chemists can write an Apple Script without having to learn the language and second, those who know the language can write a script to drive multiple applications, iNMR included. Another article describes these new additions. The first way you can customize your Lua environment is by changing the vocabulary. Let's say, for example, that you don't like the spelling “color” and prefer “colour” instead. All you have to do is to write:

colour = color

and from that moment on you can use both terms. You can also delete a command completely (and free a little of memory). To eliminate color, type:

color = nil

If you have already declared a synonym, like we did, the command persists under it. A more useful application of this rule is when you create a meaningful mnemonic for a variable. For example, the color red is identified, like all colors, by a table of three components. The standard syntax to draw a spectrum in red is:

color {r=1; g=0; b=0}

but if you have defined:

red = {r=1; g=0; b=0}

the complete command becomes more readable indeed:

color red

You will probably prefer to extract your names from a different vocabulary, if English is not your first language. The problem is that, when you close a console, all is lost. You'd want to write all you (re-)definitions into a file. It's a necessity to declare a folder where iNMR can start searching Lua scripts from. You specify this folder in the first panel of the Preferences dialog. All Lua scripts must end with the suffix ".lua". iNMR, at launch time, creates a menu with an entry for each script file. You can create subfolders: iNMR will create corresponding submenus. Such scripts can be called in 3 ways. From the console, with the command:

run “filename_without_the_extension”

or, more simply, selecting the corresponding menu item. The existence of the first method is to allow the inclusion of a file into another file at run time. The third solution is to create a shortcut button at the bottom of the console. You declare the names of these buttons (in the Preferences dialog again) and iNMR will display them each time it opens a console. The choice of names is limited to single words. A Lua file with the same name must of course exist in the designated folder. An important (optional) file will be called "Lua.init". It will not appear into the menus (because of the improper extension) and will be automatically executed whenever a new console is created. This is the place where you'll write all your initialization code (like declaring new synonyms, variables, functions, etc.. or showing a welcome message):

empty console

“Benvenuto” is the Italian for “welcome”.

At this point you may wonder: where does a Lua program start and where does it end? All the files you write contain spare pieces of code. The concept of "program" is not ideal to describe what happens. What you have is actually a "session": each new statements adds something to the proceedings, until you close the console window. Quite rude, not a difficult concept.

--> Lua Programs For You

Brukerization and Varianization

If you are the only guy in the lab willing to learn iNMR, customization is the solution to force others to use it. Let's say they know the Varian software very well. You can initialize, into “Lua.init”, all the common Varian parameters, like fn, lb, etc... and also define functions called ft, wft, aph, dc, etc.. They can't say they don't know the commands, after that!

Copyright © 2005-2021 nucleomatica
Valid XHTML and CSS. UTF-8 encoding.