EDIT: Tutorial updated on March 20, 2009 to work in GIMP 2.6.
In this tutorial, I'll show you how to create a script, Mad Lib style! As I'm sure you already know, Mad Libs is the fun game where you fill in the blanks with requested information and when you're done, you get a funny storyline. (For those of you who have never played Mad Libs before, you can read about it here: http://en.wikipedia.org/wiki/Mad_Libs
). The results from our "Mad Lib" script won't be a funny story, but a very usable script created by you, for you. If the subject of scripting scares you to death, don't let it keep you from trying this tutorial. I promise it will be painless! There's a LOT of detail, but I've done the tutorial in such a fashion that you'll have a good time with it! There's also a money back guarantee....so what do you have to lose?
Here's how it works: Let's say that our husband and wife admin staff members, Darth_Gimp and Damascus, use the same computer with GIMP on it. Darth has certain settings he likes to use, but every time Damascus uses GIMP, she changes the settings to her favorites (and we all know Darth, who must have his settings, does the same thing in return to her!) These types of issues could put a significant strain on a marriage. Well, if we could put together a simple script so that Darth could have his settings and Damascus could have her's, life would be happy in the Darth/Damascus household. Who knew that GIMP scripting could keep a couple out of counseling?!
Well, that's what this tutorial is all about. I'll present a simple script that has several options for setting various defaults and all that you have to do is fill in the blanks. No scripting knowledge required! In fact, I'll walk you through the entire process. It's very simple.With the script in it's current format, you could do the following:
Set the foreground/background colors, the active brush, gradient, pattern & font.
Let's begin!Step 1 - Download the Template Script
Please download the template script from here (Click Me for MyDefaults Script!
) and open it up in your favorite text editor.Step 2 - Let's Evaluate the Script
If you were to save the current script to your GIMP scripts folder and Refresh the script, you would notice that there it shows up in the Image window under File > My Defaults. Clicking on the My Defaults option would do absolutely nothing. Why? Because I've disabled all of the actual "commands" by placing them behind a semi-colon, which is the Scheme language's way of allowing the programmer to place comments in the script. So, everything, with the exception of defining the function and registering it with the Procedural Database, is nothing but a comment. In order to make our script run, we'll need to delete the semi-colon from the desired commands and assign the necessary values.
For example....the command to change the foreground color in our template script looks like this (notice the semi-colon before the command - I've highlighted it in red):;
(gimp-context-set-foreground '(60 134 196))
So that GIMP will actually "see" it as a command, simply delete the semi-colon as shown:
(gimp-context-set-foreground '(60 134 196))Step 3 - Modifying the Script
Here I will show you how to modify the script to use every option I've set up. I'll also explain what to do if you don't want to use a particular option. Let's play Mad Lib scripting!
In each "command", I'll bold the options that need to be changed and give you a brief description on what to change it with.
In keeping with our storyline, it is assumed that Darth and Damascus will each create their own default scripts. However, in order for both of them to work correctly, each script has to have a unique function name registered with the Procedural Database. In fact, for any script to run, it must have a unique name. For those of you who are only doing one script for the time being, you can leave everything as is.
Assign a Unique Name to the Function
You can call these functions anything you want, but they need to be the same names in the define & register sections AND the register section needs to have the name in quotes! ALSO, the function names must be unique to the Procedural Database. So, if you're unsure if a desired function name is already registered with the Procedural Database, go to Help > Procedure Browser and type in the desired name. If it doesn't appear, you're good to go! Otherwise, modify the name to make it unique. Place the function name in the bold area below.
Set the Foreground/Background Colors
Here, we're going to set our foreground/background colors to new ones. My desired default foreground color is a pretty blue and the background color will be red, as shown in the next two commands. (Remember to delete the semi-colon in the script so that GIMP can read it.
(gimp-context-set-foreground '(60 134 196
(gimp-context-set-background '(255 0 0
Using the foreground command as an example, notice the numbers in parenthesis: 60 134 196
. The 60 represents the R-value, 134 the G-value, and 196 the B-value from the RGB color scheme. So, to find the values for your desired color, open up the Color selector dialog by double-clicking on the foreground color swatch, pick somewhere on your desired color, and get the appropriate R G B values and place them as shown (a space needs to be in-between each value).
Set the Brush
To set the default brush, open the brush dialog window, choose the desired brush and get it's name. I've chosen the Circle Fuzzy (17) brush.
Since this is a name, you must surround it with quotes as shown.
(gimp-context-set-brush "Circle Fuzzy (17)
Set the Gradient
I'd also like the German flag smooth gradient to be my default. Just like the brushes, get the name from the gradient dialog window.
Since this is a name, you must surround it with quotes as shown. The command will look like so:
(gimp-context-set-gradient "German flag smooth
Set the Pattern
Setting the pattern name is very similar to the brushes/gradients. Just open up the Pattern Dialog Window, select the desired pattern and you'll see the name of it at the top.
Put this name exactly as shown in the Pattern Dialog Window (without the pattern dimensions - for example 45 x 45) within quotes
(gimp-context-set-pattern "3D Green
Set the Font
I happen to like the Scriptina font. So, I look in my font window, copy down the name shown there and type it below as shown. If you don't have Scriptina or don't want to use it, obviously choose your desired font.
Since this is a name, you must surround it with quotes as shown.
Register the Script and Assign a Menu Location
Remember the Unique Name we assigned to the function above? Well, that same name
needs to be added to the Register section, which is near the bottom of the script as shown (the name MUST be in quotes)
The other thing they'll want to change is the Menu location. Currently it can be found here: "<Image>/File/My Defaults"
Darth could set his up as: "<Image>/File/Darth's Defaults
" and Dam's as "<Image>/File/Dam's Defaults
" That way, each of them knows where their script is located. You change the bold information to fit your situation.
At this point, nothing else needs to be changed! The script is ready for use. So let's do a little test run. Save your current script to your scripts folder, C:\Program Files\GIMP-2.X\share\gimp\2.0\scripts, add an "scm" extension to the end. Windows users: Windows has a nasty habit of adding a "txt" extension to the end of script files when using Notepad/WordPad. In Notepad, make sure you choose "All Files" under the Save As Type dropdown (don't use Text files) and in WordPad, make sure you use "Text Document" under the Save As Type Dropdown. I know it's a pain/confusing, but that's the way it is.
Then, refresh your scripts.
The script should be located here: Image Window: File > My Defaults (or at the Menu location you may have specified as described previously).
Hopefully, the changes you made above will return the correct values. If not, make sure you've made the changes correctly, resave, refresh, and try again.
That, my friends, is how we script, Mad Lib style! Maybe try setting up different defaults for different uses. Maybe one for large pieces or one for signatures, etc. However, remember, if you set up more than one, you need to give it a new function/register name and menu location as described above.
I hope you enjoyed this tutorial. If you run into any snags or want to try adding other options, let me know and I'll help you as best as I can.