Back to DrPython Help

DrScript

DrScript is vaguely modeled after Script-Fu in The Gimp
(A VERY powerful open source image manipulation program,
used on the images in DrPython).

You can add any python file you want to the DrPython menu
under the heading DrScript (Simply select "Add Script").

To start from scratch (with a healthy boost), select
"New Script" to add a new script to the menu, then
open it for editing (You will have to select the filename).

Whether you select "New Script" or "Add Script", you must
select a title for your script.  This is what you will see
on the DrScript submenu.

You can also Move Scripts Around on the menu once you have
added them.  (Moving Scripts Around Updates and Saves
All of Your Shortcuts.)

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.

Now you are ready to script!
For anything interesting to happen,
the first two lines of any python program accessed from
DrScript should read as follows:

#drscript
#definition:

Let's look at an example:

Let's say you want a function that adds "with ducks" to the selected text:

#drscript
#definition:AddWithDucks
#arguments:Document_Selection
#returns:Document_Selection

def AddWithDucks(Document_Selection):
       return (Document_Selection + " with ducks!")

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!"

Back to the program:

DrPython will run the function named directly following the colon after "definition":
#definition:AddWithDucks

tells DrPython to look for a function named "AddWithDucks", and run it.

"#arguments:" tells drpython what arguments to send the function.

You can choose from:
"Frame":                             which gives access to DrPython internals (DrFrame)

"DrScript":                          a wxObject attached to DrFrame to hold persistant variables.

"Filename":                        the current filename (of the active tab if in mdi mode)

"Document":                        which gives access to the Document wxStyledTextControl
"Document_Text":               which gives access to all of the text in the file text control
"Document_Selection":       which gives access to the currently selected text in the file text control

"Prompt":                        which gives access to the Prompt wxStyledTextControl
"Prompt_Text":               which gives access to all of the text in the prompt text control
"Prompt_Selection":       which gives access to the currently selected text in the prompt text control

You could choose Frame for several reasons.  If you want a dialog, all you have to do is:

wxTextEntryDialog(frame, "Replace What?:", "Replace All In Selection", "")

In other words, it is perfect for functions that require a wxFrame as an argument.

You can also access DrPython internals:
Frame.txtDocument.GetTextLength()

Now if you wanted to set a variable in one script, then use it in another, you would write:

#drscript
#definition:SetWhoRoars
#arguments:DrFrame, DrScript

def SetWhoRoars(DrScript):
       d = wxTextEntryDialog(Frame, "Who Roars?", "Determine Who Roars", "")
       if (d.ShowModal() == wxID_OK):
            DrScript.WhoRoars = d.GetValue()

#drscript
#definition:AddRoar
#arguments:DrScript, Document_Selection
#returns:
Document_Selection

def AddRoar(DrScript, Document_Selection):
       if DrScript.VariableExists("WhoRoars"):
                return Document_Selection + " roared the " + DrScript.WhoRoars
       else:
                return Document_Selection + " roared the Mouse"

"#returns" is different.  You can return "_Text" or "_Selection" (but not both).
For either Prompt or Document.

For example:
#drscript
#definition:AddWithDucks
#arguments:Document_Selection
#returns:Document_Text

def AddWithDucks(Document_Selection):
       return (Document_Selection + " with ducks!")

This code will set the document text to the selection plus the string " with ducks!".

A few more words on format:

All you really need is for the string "\n#definition:" to appear in the program with the name
of the function you wish to run.  ("\n" simply means the string starts at the beginning of
a line).

Back to DrPython Help