File: drscript.txt

package info (click to toggle)
drpython 1%3A3.11.4-1.1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 3,868 kB
  • ctags: 2,871
  • sloc: python: 16,653; makefile: 141; sh: 1
file content (143 lines) | stat: -rw-r--r-- 4,414 bytes parent folder | download
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
================
 DrScript
================

.. _The Gimp: http://www.gimp.org/

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 :guilabel:`Add Existing Script`.

To start from scratch, select :guilabel:`Add New Script` to add a new
script to the menu, then open it for editing. (You will have
to select the filename).

To add a Shell Command, select :guilabel:`Add Shell Command`. Note that:

- *Current Directory* is replaced with the directory of the
  currently open file.
- *Current File* is replaced with the current open file.
- *Current File* can only be used as an argument.
- You can either run the command in the prompt (default),
  or run it using :func:`wx.Shell()`.

You can select :guilabel:`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.

Whether you select :guilabel:`Add New Script` or
:guilabel:`Add Existing Script`, you must select a title for your script.
This is what you will see on the :guilabel:`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! It is advisable to make the
first line or so read

.. code-block:: python

  #drscript

just as a reminder.

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.

.. code-block:: python

  #drscript
  DrDocument.SetSelectedText(DrDocument.GetSelectedText() + " 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 code in the
DrScript exactly as if it were written into the source of
DrPython itself! The difference is that there are special
names DrPython recognizes.

You can choose from:

================= ==============================
:obj:`DrFrame`    which gives access to DrPython internals (:class:`DrFrame`)
:obj:`DrScript`   an object attached to :obj:`DrFrame` to hold persistant variables.
:obj:`DrFilename` the current filename (of the active tab if in mdi mode)
:obj:`DrDocument` gives access to the Document :class:`wx.StyledTextControl`
:obj:`DrPrompt`   gives access to the Prompt :class:`wx.StyledTextControl`
================= ==============================

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

.. code-block:: python

  wx.TextEntryDialog(DrFrame, "Replace What?:", "Replace All In Selection", "")

In other words, it is perfect for functions that require a
:class:`wx.Frame` as an argument.

You can also access DrPython internals

.. code-block:: python

  Frame.txtDocument.GetTextLength()

Of course, you could also write

.. code-block:: python

  DrDocument.GetTextLength()

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

.. code-block:: python

  #drscript
  #SetWhoRoars

  import wx

  d = wx.TextEntryDialog(DrFrame, "Who Roars?", "Determine Who Roars", "")
  if (d.ShowModal() == wx.ID_OK):
    DrScript.WhoRoars = d.GetValue()

and in the second:

.. code-block:: python

  #drscript
  #AddRoar

  if hasattr(DrScript, "WhoRoars"):
      DrDocument.SetSelectedText(DrDocument.GetSelectedText() + " roared the " + DrScript.WhoRoars)
  else:
      DrDocument.SetSelectedText(DrDocument.GetSelectedText() + " roared the Mouse")

  # Alternative:
  new_text = DrDocument.GetSelectedText() + " roared the " + getattr(DrScript, "WhoRoars", "Mouse")
  DrDocument.SetSelectedText(new_text)

You can also set the text for either the prompt or document.
For example:

.. code-block:: python

  #drscript
  #AddWithDucks

  DrPrompt.SetText(DrDocument.GetSelectedText() + " with ducks!")

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