| 1 |
.. _commands-script: |
| 2 |
|
| 3 |
Script Commands |
| 4 |
=============== |
| 5 |
|
| 6 |
Script command is type of command which allows overriding existing functions |
| 7 |
and creating new ones (allowing new command line arguments to be used). |
| 8 |
|
| 9 |
The command is executed before any script and all defined variables and |
| 10 |
functions are available to the scripts. |
| 11 |
|
| 12 |
Script commands can be created in Command dialog by setting Type of Action to |
| 13 |
:ref:`command-dialog-script`. |
| 14 |
|
| 15 |
Extending Command Line Interface |
| 16 |
-------------------------------- |
| 17 |
|
| 18 |
By adding following script command you can use ``hello()`` from other script |
| 19 |
or on command line (``copyq hello``): |
| 20 |
|
| 21 |
.. code-block:: js |
| 22 |
|
| 23 |
global.hello = function() { |
| 24 |
print('Hello, World!\n') |
| 25 |
} |
| 26 |
|
| 27 |
Script commands are executed in own scope so as to avoid adding temporary |
| 28 |
variables in the global scope which contains all functions like ``copy()`` or |
| 29 |
``add()``. Using ``global`` object allows to modify the global scope. |
| 30 |
|
| 31 |
It's useful to move code used by multiple commands to a new script command. |
| 32 |
|
| 33 |
It can also simplify using ``copyq`` from another application or shell script. |
| 34 |
|
| 35 |
Override Functionality |
| 36 |
---------------------- |
| 37 |
|
| 38 |
Existing functions can be overridden from script commands. |
| 39 |
|
| 40 |
Specifically :any:`onClipboardChanged` and functions it calls can be |
| 41 |
overridden to customize handling of new clipboard content. |
| 42 |
|
| 43 |
E.g. following command saves only textual clipboard data and removes any |
| 44 |
formatted text: |
| 45 |
|
| 46 |
.. code-block:: js |
| 47 |
|
| 48 |
var saveData_ = saveData |
| 49 |
|
| 50 |
saveData = function() { |
| 51 |
if ( str(data(mimeText)) != "" ) { |
| 52 |
popup('Saving only text') |
| 53 |
removeData(mimeHtml) |
| 54 |
saveData_() |
| 55 |
} else { |
| 56 |
popup('Not saving non-textual data') |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
E.g. following command overrides ``paste()`` to use an external utility for |
| 61 |
pasting clipboard: |
| 62 |
|
| 63 |
.. code-block:: js |
| 64 |
|
| 65 |
paste = function() { |
| 66 |
var x = execute( |
| 67 |
'xdotool', |
| 68 |
'keyup', 'alt', 'ctrl', 'shift', 'super', 'meta', |
| 69 |
'key', 'shift+Insert') |
| 70 |
if (!x) |
| 71 |
throw 'Failed to run xdotool' |
| 72 |
if (x.stderr) |
| 73 |
throw 'Failed to run xdotool: ' + str(x.stderr) |
| 74 |
} |
| 75 |
|
| 76 |
E.g. show custom notifications for clipboard and primary selection changes. |
| 77 |
|
| 78 |
.. code-block:: js |
| 79 |
|
| 80 |
function clipboardNotification(owns, hidden) { |
| 81 |
var id = isClipboard() ? 'clipboard' : 'selection' |
| 82 |
var icon = isClipboard() ? '\uf0ea' : '\uf246' |
| 83 |
var owner = owns ? 'CopyQ' : str(data(mimeWindowTitle)) |
| 84 |
var title = id + ' - ' + owner |
| 85 |
var message = hidden ? '<HIDDEN>' : data(mimeText).left(100) |
| 86 |
notification( |
| 87 |
'.id', id, |
| 88 |
'.title', title, |
| 89 |
'.message', message, |
| 90 |
'.icon', icon |
| 91 |
) |
| 92 |
} |
| 93 |
|
| 94 |
var onClipboardChanged_ = onClipboardChanged |
| 95 |
onClipboardChanged = function() { |
| 96 |
clipboardNotification(false, false) |
| 97 |
onClipboardChanged_() |
| 98 |
} |
| 99 |
|
| 100 |
var onOwnClipboardChanged_ = onOwnClipboardChanged |
| 101 |
onOwnClipboardChanged = function() { |
| 102 |
clipboardNotification(true, false) |
| 103 |
onOwnClipboardChanged_() |
| 104 |
} |
| 105 |
|
| 106 |
var onHiddenClipboardChanged_ = onHiddenClipboardChanged |
| 107 |
onHiddenClipboardChanged = function() { |
| 108 |
clipboardNotification(true, true) |
| 109 |
onHiddenClipboardChanged_() |
| 110 |
} |