Debian Codegraph @

docs/scripting.rst

raw
1 Scripting
2 =========
3
4 If you need to process items in some non-trivial way you can take
5 advantage of the scripting interface the application provides. This is
6 accessible on command line as ``copyq eval SCRIPT`` or
7 ``copyq -e SCRIPT`` where ``SCRIPT`` is string containing commands
8 written in JavaScript-similar scripting language (see :ref:`scripting-api`).
9
10 Every command line option is available as function in the scripting
11 interface. Command ``copyq help tab`` can be written as
12 ``copyq eval 'print(help("tab"))'`` (note: ``print`` is needed to print
13 the return value of ``help("tab")`` function call).
14
15 Searching Items
16 ---------------
17
18 You can print each item with ``copyq read N`` where N is item number
19 from 0 to ``copyq size`` (i.e. number of items in the first tab) and put
20 item to clipboard with ``copyq select N``. With these commands it's
21 possible to search items and copy the right one with a script. E.g.
22 having file ``script.js`` containing
23
24 ::
25
26 var match = "MATCH-THIS";
27 var i = 0;
28 while (i < size() && str(read(i)).indexOf(match) === -1)
29 ++i;
30 select(i);
31
32 and passing it to CopyQ using ``cat script.js | copyq eval -`` will put
33 first item containing "MATCH-THIS" string to clipboard.
34
35 Working with Tabs
36 -----------------
37
38 By default commands and functions work with items in the first tab.
39 Calling ``read(0, 1, 2)`` will read first three items from the first
40 tab. To access items in other tab you need to switch the current tab
41 with ``tab("TAB_NAME")`` (or ``copyq tab TAB_NAME`` on command line)
42 where ``TAB_NAME`` is name of the tab.
43
44 For example to search for an item as in the previous script but in all
45 tabs you'll have to run:
46
47 ::
48
49 var match = "MATCH-THIS";
50 var tabs = tab();
51 for (var i in tabs) {
52 tab(tabs[i]);
53 var j = 0;
54 while (j < size() && str(read(j)).indexOf(match) === -1)
55 ++j;
56 if (j < size())
57 print("Match in tab \"" + tabs[i] + "\" item number " + j + ".\n");
58 }
59
60 Scripting Functions
61 -------------------
62
63 As mentioned above, all command line options are also available for
64 scripting e.g.: ``show()``, ``hide()``, ``toggle()``, ``copy()``,
65 ``paste()``.
66
67 Reference for available scripting functions and language features can be found
68 at :ref:`scripting-api`.