Setting Up iNMR to Run the Scripts
The console works like a command line where you can type commands,
either built-in or created by yourself.
You can also paste a whole script (a sequence of commands,
statements and expressions: a computer program) and execute it.
It is more efficient, however, if you create shortcuts for these scripts.
iNMR can create shortcuts automatically, if you tell it where your scripts are stored.
Scripts are interpreted rather than compiled.
The same commands that you type into the console can
therefore be stored in a script and executed later.
iNMR can be programmed in Lua and in Apple Script.
The console, however, only understands Lua, thanks to the embedded interpreter.
Lua was created by Roberto Ierusalimschy, Luiz Henrique de Figueiredo and Waldemar Celes at PUC-Rio (Rio de Janeiro, Brazil).
Lua comes with its rich library of functions to manipulate numbers and strings.
It is not necessary to learn the language to use a command or a script.
How to set up your computer:
- Create a folder to store all your scripts. They can be organized into subfolders.
- Choose iNMR > Settings. Select the first panel of the dialog.
- Click the button choose on the right. A standard file-selection dialog appears.
-
Select the folder you have created at step 1. The path appears in the Settings dialog.
The following steps are optional: -
You can define up to eight shortcut buttons that will appear in the console.
The name of a button will be the same as the name of the corresponding script, without the extension.
Write the list of shortcuts, at the bottom of the Settings dialog, separated by spaces.
If the script resides in a subfolder, prepend the name of the subfolder and a slash, ex.:
subfolder/name. - To define your own functions, to initialize some variables and to have some commands performed every time you open the console, place the initialization code in a file called Lua.init and place it at the top level of the folder. This file can be as long and complex as you need.
- If you wish to write your own scripts, any text editor will do. Popular choices include VS Code, BBEdit, Geany and Notepad++.
- To learn the Lua language, there's nothing better than the word of its creator.
iNMR populates the script menu, at startup, with all the files that end with the .lua extension and reside into the designated folder. Subfolders are listed as submenus. If you add a new file into the folder and iNMR is already running, the name will not appear in the menu. The new shortcut buttons, instead, appear the next time you open the console (no need to quit the program).
To run a script from the console, without a shortcut, type:
run “path/to/the/script”
(the base path is the scripts folder; omit the extension) or:
dofile “/absolute/path/to/the/script.lua”
Apple Script (Mac-only)
The Lua console can be controlled by an Apple Script.
You have two basic Apple Script commands: “dofile” to call an existing Lua script and “Lua”
to pass a Lua statement directly. Any text printed by the console appears in the AppleScript editor.
To specify a file, either pass the absolute path with the extension or
the path relative to the scripts folder, without extension.
To get the final result from the console you can also use “get message”,
while “set message to...” performs the inverse task (useful for debugging your scripts, for example).
You can also set and get the value of the option “for all documents”,
with the syntax: “set forall to true”, etc.
With Apple Script
you can include iNMR operations into a flow that involves more applications.
To test the mechanism, run the following code from inside the AppleScript editor:
-- the following examples show how to communicate between an AppleScript script and iNMR
tell application "iNMR"
-- setting a Lua variable from outside
setLuaNumber "x" value 2
setLuaString "name" value "Joe"
-- executing Lua statements
Lua "sum = x + x"
Lua "print('x + x = '..sum)"
Lua "print( name )"
-- getting values from iNMR
set aNumber to getLuaNumber "sum"
-- showing text into iNMR
Lua "print 'these words come through Lua...'"
end tell
display dialog aNumber buttons {"Cancel", "Ok"}
Related Topics
The Console: Command Line and Script Editor