1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>10. A Script-Fu Tutorial</title>
<link rel="stylesheet" href="gimp-help-plain.css" type="text/css" />
<link rel="stylesheet" href="gimp-help-screen.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.66.1" />
<link rel="start" href="index.html" title=" " />
<link rel="up" href="ch02.html" title="Chapter 2. Using Gimp" />
<link rel="prev" href="ch02s09.html" title="9. Using Script-Fu Scripts" />
<link rel="next" href="ch02s10s02.html" title="10.2. Variables And Functions" />
</head>
<body>
<div xmlns="" class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center" id="chaptername">10. A Script-Fu Tutorial</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="ch02s09.html">Prev</a> </td>
<th width="60%" align="center" id="sectionname">10. A Script-Fu Tutorial</th>
<td width="20%" align="right"> <a accesskey="n" href="ch02s10s02.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="gimp-using-script-fu-tutorial"></a>10. A Script-Fu Tutorial</h2>
</div>
</div>
</div>
<p>
In this training course, we'll introduce you to the fundamentals
of Scheme necessary to use Script-Fu, and then build a handy
script that you can add to your toolbox of scripts. The script
prompts the user for some text, then creates a new image sized
perfectly to the text. We will then enhance the script to allow
for a buffer of space around the text. We will conclude with a few
suggestions for ways to ramp up your knowledge of Script-Fu.
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25">
<img alt="[Note]" src="../images/note.png" />
</td>
<th align="left">Note</th>
</tr>
<tr>
<td colspan="2" align="left" valign="top">
<p>
This section as adapted from a tutorial written for the Gimp 1 User
Manual by Mike Terry.
</p>
</td>
</tr>
</table>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="id3316524"></a>10.1. Getting Acquainted With Scheme</h3>
</div>
</div>
</div>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3316532"></a>Let's Start Scheme'ing</h4>
</div>
</div>
</div>
<p>
The first thing to learn is that:
</p>
<p>
<span class="bold">
<b>
Every statement in Scheme is surrounded by parentheses ().
</b>
</span>
</p>
<p>
The second thing you need to know is that:
</p>
<p>
<span class="bold">
<b>
The function name/operator is always the first item in the
parentheses, and the rest of the items are parameters to the
function.
</b>
</span>
</p>
<p>
However, not everything enclosed in parentheses is a function --
they can also be items in a list -- but we'll get to that
later. This notation is referred to as prefix notation, because
the function prefixes everything else. If you're familiar with
postfix notation, or own a calculator that uses Reverse Polish
Notation (such as most HP calculators), you should have no
problem adapting to formulating expressions in Scheme.
</p>
<p>
The third thing to understand is that:
</p>
<p>
<span class="bold">
<b>
Mathematical operators are also considered functions, and thus
are listed first when writing mathematical expressions.
</b>
</span>
</p>
<p>
This follows logically from the prefix notation that we just
mentioned.
</p>
</div>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3316392"></a>Examples Of Prefix, Infix, And Postfix Notations</h4>
</div>
</div>
</div>
<p>
Here are some quick examples illustrating the differences
between <span class="emphasis"><em>prefix</em></span>,
<span class="emphasis"><em>infix</em></span>, and <span class="emphasis"><em>postfix</em></span>
notations. We'll add a 1 and 3 together:
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
Prefix notation: + 1 3 (the way Scheme will want it)
</p>
</li>
<li>
<p>
Infix notation: 1 + 3 (the way we "normally" write it)
</p>
</li>
<li>
<p>
Postfix notation: 1 3 + (the way many HP calculators will
want it)
</p>
</li>
</ul>
</div>
</div>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3316645"></a>Practicing In Scheme</h4>
</div>
</div>
</div>
<p>
Now, let's practice what we have just learned. Start up Gimp,
if you have not already done so, and choose
Xtns/Script-Fu/Console. This will start up the Script-Fu
Console window, which allows us to work interactively in
Scheme. In a matter of moments, the Script-Fu Console will
appear:
</p>
</div>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3316396"></a>The Script-Fu Console Window</h4>
</div>
</div>
</div>
<p>
At the bottom of this window is an entry-field entitled
Current Command. Here, we can test out simple Scheme commands
interactively. Let's start out easy, and add some numbers:
</p>
<pre class="programlisting">
(+ 3 5)
</pre>
<p>
Typing this in and hitting Return yields the expected answer
of 8 in the center window.
</p>
<p>
Now, what if we wanted to add more than one number? The "+"
function can take two or more arguments, so this is not a
problem:
</p>
<pre class="programlisting">
(+ 3 5 6)
</pre>
<p>
This also yields the expected answer of 14.
</p>
<p>
So far, so good -- we type in a Scheme statement and it's
executed immediately in the Script-Fu Console window. Now for
a word of caution....
</p>
</div>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3316355"></a>Watch Out For Extra Parens</h4>
</div>
</div>
</div>
<p>
If you're like me, you're used to being able to use extra
parentheses whenever you want to -- like when you're typing a
complex mathematical equation and you want to separate the
parts by parentheses to make it clearer when you read it. In
Scheme, you have to be careful and not insert these extra
parentheses incorrectly. For example, say we wanted to add 3
to the result of adding 5 and 6 together:
</p>
<pre class="programlisting">
3 + (5 + 6) + 7= ?
</pre>
<p>
Knowing that the + operator can take a list of numbers to add,
you might be tempted to convert the above to the following:
</p>
<pre class="programlisting">
(+ 3 (5 6) 7)
</pre>
<p>
However, this is incorrect -- remember, every statement in
Scheme starts and ends with parens, so the Scheme interpreter
will think that you're trying to call a function named "5" in
the second group of parens, rather than summing those numbers
before adding them to 3.
</p>
<p>
The correct way to write the above statement would be:
</p>
<pre class="programlisting">
(+ 3 (+ 5 6) 7)
</pre>
</div>
<div class="simplesect" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="id3316852"></a>Make Sure You Have The Proper Spacing, Too</h4>
</div>
</div>
</div>
<p>
If you are familiar with other programming languages, like
C/C++, Perl or Java, you know that you don't need white space
around mathematical operators to properly form an expression:
</p>
<pre class="programlisting">
<tt class="literal">3+5, 3 +5, 3+ 5</tt>
</pre>
<p>
These are all accepted by C/C++, Perl and Java
compilers. However, the same is not true for Scheme. You must
have a space after a mathematical operator (or any other
function name or operator) in Scheme for it to be correctly
interpreted by the Scheme interpreter.
</p>
<p>
Practice a bit with simple mathematical equations in the
Script-Fu Console until you're totally comfortable with these
initial concepts.
</p>
</div>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="ch02s09.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="ch02.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="ch02s10s02.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">9. Using Script-Fu Scripts </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> 10.2. Variables And Functions</td>
</tr>
</table>
</div>
</body>
</html>
|