3.4.  Your First Script-Fu Script

Do you not need to stop and catch your breath? No? Well then, let's proceed with your fourth lesson -- your first Script-Fu Script.

Now, let's register the function with GIMP. This is done by calling the function <code>script-fu-register</code>. When GIMP reads in a script, it will execute this function, which registers the script with the procedural database. You can place this function call wherever you wish in your script, but I usually place it at the end, after all my other code.

Here's the listing for registering this function (I will explain all its parameters in a minute):

        (script-fu-register
          "script-fu-text-box"                        ;func name
          "Text Box"                                  ;menu label
          "Creates a simple text box, sized to fit\
            around the user's choice of text,\
            font, font size, and color."              ;description
          "Michael Terry"                             ;author
          "copyright 1997, Michael Terry"             ;copyright notice
          "October 27, 1997"                          ;date created
          ""                     ;image type that the script works on
          SF-STRING      "Text:"         "Text Box"   ;a string variable
          SF-FONT        "Font:"         "Charter"    ;a font variable
          SF-ADJUSTMENT  "Font size"     '(50 1 1000 1 10 0 1)
                                                      ;a spin-button
          SF-COLOR       "Color:"        '(0 0 0)     ;color variable
        )
        (script-fu-menu-register "script-fu-text-box" "<Toolbox>/Xtns/Script-Fu/Text")
      

If you save these functions in a text file with a .scm suffix in your script directory, then choose Xtns->Script-Fu->Refresh Scripts, this new script will appear as Xtns->Script-Fu->Text->Text Box.

If you invoke this new script, it won't do anything, of course, but you can view the prompts you created when registering the script (more information about what we did is covered next).

Finally, if you invoke the Procedure Browser ( Xtns->Procedure Browser), you'll notice that our script now appears in the database.

To register our script with GIMP, we call the function script-fu-register, fill in the seven required parameters and add our script's own parameters, along with a description and default value for each parameter.

The Required Parameters

  • The name of the function we defined. This is the function called when our script is invoked (the entry-point into our script). This is necessary because we may define additional functions within the same file, and GIMP needs to know which of these functions to call. In our example, we only defined one function, text-box, which we registered.

  • The location in the menu where the script will be inserted. The exact location of the script is specified like a path in Unix, with the root of the path being either toolbox or right-click.

    If your script does not operate on an existing image (and thus creates a new image, like our Text Box script will), you'll want to insert it in the toolbox menu -- this is the menu in GIMP's main window (where all the tools are located: the selection tools, magnifying glass, etc.).

    If your script is intended to work on an image being edited, you'll want to insert it in the menu that appears when you right-click on an open image. The rest of the path points to the menu lists, menus and sub-menus. Thus, we registered our Text Box script in the Text menu of the Script-Fu menu of the Xtns menu of the toolbox ( Xtns->Script-Fu->Text->Text Box ).

    If you notice, the Text sub-menu in the Script-Fu menu wasn't there when we began -- GIMP automatically creates any menus not already existing.

  • A description of your script, to be displayed in the Procedure Browser.

  • Your name (the author of the script).

  • Copyright information.

  • The date the script was made, or the last revision of the script.

  • The types of images the script works on. This may be any of the following: RGB, RGBA, GRAY, GRAYA, INDEXED, INDEXEDA. Or it may be none at all -- in our case, we're creating an image, and thus don't need to define the type of image on which we work.

[Note] Note

Beside the above parameter types there are more types for the interactive mode, each of them will create a widget in the control dialog. You will find the description of these parameters and examples in the test script plug-ins/script-fu/scripts/test-sphere.scm shipped with the GIMP source code.

Param Type

Description

SF-ADJUSTMENT

In interactive mode it creates an adjustment widget in the dialog.

SF-ADJUSTMENT "label" '(value lower upper step_inc page_inc digits type)

"label" : Text printed before the widget.

value : Value print at the start.

lower / upper : the lower / upper values (range of choice).

step_inc : increment/decrement value.

page_inc : increment/decrement value using page key.

digits : digits after the point (decimal part).

type : is one of: SF-SLIDER or 0, SF-SPINNER or 1

SF-COLOR

Creates a color button in the dialog.

SF-COLOR "label" '(red green blue)

or

SF-COLOR "label" "color"

"label" : Text printed before the widget.

'(red green blue) : list of three values for the red, green and blue components.

"color" : a color name in CSS notatation.

SF-FONT

Creates a font-selection widget in the dialog. It returns a fontname as a string. There are two new gimp-text procedures to ease the use of this return parameter:

(gimp-text-fontname image drawable x-pos y-pos text border antialias size unit font)

(gimp-text-get-extents-fontname text size unit font)

where font is the fontname you get. The size specified in the fontname is silently ignored. It is only used in the font-selector. So you are asked to set it to a useful value (24 pixels is a good choice).

SF-FONT "label" "fontname"

"label" : Text printed before the widget.

"fontname" : name of the défault font.

SF-BRUSH

It will create a widget in the control dialog. The widget consists of a preview area (which when pressed will produce a popup preview ) and a button with the "..." label. The button will popup a dialog where brushes can be selected and each of the characteristics of the brush can be modified.

SF-BRUSH "Brush" '("Circle (03)" 100 44 0)

Here the brush dialog will be popped up with a default brush of Circle (03) opacity 100 spacing 44 and paint mode of Normal (value 0).

If this selection was unchanged the value passed to the function as a parameter would be '("Circle (03)" 100 44 0).

SF-PATTERN

It will create a widget in the control dialog. The widget consists of a preview area (which when pressed will produce a popup preview ) and a button with the "..." label. The button will popup a dialog where patterns can be selected.

SF-PATTERN "Pattern" "Maple Leaves"

The value returned when the script is invoked is a string containing the pattern name. If the above selection was not altered the string would contain "Maple Leaves".

SF-GRADIENT

It will create a widget in the control dialog. The widget consists of a button containing a preview of the selected gradient.

If the button is pressed a gradient selection dialog will popup.

SF-GRADIENT "Gradient" "Deep Sea"

The value returned when the script is invoked is a string containing the gradient name. If the above selection was not altered the string would contain "Deep Sea".

Cliquer sur celui-ci fait apparaître la boite de dialogue des dégradés.

SF-PALETTE

It will create a widget in the control dialog. The widget consists of a button containing the name of the selected palette.

If the button is pressed a palette selection dialog will popup.

SF-PALETTE "Palette" "Named Colors"

The value returned when the script is invoked is a string containing the palette name. If the above selection was not altered the string would contain "Named Colors".

Cliquer sur celui-ci fait apparaître la boite de dialogue des palettes.

SF-FILENAME

It will create a widget in the control dialog. The widget consists of a button containing the name of a file.

If the button is pressed a file selection dialog will popup.

SF-FILENAME "Environment Map" (string-append "" gimp-data-directory "/scripts/beavis.jpg")

The value returned when the script is invoked is a string containing the filename.

SF-DIRNAME

Only useful in interactive mode. Very similar to SF-FILENAME, but the created widget allows to choose a directory instead of a file.

SF-DIRNAME "Image Directory" "/var/tmp/images"

The value returned when the script is invoked is a string containing the dirname.

SF-OPTION

It will create a widget in the control dialog. The widget is a combo-box showing the options that are passed as a list.

The first option is the default choice.

SF-OPTION "Orientation" '("Horizontal" "Vertical")

The value returned when the script is invoked is the number of the chosen option, where the option first is counted as 0.

SF-ENUM

It will create a widget in the control dialog. The widget is a combo-box showing all enum values for the given enum type. This has to be the name of a registered enum, without the "Gimp" prefix. The second parameter speficies the default value, using the enum value's nick.

SF-ENUM "Interpolation" '("InterpolationType" "linear")

The value returned when the script is invoked corresponds to chosen enum value.



[3] This section is not part of the original tutorial.