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
|
<?xml version="1.0" ?>
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; " />
<meta name="generator" content="Docutils 0.3.8: http://docutils.sourceforge.net/" />
<title>DrScript</title>
<link rel="stylesheet" href="default.css" type="text/css" />
</head>
<body>
<div class="document" id="drscript">
<h1 class="title">DrScript</h1>
<p>Back to <a class="reference" href="help.html">Help</a></p>
<a class="target" id="top" name="top"></a><p>DrScript is vaguely modeled after Script-Fu in The Gimp
(a <em>VERY</em> powerful open source image manipulation program,
used on the images in DrPython).</p>
<p>You can add any Python file you want to the DrPython menu
under the heading DrScript. Simply select "Add Existing Script".</p>
<p>To start from scratch, select "Add New Script" to add a new
script to the menu, then open it for editing. (You will have
to select the filename).</p>
<p>To add a Shell Command, select "Add Shell Command". Note that:</p>
<ul class="simple">
<li><em>Current Directory</em> is replaced with the directory of the
currently open file.</li>
<li><em>Current File</em> is replaced with the current open file.</li>
<li><em>Current File</em> can only be used as an argument.</li>
<li>You can either run the command in the prompt (default),
or run it using wx.Shell().</li>
</ul>
<p>You can select "Dynamic DrScript" from the menu to type in a
DrScript right away (without saving it to a file). Dynamic
DrScripts are parsed in the same way as a saved DrScript.</p>
<p>Whether you select "Add New Script" or "Add Existing Script",
you must select a title for your script. This is what you will
see on the DrScript submenu.</p>
<p>You can also move scripts Around on the menu once you have
added them. (Moving scripts around updates and saves all of
your shortcuts.)</p>
<p>When shortcuts have been added to the menu, they can then be
seen in the customize shortcuts dialog, and you can bind keys
to your custom scripts.</p>
<p>Now you are ready to script! It is advisable to make the
first line or so read.. code-block:: Python</p>
<blockquote>
#drscript</blockquote>
<p>just as a reminder.</p>
<p>Now let's look at an example DrScript. Let's say you want to
write a script which adds "with ducks" to the selected text.</p>
<div class="code-block">
<span class="p_commentline">#drscript</span><span class="p_default"><br/>
</span><span class="p_identifier">DrDocument</span><span class="p_operator">.</span><span class="p_identifier">SetSelectedText</span><span class="p_operator">(</span><span class="p_identifier">DrDocument</span><span class="p_operator">.</span><span class="p_identifier">GetSelectedText</span><span class="p_operator">()</span><span class="p_default"> </span><span class="p_operator">+</span><span class="p_default"> </span><span class="p_string">" with ducks!"</span><span class="p_operator">)</span>
</div>
<p>First let's look at what this does. Let's say I select the
text "The Philosopher shares his epipheny". I then select
"Add With Ducks" from the DrScript submenu. Viola! The text
now reads "The Philosopher shares his epipheny with ducks!"</p>
<p>Back to the program. DrPython will run the code in the
DrScript exactly as if it were written into the source of
DrPython itself! The difference is that there are special
keywords DrPython recognizes.</p>
<p>You can choose from:</p>
<table border="1" class="docutils">
<colgroup>
<col width="21%" />
<col width="79%" />
</colgroup>
<tbody valign="top">
<tr><td>DrFrame</td>
<td>which gives access to DrPython internals (DrFrame)</td>
</tr>
<tr><td>DrScript</td>
<td>a wxObject attached to DrFrame to hold persistant variables.</td>
</tr>
<tr><td>DrFilename</td>
<td>the current filename (of the active tab if in mdi mode)</td>
</tr>
<tr><td>DrDocument</td>
<td>gives access to the Document wxStyledTextControl</td>
</tr>
<tr><td>DrPrompt</td>
<td>gives access to the Prompt wxStyledTextControl</td>
</tr>
</tbody>
</table>
<p>You could choose DrFrame for several reasons. If you want a
dialog, all you have to do is.. code-block:: Python</p>
<blockquote>
wx.TextEntryDialog(DrFrame, "Replace What?:", "Replace All In Selection", "")</blockquote>
<p>In other words, it is perfect for functions that require a
wxFrame as an argument.</p>
<p>You can also access DrPython internals.. code-block:: Python</p>
<blockquote>
Frame.txtDocument.GetTextLength()</blockquote>
<p>Of course, you could also write.. code-block:: Python</p>
<blockquote>
DrDocument.GetTextLength()</blockquote>
<p>Now if you wanted to set a variable in one script, then use
it in another, you would write in the first:</p>
<div class="code-block">
<span class="p_commentline">#drscript</span><span class="p_default"><br/>
</span><span class="p_commentline">#SetWhoRoars</span><span class="p_default"><br/>
<br/>
</span><span class="p_word">import</span><span class="p_default"> </span><span class="p_identifier">wx</span><span class="p_default"><br/>
<br/>
</span><span class="p_identifier">d</span><span class="p_default"> </span><span class="p_operator">=</span><span class="p_default"> </span><span class="p_identifier">wx</span><span class="p_operator">.</span><span class="p_identifier">TextEntryDialog</span><span class="p_operator">(</span><span class="p_identifier">DrFrame</span><span class="p_operator">,</span><span class="p_default"> </span><span class="p_string">"Who Roars?"</span><span class="p_operator">,</span><span class="p_default"> </span><span class="p_string">"Determine Who Roars"</span><span class="p_operator">,</span><span class="p_default"> </span><span class="p_string">""</span><span class="p_operator">)</span><span class="p_default"><br/>
</span><span class="p_word">if</span><span class="p_default"> </span><span class="p_operator">(</span><span class="p_identifier">d</span><span class="p_operator">.</span><span class="p_identifier">ShowModal</span><span class="p_operator">()</span><span class="p_default"> </span><span class="p_operator">==</span><span class="p_default"> </span><span class="p_identifier">wx</span><span class="p_operator">.</span><span class="p_identifier">ID_OK</span><span class="p_operator">):</span><span class="p_default"><br/>
</span><span class="p_identifier">DrScript</span><span class="p_operator">.</span><span class="p_identifier">WhoRoars</span><span class="p_default"> </span><span class="p_operator">=</span><span class="p_default"> </span><span class="p_identifier">d</span><span class="p_operator">.</span><span class="p_identifier">GetValue</span><span class="p_operator">()</span>
</div>
<p>and in the second:</p>
<div class="code-block">
<span class="p_commentline">#drscript</span><span class="p_default"><br/>
</span><span class="p_commentline">#AddRoar</span><span class="p_default"><br/>
<br/>
</span><span class="p_word">if</span><span class="p_default"> </span><span class="p_identifier">DrScript</span><span class="p_operator">.</span><span class="p_identifier">VariableExists</span><span class="p_operator">(</span><span class="p_string">"WhoRoars"</span><span class="p_operator">):</span><span class="p_default"><br/>
</span><span class="p_identifier">DrDocument</span><span class="p_operator">.</span><span class="p_identifier">SetSelectedText</span><span class="p_operator">(</span><span class="p_identifier">DrDocument</span><span class="p_operator">.</span><span class="p_identifier">GetSelectedText</span><span class="p_operator">()</span><span class="p_default"> </span><span class="p_operator">+</span><span class="p_default"> </span><span class="p_string">" roared the "</span><span class="p_default"> </span><span class="p_operator">+</span><span class="p_default"> </span><span class="p_identifier">DrScript</span><span class="p_operator">.</span><span class="p_identifier">WhoRoars</span><span class="p_operator">)</span><span class="p_default"><br/>
</span><span class="p_word">else</span><span class="p_operator">:</span><span class="p_default"><br/>
</span><span class="p_identifier">DrDocument</span><span class="p_operator">.</span><span class="p_identifier">SetSelectedText</span><span class="p_operator">(</span><span class="p_identifier">DrDocument</span><span class="p_operator">.</span><span class="p_identifier">GetSelectedText</span><span class="p_operator">()</span><span class="p_default"> </span><span class="p_operator">+</span><span class="p_default"> </span><span class="p_string">" roared the Mouse"</span><span class="p_operator">)</span>
</div>
<p>You can also set the text for either the Prompt or Document.
For example:</p>
<div class="code-block">
<span class="p_commentline">#drscript</span><span class="p_default"><br/>
</span><span class="p_commentline">#AddWithDucks</span><span class="p_default"><br/>
<br/>
</span><span class="p_identifier">DrPrompt</span><span class="p_operator">.</span><span class="p_identifier">SetText</span><span class="p_operator">(</span><span class="p_identifier">DrDocument</span><span class="p_operator">.</span><span class="p_identifier">GetSelectedText</span><span class="p_operator">()+</span><span class="p_default"> </span><span class="p_string">" with ducks!"</span><span class="p_operator">)</span>
</div>
<p>This code will set the prompt text to the document selection
plus the string "with ducks!".</p>
</div>
</body>
</html>
|