Chapter 8. Script-fus

This chapter is intended to introduce how interpreters (with an 's' as in 'not only Python') can be used to extend Gaby capabilities.

Important: This is not a documentation about writing script-fus in a particular language; even if most examples are written with Python.

It should supersede the README in the Python plug-in subdirectory.

How to use script-fus ?

Script-fus can be used in several places to do several things.

As an item in the 'Actions' menu

Script-fus can be run from the 'Actions' menu, once they are defined correctly in the descfile:

Example 8-1. Script-fu action in a descfile

Begin actions
        Mail to...
                !script_fu( AddressBook:E-Mail )
                #include mailto.py
        ...
End

This defines a 'Mail to' action (that will certainly launch a mail user agent) that take one arg (the E-Mail field from the AddressBook table) and is located in mailto.py that may be sitting in /usr/local/share/gaby/scripts/actions/ or in ~/.gaby/scripts/.

If the script writes to the screen, the output is catched and showed in a dialog box once the script is finished.

Example 8-2. Script-fu writing to the screen

# Interpreter: Python
# Descfiles: *
# Description: show the number of records in the current window

print 'Number of records: ',
print len(get_current_window().subtable.fast_records_list())

As a start-up script

Once launched Gaby looks in the scripts directories (see above) for a file name autoexec.appname (ie autoexec.gaby when using Gaby as gaby and autoexec.gcd when using Gaby as gcd). If found it is executed.

Example 8-3. Script-fu used as start-up script

# Interpreter: Python

# this script is an alternative for a great part
# of the misc section of desc.gaby

main_win = get_main_window()
st = get_subtable_by_name('Phone Book')
xlist_win = st.create_new_window( 'xlist' )

main_win.resize(400, 500)

main_win.add_bound_window(xlist_win)
xlist_win.add_bound_window(main_win)

As a 'user-launched' script

When in 'Expert Mode' [1] there is a 'Home-brewed script' menu item in the 'Actions' menu. It allows you to select a script to run in a file selection box.

As command-line script

Together with the gabyscript it is possible to launch scripts without using the GUI; for example to perform automative tasks or to interface with other programs.

Example 8-4. Command-line script-fu

# Interpreter: python
# Descfiles: gaby
# Description: Sets the country field of every records

# This script sets the country fields of every records to 'Belgium' if no
# country was previously given.

subtable = get_subtable_by_name('Address Book')
field_number = subtable.dict_fields['Country']

for i in subtable.fast_records_list():
        record = subtable.get_record_no(i)
        if ( len(record[field_number]) == 0 ):
                record[field_number] = 'Belgium'
        subtable.set_record_no(i, record)

Notes

[1]

The only way to move to the so-called 'Expert Mode' is by editing your Gabyrc and adding 'expert_mode TRUE' in the 'common:misc' section. If you don't know how to do that you're not an expert and don't need this mode :)