<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Script-fus</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Gaby's documentation" HREF="index.html"><LINK REL="UP" TITLE="Introduction" HREF="c139.html"><LINK REL="PREVIOUS" TITLE="misc" HREF="x224.html"><LINK REL="NEXT" TITLE="The embedded python" HREF="c274.html"></HEAD ><BODY CLASS="CHAPTER" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Gaby's documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="x224.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="c274.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="CHAPTER" ><H1 ><A NAME="INTERPRETERS" ></A >Chapter 1. Script-fus</H1 ><P >This chapter is intended to introduce how interpreters (with an 's' as in 'not only Python') can be used to extend Gaby capabilities.</P ><DIV CLASS="IMPORTANT" ><P ></P ><TABLE CLASS="IMPORTANT" WIDTH="100%" BORDER="0" ><TR ><TD WIDTH="25" ALIGN="CENTER" VALIGN="TOP" ><IMG SRC="../images/important.gif" HSPACE="5" ALT="Important"></TD ><TD ALIGN="LEFT" VALIGN="TOP" ><P >This is <SPAN CLASS="emphasis" ><I CLASS="EMPHASIS" >not</I ></SPAN > a documentation about writing script-fus in a particular language; even if most examples are written with Python.</P ></TD ></TR ></TABLE ></DIV ><P >It should supersede the <TT CLASS="FILENAME" >README</TT > in the Python plug-in subdirectory.</P ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="AEN235" >1.1. How to use script-fus ?</A ></H1 ><P >Script-fus can be used in several places to do several things.</P ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN238" >1.1.1. As an item in the 'Actions' menu</A ></H2 ><P >Script-fus can be run from the 'Actions' menu, once they are defined correctly in the descfile:</P ><DIV CLASS="EXAMPLE" ><A NAME="AEN241" ></A ><P ><B >Example 1-1. Script-fu action in a descfile</B ></P ><PRE CLASS="PROGRAMLISTING" >Begin actions Mail to... !script_fu( AddressBook:E-Mail ) #include mailto.py ... End</PRE ></DIV ><P >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 <TT CLASS="FILENAME" >mailto.py</TT > that may be sitting in <TT CLASS="FILENAME" >/usr/local/share/gaby/scripts/actions/</TT > or in <TT CLASS="FILENAME" >~/.gaby/scripts/</TT >.</P ><P >If the script writes to the screen, the output is catched and showed in a dialog box once the script is finished.</P ><DIV CLASS="EXAMPLE" ><A NAME="AEN249" ></A ><P ><B >Example 1-2. Script-fu writing to the screen</B ></P ><PRE CLASS="PROGRAMLISTING" ># 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())</PRE ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN252" >1.1.1. As a start-up script</A ></H2 ><P >Once launched Gaby looks in the scripts directories (see above) for a file name <TT CLASS="FILENAME" >autoexec.appname</TT > (ie <TT CLASS="FILENAME" >autoexec.gaby</TT > when using Gaby as gaby and <TT CLASS="FILENAME" >autoexec.gcd</TT > when using Gaby as gcd). If found it is executed. </P ><DIV CLASS="EXAMPLE" ><A NAME="AEN258" ></A ><P ><B >Example 1-3. Script-fu used as start-up script</B ></P ><PRE CLASS="PROGRAMLISTING" ># 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)</PRE ></DIV ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN261" >1.1.2. As a 'user-launched' script</A ></H2 ><P >When in 'Expert Mode' <A NAME="AEN264" HREF="#FTN.AEN264" ><SPAN CLASS="footnote" >[1]</SPAN ></A > 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.</P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN267" >1.1.3. As command-line script</A ></H2 ><P >Together with the <B CLASS="COMMAND" >gabyscript</B > it is possible to launch scripts without using the GUI; for example to perform automative tasks or to interface with other programs.</P ><DIV CLASS="EXAMPLE" ><A NAME="AEN271" ></A ><P ><B >Example 1-4. Command-line script-fu</B ></P ><PRE CLASS="PROGRAMLISTING" ># 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)</PRE ></DIV ></DIV ></DIV ></DIV ></DIV ><H3 CLASS="FOOTNOTES" >Notes</H3 ><TABLE BORDER="0" CLASS="FOOTNOTES" WIDTH="100%" ><TR ><TD ALIGN="LEFT" VALIGN="TOP" WIDTH="5%" ><A NAME="FTN.AEN264" HREF="c227.html#AEN264" ><SPAN CLASS="footnote" >[1]</SPAN ></A ></TD ><TD ALIGN="LEFT" VALIGN="TOP" WIDTH="95%" ><P >The <SPAN CLASS="emphasis" ><I CLASS="EMPHASIS" >only</I ></SPAN > 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 :)</P ></TD ></TR ></TABLE ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="x224.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="c274.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >misc</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c139.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >The embedded python</TD ></TR ></TABLE ></DIV ></BODY ></HTML >