9.8. Scripts

9.8.1. Scripts from the user's point of view

In TEA you can use scripts to process a selected text or a whole text, just as you use a built-in TEA functions. If the script can handle some parameters, put them into the Famous text entry.

TEA can work with scripts those written in Python, Perl, Ruby and Bash (Sh). To install a script, just copy him to $HOME/.config/tea/scripts. If a script is packed, please unpack it first. TEA will "see" the newly installed scripts after restart.

9.8.2. Scripts from the developer's point of view

How to write a script for TEA? It's a quite simple. But the first thing that you must to know it is how TEA gives a text to the script and how TEA takes a processed text back to replace the selection with it.

TEA runs each script with one or two parameters. Both of them are file names. The first file contains the text. This text is a selected of a whole text that TEA takes from the current document.

And the second file (if exists) contains a text from the Famous text entry. So your script can read the first file to obtain the text data, and read the second file to get some optional parameters for your script.

Be careful with a text handling - TEA internally operates with UTF-8 encoding, so all text processing at the script musb be UTF-8-safe. OK, now let's see how a script can return a processed text. Just write that processed text to the first file again, i.e. to the file, which file name you take from the first parameter of your script.

Below this lines you shall see an example of UTF8-safe Python-script that takes a text, swaps the case, and returns the processed text back to the TEA.

import sys
import string
import codecs
f = codecs.open(sys.argv[1], "r", "utf-8" )
u = f.read()
f.close
u = u.swapcase()
f = codecs.open(sys.argv[1], "w+", "utf-8" )
f.write (u)
f.close

sys.argv[1] contains a name of file with the text from TEA. We read the whole content of that file, then we process this content, and then we write it back to the sys.argv[1]-file. Voila! Please note how to use the codecs.

And another example - the "inline" calculator. Here we don't use any codecs because we work with a numerical data:

import sys
f = file (sys.argv[1], 'r')
s = f.read()
f.close
t = eval (s)
f = file (sys.argv[1], 'w+')
f.write (str (t))
f.close

But what if we need to get some additional user's parameters to the script? They are available as a string, and you can read it from the file that named at sys.argv[2] (in Python). At Bash-script, use $1 to get the first parameter, and $2 for the second. Let me explain one more time. The second parameter is a file which contains a text from the Famous text entry. Note that Ruby scripts takes the first parameter in ARGV[0], and the second in ARGV[1].

If you wish to contribute some scripts to the TEA site scripts repository, please consider to contribute your script with a public domain status. Or at least not with the proprietaty license. There is also will be useful to put the credits and the description somewhere in the comments within your script.