Accessing Individual Spectra from a Lua program
When you write a script for iNMR, it's like you are writing directly into the console. You don't specify the document (window) you want to operate onto and in normal cases this is fine, because the foremost window is implicitly assumed as the target of your commands. If you want something moderately more powerful, you can activate the option “For all documents”. In this case each command is executed for each document before the next statement is parsed. Unfortunately, some commands ignore this option. What is worse, if you create your own commands, they are also unaffected (but each command you invoke will have, internally, an implicit loop). A programmer needs, instead, the freedom to decide how long a loop should be.
All you can do, with iNMR scripting, is to switch to the next window, with the command:
next_window()
. This becomes a powerful building block indeed, when combined
with the commands “open()” and “pathtofile()”.
The latter returns the unix path to the original file that contains the data points.
Under normal circumstances, each window points to a different file, and we are going to exploit
this unicity.
to loop over all documents
first = pathtofile()
repeat
your_command_here() -- this is the loop core
other_statements() -- substitute these lines
....
next_window() -- switch
until pathtofile() == first -- cycle is complete
to reach the document corresponding to a given path
function safe_switch( path )
open( path )
for i = 1,100 do
next_window()
if pathtofile() == path then
break
end
end
end
function freezing_switch( path )
-- path must point to the file “fid” or “ser” (containing the data points)
-- otherwise the following is an endless loop
open( path )
repeat
next_window()
until pathtofile() == path
end
to generate a list of open documents
List = {}
L[1] = pathtofile()
NumberOfDocuments = 100
for i = 2,NumberOfDocuments do
next_window() -- switch
L[i] = pathtofile()
if L[i] == L[1] then
L[i] = nil
NumberOfDocuments = i - 1
break
end
end