-
Notifications
You must be signed in to change notification settings - Fork 449
Scripting
If you need to process items in some non-trivial way you can take advantage of the scripting interface the application provides. This is accessible on command line as copyq eval SCRIPT
or copyq -e SCRIPT
where SCRIPT
is string containing commands written in Javascript-similar scripting language (Qt Script with is ECMAScript scripting language).
Every command line option is available as function in the scripting interface. Command copyq help tab
can be written as copyq eval 'print(help("tab"))'
(note: print
is needed to print the return value of help("tab")
function call).
You can print each item with copyq read N
where N is item number from 0 to copyq size
(i.e. number of items in the first tab) and put item to clipboard with copyq select N
. With these commands it's possible to search items and copy the right one with a script. E.g. having file script.js
containing
var match = "MATCH-THIS";
var i = 0;
while (i < size() && str(read(i)).indexOf(match) === -1)
++i;
select(i);
and passing it to CopyQ using cat script.js | copyq eval -
will put first item containing "MATCH-THIS" string to clipboard.
By default commands and functions work with items in the first tab. Calling read(0, 1, 2)
will read first three items from the first tab. To access items in other tab you need to switch the current tab with tab("TAB_NAME")
(or copyq tab TAB_NAME
on command line) where TAB_NAME
is name of the tab.
For example to search for an item as in the previous script but in all tabs you'll have to run:
var match = "MATCH-THIS";
var Tabs = tab().split("\n");
for (var i in Tabs) {
tab(Tabs[i]);
var j = 0;
while (j < size() && str(read(j)).indexOf(match) === -1)
++j;
if (j < size())
print("Match in tab \"" + Tabs[i] + "\" item number " + j + ".\n");
}
As mentioned above, all command line options are also available for scripting e.g.: show()
, hide()
, toggle()
, copy()
, paste()
. Additionally there are following scripting functions:
-
str(...)
- Returns argument as string. -
print(...)
- Print string on standard output. -
input()
(from v2.1.0) - Returns bytes read from standard input. This is also the input passed to user commands. -
data(...)
(from v2.1.0) - Returns data for current command (clipboard data if command was run automatically on new clipboard content, otherwise data of selected items). Usedata("?")
to list available formats. For example:data("text/plain")
will return the data of thetext/plain
format.
Hint: With CopyQ 2.1.0 the syntax will be a bit easier see #156
-
UPPERCASE/lowercase
https://gist.github.com/m4r71n/6726a03d89fb23aee8c8
https://gist.github.com/m4r71n/2ab66d6592e36d767236 -
Date conversion
https://gist.github.com/m4r71n/6fd7452cba0151436639
https://gist.github.com/m4r71n/b988322cece3dfb483ee -
Strip whitespaces and linebreaks
https://gist.github.com/m4r71n/d93fe7fdca3f72e64a52