Debian Codegraph @

docs/command-examples.rst

raw
1 .. _command-examples:
2
3 Command Examples
4 ================
5
6 Here are some useful commands for creating custom menu items, global
7 shortcuts and automatically process new clipboard content in CopyQ.
8
9 If you want to use any of the commands below, copy it to clipboard and
10 paste it to the command list in Command dialog (opened with F6
11 shortcut). For detailed info see :ref:`faq-share-commands`.
12
13 All these and more commands are available at
14 `CopyQ command repository <https://github.com/hluk/copyq-commands>`__.
15
16 Join Selected Items
17 ~~~~~~~~~~~~~~~~~~~
18
19 Creates new item containing concatenated text of selected items.
20
21 .. code-block:: ini
22
23 [Command]
24 Name=Join Selected Items
25 Command=copyq add -- %1
26 InMenu=true
27 Icon=\xf066
28 Shortcut=Space
29
30 Paste Current Date and Time
31 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
32
33 Copies current date/time text to clipboard and pastes to current window
34 on global shortcut Win+Alt+T.
35
36 .. code-block:: ini
37
38 [Command]
39 Command="
40 copyq:
41 var time = dateString('yyyy-MM-dd hh:mm:ss')
42 copy('Current date/time is ' + time)
43 paste()"
44 GlobalShortcut=meta+alt+t
45 Icon=\xf017
46 Name=Paste Current Time
47
48 Play Sound when Copying to Clipboard
49 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50
51 The following commands will play an audio file whenever something is
52 copied to the clipboard.
53
54 Built-in (Cross-Platform)
55 ^^^^^^^^^^^^^^^^^^^^^^^^^
56
57 The built-in audio capabilities are available since version 14.0.0 and supports
58 WAV, MP3 and FLAC formats on all platforms.
59
60 .. code-block:: ini
61
62 [Command]
63 Name=Play Sound on Copy
64 Command="
65 copyq:
66 playSound('/path/to/notification.wav')"
67 Automatic=true
68 Icon=\xf028
69
70 To play at a lower volume:
71
72 .. code-block:: ini
73
74 [Command]
75 Name=Play Sound on Copy (Quiet)
76 Command="
77 copyq:
78 playSound({file: '/path/to/notification.wav', volume: 25})"
79 Automatic=true
80 Icon=\xf028
81
82 Alternatively, if audio is not available in the app itself, you can use
83 external utilities to play audio file mentioned below.
84
85 Windows
86 ^^^^^^^
87
88 Plays the user-specified audio file whenever something is copied to the
89 clipboard. By default, this adds to the sound associated to native
90 notifications rather than replacing it, so you may want to use Qt
91 (non-native) notifications to use this.
92
93 .. code-block:: ini
94
95 [Command]
96 Name=Play Sound on Copy
97 Command="
98 powershell:
99 (New-Object Media.SoundPlayer \"C:\\Users\\copy.wav\").PlaySync()"
100 Automatic=true
101 Icon=\xf028
102
103 GNU/Linux
104 ^^^^^^^^^
105
106 Requires VLC multimedia player:
107
108 .. code-block:: ini
109
110 [Command]
111 Name=Play Sound on Copy
112 Command="
113 bash:
114 cvlc --play-and-exit ~/audio/example.mp3"
115 Automatic=true
116 Icon=\xf028
117
118 `Alternative using the mpv player <https://github.com/hluk/copyq-commands/blob/master/commands/play-sound-when-copying-to-clipboard-linux.ini>`__
119
120 Edit and Paste
121 ~~~~~~~~~~~~~~
122
123 Following command allows to edit current clipboard text before pasting
124 it. If the editing is canceled the text won't be pasted.
125
126 .. code-block:: ini
127
128 [Command]
129 Command="
130 copyq:
131 var text = dialog('paste', str(clipboard()))
132 if (text) {
133 copy(text)
134 copySelection(text)
135 paste()
136 }"
137 GlobalShortcut=ctrl+shift+v
138 Icon=\xf0ea
139 Name=Edit and Paste
140
141 Remove Background and Text Colors
142 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143
144 Removes background and text colors from rich text (e.g. text copied from
145 web pages).
146
147 Command can be both automatically applied on text copied to clipboard
148 and invoked from menu (or using custom shortcut).
149
150 .. code-block:: ini
151
152 [Command]
153 Automatic=true
154 Command="
155 copyq:
156 var html = str(input())
157 html = html.replace(/color\\s*:/g, 'xxx:')
158 setData('text/html', html)"
159 Icon=\xf042
160 InMenu=true
161 Input=text/html
162 Name=Remove Background and Text Colors
163
164 Linkify
165 ~~~~~~~
166
167 Stores an item with interactive link from plain text URL copied to clipboard.
168
169 .. code-block:: ini
170
171 [Command]
172 Automatic=true
173 Command="
174 copyq:
175 const link = str(input());
176 const href = `<a href=\"${link}\">${link}</a>`;
177 setData('text/html', href);"
178 Icon=\xf127
179 Input=text/plain
180 Match=^(https?|ftps?|file|mailto)://
181 Name=Linkify
182
183 Highlight Text
184 ~~~~~~~~~~~~~~
185
186 Highlight all occurrences of a text (change ``x = "text"`` to match
187 something else than ``text``).
188
189 .. code-block:: ini
190
191 [Command]
192 Name=Highlight Text
193 Command="
194 copyq:
195 x = 'text'
196 style = 'background: yellow; text-decoration: underline'
197
198 text = str(input())
199 x = x.toLowerCase()
200 lowertext = text.toLowerCase()
201 html = ''
202 a = 0
203 esc = function(a, b) {
204 return escapeHTML( text.substr(a, b - a) )
205 }
206
207 while (1) {
208 b = lowertext.indexOf(x, a)
209 if (b != -1) {
210 html += esc(a, b) + '<span>' + esc(b, b + x.length) + '</span>'
211 } else {
212 html += esc(a, text.length)
213 break
214 }
215 a = b + x.length;
216 }
217
218 tab( selectedtab() )
219 write(
220 index(),
221 'text/plain', text,
222 'text/html',
223 '<html><head><style>span{'
224 + style +
225 '}</style></head><body>'
226 + html +
227 '</body></html>'
228 )"
229 Input=text/plain
230 Wait=true
231 InMenu=true
232
233 Render HTML
234 ~~~~~~~~~~~
235
236 Render HTML code.
237
238 .. code-block:: ini
239
240 [Command]
241 Name=Render HTML
242 Match=^\\s*<(!|html)
243 Command="
244 copyq:
245 tab(selectedtab())
246 write(index() + 1, 'text/html', input())"
247 Input=text/plain
248 InMenu=true
249
250 Translate to English
251 ~~~~~~~~~~~~~~~~~~~~
252
253 Pass to text to `Google Translate <https://translate.google.com/>`__.
254
255 .. code-block:: ini
256
257 [Command]
258 Name=Translate to English
259 Command="
260 copyq:
261 text = str(input())
262 url = \"https://translate.google.com/#auto/en/???\"
263
264 x = url.replace(\"???\", encodeURIComponent(text))
265 html = '<html><head><meta http-equiv=\"refresh\" content=\"0;url=' + x + '\" /></head></html>'
266
267 tab(selectedtab())
268 write(index() + 1, \"text/html\", html)"
269 Input=text/plain
270 InMenu=true
271
272 Paste and Forget
273 ~~~~~~~~~~~~~~~~
274
275 Paste selected items and clear clipboard.
276
277 .. code-block:: ini
278
279 [Command]
280 Name=Paste and Forget
281 Command="
282 copyq:
283 tab(selectedtab())
284 items = selecteditems()
285 if (items.length > 1) {
286 text = ''
287 for (i in items)
288 text += read(items[i]);
289 copy(text)
290 } else {
291 select(items[0])
292 }
293
294 hide()
295 paste()
296 copy('')"
297 InMenu=true
298 Icon=\xf0ea
299 Shortcut=Ctrl+Return
300
301 Render Math Equations
302 ~~~~~~~~~~~~~~~~~~~~~
303
304 Render math equations using `MathJax <http://www.mathjax.org/>`__ (e.g.
305 ``$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}$$``).
306
307 .. code-block:: ini
308
309 [Command]
310 Name=Render Math Equations
311 Command="
312 copyq:
313 text = str(input())
314 js = 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
315
316 html = '<html><head><script type=\"text/javascript\" src=\"' + js + '\"></script></head><body>' + escapeHTML(text) + '</body></html>';
317
318 tab(selectedtab())
319 write(index() + 1, 'text/html', html)"
320 Input=text/plain
321 InMenu=true
322 Icon=\xf12b
323
324 Move Images to Other Tab
325 ~~~~~~~~~~~~~~~~~~~~~~~~
326
327 With this command active, images won't be saved in the first tab. This
328 can make application a bit more snappier since big image data won't need
329 to be loaded when main window is displayed or clipboard is stored for
330 the first time.
331
332 .. code-block:: ini
333
334 [Command]
335 Name=Move Images to Other Tab
336 Input=image/png
337 Automatic=true
338 Remove=true
339 Icon=\xf03e
340 Tab=&Images
341
342 Copy Clipboard to Window Tabs
343 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
344
345 Following command automatically adds new clipboard to tab with same name
346 as title of the window where copy operation was performed.
347
348 .. code-block:: ini
349
350 [Command]
351 Name=Window Tabs
352 Command="copyq:
353 item = unpack(input())
354 window_title = item[\"application/x-copyq-owner-window-title\"]
355 if (window_title) {
356 // Remove the part of window title before dash
357 // (it's usually document name or URL).
358 tabname = str(window_title).replace(/.* (-|\x2013) /, \"\")
359 tab(\"Windows/\" + tabname)
360 write(\"application/x-copyq-item\", input())
361 }
362 "
363 Input=application/x-copyq-item
364 Automatic=true
365 Icon=\xf009
366
367 Quickly Show Current Clipboard Content
368 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
369
370 Quickly pop up notification with text in clipboard using ``Win+Alt+C``
371 system shortcut.
372
373 .. code-block:: ini
374
375 [Command]
376 Name=Show clipboard
377 Command="
378 copyq:
379 seconds = 2;
380 popup(\"\", clipboard(), seconds * 1000)"
381 GlobalShortcut=Meta+Alt+C
382
383 Replace All Occurrences in Selected Text
384 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
385
386 .. code-block:: ini
387
388 [Command]
389 Command="
390 copyq:
391 copy();
392 const text = str(clipboard());
393 if (text) {
394 const r1 = 'Text';
395 const r2 = 'Replace with';
396 const reply = dialog(r1, '', r2, '');
397 if (reply) {
398 copy(text.replace(new RegExp(reply[r1], 'g'), reply[r2]));
399 paste();
400 }
401 }"
402 GlobalShortcut=meta+alt+r
403 Icon=
404 IsGlobalShortcut=true
405 Name=Replace in Selection
406
407 Copy Nth Item
408 ~~~~~~~~~~~~~
409
410 Copy item in row depending on which shortcut was pressed. E.g. Ctrl+2
411 for item in row "2".
412
413 .. code-block:: ini
414
415 [Command]
416 Name=Copy Nth Item
417 Command="
418 copyq:
419 var shortcut = str(data(\"application/x-copyq-shortcut\"));
420 var row = shortcut ? shortcut.replace(/^\\D+/g, '') : currentItem();
421 var itemIndex = (config('row_index_from_one') == 'true') ? row - 1 : row;
422 selectItems(itemIndex);
423 copy(\"application/x-copyq-item\", pack(getItem(itemIndex)));"
424 InMenu=true
425 Icon=\xf0cb
426 Shortcut=ctrl+1, ctrl+2, ctrl+3, ctrl+4, ctrl+5, ctrl+6, ctrl+7, ctrl+8, ctrl+9, ctrl+0
427
428 Edit Files
429 ~~~~~~~~~~
430
431 Opens files referenced by selected item in external editor (uses
432 "External editor command" from "History" config tab).
433
434 Works with following path formats (some editors may not support all of
435 these).
436
437 - ``C:/...``
438 - ``file://...``
439 - ``~...`` (some shells)
440 - ``%...%...`` (Windows environment variables)
441 - ``$...`` (environment variables)
442 - ``/c/...`` (gitbash)
443
444 .. code-block:: ini
445
446 [Command]
447 Name=Edit Files
448 Match=^([a-zA-Z]:[\\\\/]|~|file://|%\\w+%|$\\w+|/)
449 Command="
450 copyq:
451 var editor = config('editor')
452 .replace(/ %1/, '')
453
454 var filePaths = str(input())
455 .replace(/^file:\\/{2}/gm, '')
456 .replace(/^\\/(\\w):?\\//gm, '$1:/')
457 .split('\\n')
458
459 var args = [editor].concat(filePaths)
460
461 execute.apply(this, args)"
462 Input=text/plain
463 InMenu=true
464 Icon=\xf040
465 Shortcut=f4
466
467 Change Monitoring State Permanently
468 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
469
470 Disables clipboard monitoring permanently, i.e. the state is restored
471 when clipboard changes even after application is restarted.
472
473 Should be the first automatic command in the list of commands so other
474 commands are not invoked.
475
476 .. code-block:: ini
477
478 [Command]
479 Automatic=true
480 Command="
481 copyq:
482 var option = 'disable_monitoring'
483 var disabled = str(settings(option)) === 'true'
484
485 if (str(data('application/x-copyq-shortcut'))) {
486 disabled = !disabled
487 settings(option, disabled)
488 popup('', disabled ? 'Monitoring disabled' : 'Monitoring enabled')
489 }
490
491 if (disabled) {
492 disable()
493 ignore()
494 } else {
495 enable()
496 }"
497 GlobalShortcut=meta+alt+x
498 Icon=\xf05e
499 Name=Toggle Monitoring
500
501 Show Window Title
502 ~~~~~~~~~~~~~~~~~
503
504 Shows source application window title for new items in tag ("Tags"
505 plugin must be enabled in "Items" config tab).
506
507 .. code-block:: ini
508
509 [Command]
510 Automatic=true
511 Command="
512 copyq:
513 var window = str(data('application/x-copyq-owner-window-title'))
514 var tagsMime = 'application/x-copyq-tags'
515 var tags = str(data(tagsMime)) + ', ' + window
516 setData(tagsMime, tags)"
517 Icon=\xf009
518 Name=Store Window Title
519
520 Show Copy Time
521 ~~~~~~~~~~~~~~
522
523 Shows copy time of new items in tag ("Tags" plugin must be enabled in
524 "Items" config tab).
525
526 .. code-block:: ini
527
528 [Command]
529 Automatic=true
530 Command="
531 copyq:
532 var time = dateString('yyyy-MM-dd hh:mm:ss')
533 setData('application/x-copyq-user-copy-time', time)
534
535 var tagsMime = 'application/x-copyq-tags'
536 var tags = str(data(tagsMime)) + ', ' + time
537 setData(tagsMime, tags)"
538 Icon=\xf017
539 Name=Store Copy Time
540
541 Mark Selected Items
542 ~~~~~~~~~~~~~~~~~~~
543
544 Toggles highlighting of selected items.
545
546 .. code-block:: ini
547
548 [Command]
549 Command="
550 copyq:
551 var color = 'rgba(255, 255, 0, 0.5)'
552 var mime = 'application/x-copyq-color'
553
554 var firstSelectedItem = selectedItems()[0]
555 var currentColor = str(read(mime, firstSelectedItem))
556 if (currentColor != color)
557 setData(mime, color)
558 else
559 removeData(mime)"
560 Icon=\xf1fc
561 InMenu=true
562 Name=Mark/Unmark Items
563 Shortcut=ctrl+m
564
565 Change Upper/Lower Case of Selected Text
566 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
567
568 .. code-block:: ini
569
570 [Command]
571 Command="
572 copyq:
573 if (!copy())
574 abort()
575
576 var text = str(clipboard())
577
578 var newText = text.toUpperCase()
579 if (text == newText)
580 newText = text.toLowerCase()
581
582 if (text == newText)
583 abort();
584
585 copy(newText)
586 paste()"
587 GlobalShortcut=meta+ctrl+u
588 Icon=\xf034
589 Name=Toggle Upper/Lower Case
590
591
592
593 Change Copied Text to Title Case
594 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
595
596 .. code-block:: ini
597
598 [Command]
599 Name=Paste as title case
600 Command="
601 copyq:
602 function toTitleCase(str) {
603 return str.replace(
604 /\\w\\S*/g,
605 function(txt) {
606 return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
607 }
608 );
609 }
610 copy(toTitleCase(str(input())))
611 paste()
612 "
613 Input=text/plain
614 IsGlobalShortcut=true
615 HideWindow=true
616 Icon=\xf15b
617 GlobalShortcut=meta+ctrl+t