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
|
<html>
<head>
<title>Skencil User's Guide: Introduction</title>
</head>
<body bgcolor=white text=black link=blue vlink=navy alink=red>
<TABLE WIDTH="100%">
<TR>
<TH ALIGN="left" WIDTH="33%"><img SRC="Images/arrow-left.png" WIDTH="16" HEIGHT="16" ALIGN="top" ALT="Prev"></TH>
<TH ALIGN="center" WIDTH="33%"><img SRC="Images/arrow-up.png" WIDTH="16" HEIGHT="16" ALIGN="top" ALT="Up"></TH>
<TH ALIGN="right" WIDTH="33%"><img SRC="Images/arrow-right.png" WIDTH="16" HEIGHT="16" ALIGN="top" ALT="Next"></TH>
</TR>
<TR>
<TD ALIGN="left"><A HREF="usersguide-5.html">User Scripting
</A></TD>
<TD ALIGN="center"><A HREF="usersguide-5.html">User Scripting
</A></TD>
<TD ALIGN="right"><A HREF="usersguide-7.html">The Example Scripts</A></TD>
</TR>
</TABLE>
<HR NOSHADE>
<H2><FONT face="Helvetica,Arial"><A NAME="N1"></A>Introduction</font></H2>
<H3><FONT face="Helvetica,Arial"><A NAME="N2"></A>Safe and Advanced Scripts</font></H3>
<P>As mentioned above user scripts in Skencil can cause data loss. To make
things easier for you in this regard, Skencil supports two kinds of
user scripts, <EM>safe scripts</EM> and <EM>advanced scripts</EM>.</P>
<P><A NAME="N3"></A>Safe scripts take care of undo handling for you
and they restrict what your script can actually do to keep you from
harm. The disadvantage of this approach is that you can't do everything
you might want to do with a safe script.</P>
<P><A NAME="N4"></A>The advanced script has no restrictions but
it has to take care of undo handling itself, which isn't difficult but
has to be done with care.</P>
<H3><FONT face="Helvetica,Arial"><A NAME="N5"></A>The Script Function</font></H3>
<P>Regardless of whether a script is a safe script or an advanced script it
is just a Python function that accepts a context object as parameter.
The context is an instance with three attributes:</P>
<P>
<DL>
<DT><B><CODE>document</CODE></B><DD>
<P>The document for which the script was called.</P>
<P>The document contains all objects that make up the drawing and
it manages the selection.</P>
<DT><B><CODE>application</CODE></B><DD>
<P>The application object.</P>
<P>The application object has methods for setting and retrieving
the application's clipboard and to pop-up message boxes and file
dialogs.</P>
<DT><B><CODE>main_window</CODE></B><DD>
<P>The top-level window containing the
canvas widget that shows the current drawing. It has methods for
loading and saving documents, among others.</P>
</DL>
</P>
<P>A simple 'hello world' type script might look like this:
<table width="100%" cellpadding="10"><tr><td bgcolor="#FFFFD0">
<PRE>
def hello_world(context):
context.application.MessageBox(title = "My Script",
message = "Hello World!")
</PRE>
</td></tr></table>
</P>
<H3><FONT face="Helvetica,Arial"><A NAME="N6"></A>Registering Scripts</font></H3>
<P>The <b>Script</b> menu just shows all the scripts in the <EM>script
registry</EM>, so to have a script appear in that menu, you have to put it
into the registry. </P>
<P>The registry is managed in a subpackage <CODE>Scripting</CODE> of the toplevel
package <CODE>Sketch</CODE> (The name of the package is a leftover from the time
Skencil was called `Sketch', in case you're wondering). The function
needed here is <tt>AddFunction</tt> and is called like this:</P>
<P><CODE>AddFunction(<i>name</i>, <i>title</i>, <i>function</i>)</CODE></P>
<P>
<DL>
<DT><B><CODE><i>name</i></CODE></B><DD>
<P>is a name for that script. This name should be
unique, as the scripts are identified in the registry by this
name. It doesn't have to be the same as the function name.</P>
<DT><B><CODE><i>title</i></CODE></B><DD>
<P>The text of the menu entry.</P>
<DT><B><CODE><i>function</i></CODE></B><DD>
<P>the function that implements the
script, e.g. <CODE>hello_world</CODE> above.</P>
</DL>
</P>
<P>There are a few optional keyword arguments, one of which is <i>type</i>.
The value of <i>script_type</i> must be either <CODE>SafeScript</CODE> (the
default) or <CODE>AdvancedScript</CODE>. Both values are defined in the
<CODE>Scripting</CODE> subpackage. The registry functions are covered in more
detail in the <A HREF="usersguide-8.html">script registry section</A>.</P>
<P>As an example, registering the hello world script might look like this:
<table width="100%" cellpadding="10"><tr><td bgcolor="#FFFFD0">
<PRE>
import Sketch.Scripting
Sketch.Scripting.AddFunction("hello_world", "Hello World", hello_world)
</PRE>
</td></tr></table>
</P>
<P>First we import the package <CODE>Sketch.Scripting</CODE> and then we call the
<CODE>AddFunction</CODE> function.</P>
<P>For more information about the organization of Skencil's code have a
look at the <A HREF="usersguide-9.html">API overview</A>.</P>
<H3><FONT face="Helvetica,Arial"><A NAME="N7"></A>The Startup Script</font></H3>
<P>The only thing left to do is to get some user supplied code to run on
startup, so it can define and register scripts. To do that just create a
file <tt>userhooks.py</tt> in the directory <tt>~/.sketch/</tt>. If such a
file exists it is automatically executed when Skencil starts. More
precisely, the directory <tt>~/.sketch/</tt> is inserted at the front of
Python's modules search path and a module <tt>userhooks.py</tt> is
imported.</P>
<P>You can put arbitrary code in there not just scripts, but one way to
implement and register our <CODE>hello_world</CODE> script would be a
<tt>userhooks.py</tt> file like this:</P>
<P>
<table width="100%" cellpadding="10"><tr><td bgcolor="#FFFFD0">
<PRE>
# example for ~/.sketch/userhooks.py
def hello_world(context):
context.application.MessageBox(title = "My Script",
message = "Hello World!")
# register script
import Sketch.Scripting
Sketch.Scripting.AddFunction("hello_world", "Hello World", hello_world)
</PRE>
</td></tr></table>
</P>
<HR NOSHADE>
<TABLE WIDTH="100%">
<TR>
<TD ALIGN="left"><A HREF="usersguide-5.html">User Scripting
</A></TD>
<TD ALIGN="center"><A HREF="usersguide-5.html">User Scripting
</A></TD>
<TD ALIGN="right"><A HREF="usersguide-7.html">The Example Scripts</A></TD>
</TR>
<TR>
<TH ALIGN="left" WIDTH="33%"><img SRC="Images/arrow-left.png" WIDTH="16" HEIGHT="16" ALIGN="top" ALT="Prev"></TH>
<TH ALIGN="center" WIDTH="33%"><img SRC="Images/arrow-up.png" WIDTH="16" HEIGHT="16" ALIGN="top" ALT="Up"></TH>
<TH ALIGN="right" WIDTH="33%"><img SRC="Images/arrow-right.png" WIDTH="16" HEIGHT="16" ALIGN="top" ALT="Next"></TH>
</TR>
</TABLE>
</body>
</html>
|