Debian Codegraph @

docs/writing-commands-and-adding-functionality.rst

raw
1 .. _writing-commands:
2
3 Writing Commands and Adding Functionality
4 =========================================
5
6 CopyQ allows you to extend its functionality through commands in
7 following ways:
8
9 - Add custom commands to context menu for selected items in history.
10 - Run custom commands automatically when clipboard changes.
11 - Assign global/system-wide shortcuts to custom commands.
12
13 Here are some examples what can be achieved by using commands:
14
15 - Automatically store web links or other types of clipboard content in
16 special tabs to keep the history clean.
17 - Paste current date and time or modified clipboard on a global
18 shortcut.
19 - Pass selected items or clipboard to external application (e.g. web
20 browser or image editor).
21 - Keep TODO lists and tag items as "important" or use custom tags.
22 - See :ref:`command-examples` for some other ideas and useful commands.
23
24 Command Dialog
25 --------------
26
27 You can create new commands in Command dialog. To open the dialog
28 either:
29
30 a. Press default shortcut F6.
31 b. Select menu item "Commands/Global Shortcuts..." in "File" menu.
32
33 Command dialog contains:
34
35 - list of custom commands on the left
36 - settings for currently selected command on the right
37 - command filter text field at the top
38 - buttons to modify the command list (add, remove and move commands) at the top
39 - buttons to save, load, copy and paste commands at the bottom
40
41 Create New Command
42 ~~~~~~~~~~~~~~~~~~
43
44 To create new command click the "Add" button in Command dialog. This
45 opens list with predefined commands.
46
47 "New Command" creates new empty command (but it won't do anything
48 without being configured). One of the most frequently used predefined
49 command is "Show/hide main window" which allows you to assign global
50 shortcut for showing and hiding CopyQ window.
51
52 If you double click a predefined command (or select one or multiple
53 commands and click OK) it will be added to list of commands. The right
54 part of the Command dialog now shows the configuration for the new
55 command.
56
57 For example, for the "Show/hide main window" you'll most likely need to
58 change only the "Global Shortcut" option so click on the button next to
59 it and press the shortcut you want to assign.
60
61 Commands can be quickly disabled by clicking the check box next to them
62 in command list.
63
64 By clicking on "OK" or "Apply" button in the dialog all commands will be
65 saved permanently.
66
67 Command Options
68 ^^^^^^^^^^^^^^^
69
70 The following options can be set for commands.
71
72 If unsure what an option does, hover mouse pointer over it and tool tip
73 with description will appear.
74
75 Name
76 ''''
77
78 Name of the command. This is used in context menu if "In Menu" check box
79 is enabled. Use the pipe character ``|`` in the name to create sub-menus.
80
81 Group: Type of Action
82 '''''''''''''''''''''
83
84 This group sets the main type of the command. Usually only one
85 sub-option is set.
86
87 Automatic
88 """""""""
89
90 If enabled, the command is triggered whenever clipboard changes.
91
92 Automatic items are run in order they appear in the command list. No
93 other automatic commands will be run if a triggered automatic command
94 has "Remove Item" option set or calls ``copyq ignore``.
95
96 The command is **applied on current clipboard data** - i.e. options
97 below access text or other data in clipboard.
98
99 In Menu
100 """""""
101
102 If enabled, the command can be run from main window either with
103 application shortcut, from context menu or "Item" menu. The command can
104 be also run from tray menu.
105
106 Shortcuts can be assigned by clicking on the button next to the option.
107 These **application shortcuts work only while CopyQ window has focus**.
108
109 If the command is run from **tray menu**, it is **applied on clipboard
110 data**, otherwise it's **applied on data in selected items**.
111
112 Global Shortcut
113 """""""""""""""
114
115 Global or system shortcut is a keyboard shortcut that **works even if the main
116 application window is not focused**.
117
118 If enabled, the command is triggered whenever assigned shortcut is pressed.
119
120 This command is **not applied on data** in clipboard nor selected items.
121
122 .. _command-dialog-script:
123
124 Script
125 """"""
126
127 If enabled, the command is script which is loaded before any other script is
128 started. This allows overriding existing functions and creating new ones
129 (allowing new command line arguments to be used).
130
131 See :ref:`commands-script`.
132
133 .. _command-dialog-display:
134
135 Display
136 """""""
137
138 If enabled, the command is used to modify item data before displaying. Use
139 ``data()`` to retrieve current item data and ``setData()`` to modify the data
140 to display (these are not stored permanently).
141
142 See :ref:`commands-display`.
143
144 Group: Match Items
145 ''''''''''''''''''
146
147 This group is visible only for "Automatic" or "In Menu" commands.
148 Sub-options specify when the command can be used.
149
150 1. Content
151 """"""""""
152
153 `Regular expression <https://doc.qt.io/qt-4.8/qregexp.html#introduction>`__
154 to match text of selected items (for "In Menu" command) or clipboard
155 (for "Automatic" command).
156
157 For example, ``^https?://`` will match simple web addresses (text
158 starting with ``http://`` or ``https://``).
159
160 2. Window
161 """""""""
162
163 `Regular expression <https://doc.qt.io/qt-4.8/qregexp.html#introduction>`__
164 to match window title of active window (only for "Automatic" command).
165
166 For example, ``- Chromium$`` or ``Mozilla Firefox$`` to match some web
167 browser window titles (``$`` in the expression means end of the title).
168
169 3. Filter
170 """""""""
171
172 A command for validating text of selected items (for "In Menu" command)
173 or clipboard (for "Automatic" command).
174
175 If the command exits with non-zero exit code it won't be shown in
176 context menu and automatically triggered on clipboard change.
177
178 Example, ``copyq: if (tab().indexOf("Web") == -1) fail()`` triggers the
179 command only if tab "Web" is available.
180
181 4. Format
182 """""""""
183
184 Match format of selected items or clipboard.
185
186 The data of this format will be sent to **standard input** of the
187 command - this doesn't apply if the command is triggered with global
188 shortcut.
189
190 Command
191 '''''''
192
193 The command to run.
194
195 This can contain either:
196
197 a. simple command line (e.g. ``copyq popup %1`` - expression ``%1`` means text of the selected item or clipboard)
198 b. input for command interpreter (prefixed with ``bash:``, ``powershell:``, ``python:`` etc.)
199 c. CopyQ script (prefixed with ``copyq:``)
200
201 You can use ``COPYQ`` environment variable to get path of application
202 binary.
203
204 Current CopyQ session name is stored in ``COPYQ_SESSION_NAME``
205 environment variable (see :ref:`sessions`).
206
207 Example (call CopyQ from Python):
208
209 .. code-block:: python
210
211 python:
212 import os
213 from subprocess import call
214 copyq = os.environ['COPYQ']
215 call([copyq, 'read', '0'])
216
217 Example (call CopyQ from PowerShell on Windows):
218
219 ::
220
221 powershell:
222 $Item1 = (& "$env:COPYQ" read 0 | Out-String)
223 echo "First item: $Item1"
224
225 Group: Action
226 '''''''''''''
227
228 This group is visible only for "Automatic" or "In Menu" commands.
229
230 1. Copy to tab
231 """"""""""""""
232
233 Creates new item in given tab.
234
235 2. Remove Item
236 """"""""""""""
237
238 Removes selected items. If enabled for "Automatic" command, the
239 clipboard will be ignored and no other automatic commands will be
240 executed.
241
242 Group: Menu Action
243 ''''''''''''''''''
244
245 This group is visible only for "In Menu" commands.
246
247 1. Hide main window after activation
248 """"""""""""""""""""""""""""""""""""
249
250 If enabled, main window will be hidden after the command is executed.
251
252 Group: Command options
253 ''''''''''''''''''''''
254
255 This group is visible only for "Automatic" or "In Menu" commands.
256
257 1. Wait
258 """""""
259
260 Show action dialog before applying options below.
261
262 2. Transform
263 """"""""""""
264
265 Modify selected items - i.e. remove them and replace with **standard
266 output** of the command.
267
268 3. Output
269 """""""""
270
271 Format of **standard output** to save as new item.
272
273 4. Separator
274 """"""""""""
275
276 Separator for splitting output to multiple items (``\n`` to split
277 lines).
278
279 5. Output tab
280 """""""""""""
281
282 Tab for saving the output of command.
283
284 Save and Share Commands
285 ~~~~~~~~~~~~~~~~~~~~~~~
286
287 You can back up or share commands by saving them in a file ("Save
288 Selected Commands..." button) or by copying them to clipboard.
289
290 The saved commands can be loaded back to command list ("Load
291 Commands..." button) or pasted to the list from clipboard.
292
293 You can try some examples by copying commands from :ref:`command-examples`.