File: scripting-api.rst

package info (click to toggle)
copyq 3.7.3-1~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 10,480 kB
  • sloc: cpp: 51,894; sh: 734; python: 211; xml: 57; makefile: 34
file content (1396 lines) | stat: -rw-r--r-- 34,293 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
.. _scripting-api:

Scripting API
=============

CopyQ provides scripting capabilities to automatically handle clipboard
changes, organize items, change settings and much more.

In addition to features provided by Qt Script there are following
`functions`_, `types`_, `objects`_ and `MIME types`_.

Execute Script
--------------

The scripts can be executed:

-  from commands (in Action or Command dialogs -- F5, F6 shortcuts) if
   the first line starts with ``copyq:``,
-  from command line as ``copyq eval '<SCRIPT>'``,
-  from command line as ``cat script.js | copyq eval -``,
-  from command line as
   ``copyq <SCRIPT_FUNCTION> <FUNCTION_ARGUMENT_1> <FUNCTION_ARGUMENT_2> ...``.

When run from command line, result of last expression is printed on
stdout.

Command exit values are:

-  0 - script finished without error,
-  1 - ``fail()`` was called,
-  2 - bad syntax,
-  3 - exception was thrown.

Command Line
------------

If number of arguments that can be passed to function is limited you can
use

.. code-block:: bash

    copyq <FUNCTION1> <FUNCTION1_ARGUMENT_1> <FUNCTION1_ARGUMENT_2> \
              <FUNCTION2> <FUNCTION2_ARGUMENT> \
                  <FUNCTION3> <FUNCTION3_ARGUMENTS> ...

where ``<FUNCTION1>`` and ``<FUNCTION2>`` are scripts where result of
last expression is functions that take two and one arguments
respectively.

E.g.

.. code-block:: bash

    copyq tab clipboard separator "," read 0 1 2

After ``eval`` no arguments are treated as functions since it can access
all arguments.

Arguments recognize escape sequences ``\n`` (new line), ``\t``
(tabulator character) and ``\\`` (backslash).

Argument ``-e`` is identical to ``eval``.

Argument ``-`` is replaced with data read from stdin.

Argument ``--`` is skipped and all the remaining arguments are
interpreted as they are (escape sequences are ignored and ``-e``, ``-``,
``--`` are left unchanged).

Functions
---------

Argument list parts ``...`` and ``[...]`` are optional and can be
omitted.

.. js:function:: String version()

   Returns version string.

.. js:function:: String help()

   Returns help string.

.. js:function:: String help(searchString, ...)

   Returns help for matched commands.

.. js:function:: show()

   Shows main window.

.. js:function:: show(tabName)

   Shows tab.

.. js:function:: showAt()

   Shows main window under mouse cursor.

.. js:function:: showAt(x, y, [width, height])

   Shows main window with given geometry.

.. js:function:: showAt(x, y, width, height, tabName)

   Shows tab with given geometry.

.. js:function:: hide()

   Hides main window.

.. js:function:: bool toggle()

   Shows or hides main window.

   Returns true only if main window is being shown.

.. js:function:: menu()

   Opens context menu.

.. js:function:: menu(tabName, [maxItemCount, [x, y]])

   Shows context menu for given tab.

   This menu doesn't show clipboard and doesn't have any special actions.

   Second argument is optional maximum number of items. The default value
   same as for tray (i.e. value of ``config('tray_items')``).

   Optional arguments x, y are coordinates in pixels on screen where menu
   should show up. By default menu shows up under the mouse cursor.

.. js:function:: exit()

   Exits server.

.. js:function:: disable(), enable()

   Disables or enables clipboard content storing.

.. js:function:: bool monitoring()

   Returns true only if clipboard storing is enabled.

.. js:function:: bool visible()

   Returns true only if main window is visible.

.. js:function:: bool focused()

   Returns true only if main window has focus.

.. js:function:: filter(filterText)

   Sets text for filtering items in main window.

.. js:function:: String filter()

   Returns current text for filtering items in main window.

.. js:function:: ignore()

   Ignores current clipboard content (used for automatic commands).

   This does all of the below.

   -  Skips any next automatic commands.
   -  Omits changing window title and tray tool tip.
   -  Won't store content in clipboard tab.

.. js:function:: ByteArray clipboard([mimeType])

   Returns clipboard data for MIME type (default is text).

   Pass argument ``"?"`` to list available MIME types.

.. js:function:: ByteArray selection([mimeType])

   Same as ``clipboard()`` for Linux/X11 mouse selection.

.. js:function:: bool hasClipboardFormat(mimeType)

   Returns true only if clipboard contains MIME type.

.. js:function:: bool hasSelectionFormat(mimeType)

   Same as ``hasClipboardFormat()`` for Linux/X11 mouse selection.

.. js:function:: bool isClipboard()

   Returns true only in automatic command triggered by clipboard change.

   This can be used to check if current automatic command was triggered by
   clipboard and not Linux/X11 mouse selection change.

.. js:function:: copy(text)

   Sets clipboard plain text.

   Same as ``copy(mimeText, text)``.

.. js:function:: copy(mimeType, data, [mimeType, data]...)

   Sets clipboard data.

   This also sets ``mimeOwner`` format so automatic commands are not run on
   the new data and it's not stored in clipboard tab.

   Exception is thrown if clipboard fails to be set.

   Example (set both text and rich text):

   .. code-block:: js

       copy(mimeText, 'Hello, World!',
            mimeHtml, '<p>Hello, World!</p>')

.. js:function:: copy()

   Sends ``Ctrl+C`` to current window.

   Exception is thrown if clipboard doesn't change (clipboard is reset
   before sending the shortcut).

.. js:function:: copySelection(...)

   Same as ``copy(...)`` for Linux/X11 mouse selection.

.. js:function:: paste()

   Pastes current clipboard.

   This is basically only sending ``Shift+Insert`` shortcut to current
   window.

   Correct functionality depends a lot on target application and window
   manager.

.. js:function:: String[] tab()

   Returns array of tab names.

.. js:function:: tab(tabName)

   Sets current tab for the script.

   E.g. following script selects third item (index is 2) from tab "Notes".

   .. code-block:: js

       tab('Notes')
       select(2)

.. js:function:: removeTab(tabName)

   Removes tab.

.. js:function:: renameTab(tabName, newTabName)

   Renames tab.

.. js:function:: String tabIcon(tabName)

   Returns path to icon for tab.

.. js:function:: tabIcon(tabName, iconPath)

   Sets icon for tab.

.. js:function:: count(), length(), size()

   Returns amount of items in current tab.

.. js:function:: select(row)

   Copies item in the row to clipboard.

   Additionally, moves selected item to top depending on settings.

.. js:function:: next()

   Copies next item from current tab to clipboard.

.. js:function:: previous()

   Copies previous item from current tab to clipboard.

.. js:function:: add(text|item...)

   Same as ``insert(0, ...)``.

.. js:function:: insert(row, text|item...)

   Inserts new items to current tab.

   Throws an exception if space for the items cannot be allocated.

.. js:function:: remove(row, ...)

   Removes items in current tab.

   Throws an exception if some items cannot be removed.

.. js:function:: edit([row|text] ...)

   Edits items in current tab.

   Opens external editor if set, otherwise opens internal editor.

.. js:function:: ByteArray read([mimeType])

   Same as ``clipboard()``.

.. js:function:: ByteArray read(mimeType, row, ...)

   Returns concatenated data from items, or clipboard if row is negative.

   Pass argument ``"?"`` to list available MIME types.

.. js:function:: write(row, mimeType, data, [mimeType, data]...)

   Inserts new item to current tab.

   Throws an exception if space for the items cannot be allocated.

.. js:function:: change(row, mimeType, data, [mimeType, data]...)

   Changes data in item in current tab.

   If data is ``undefined`` the format is removed from item.

.. js:function:: String separator()

   Returns item separator (used when concatenating item data).

.. js:function:: separator(separator)

   Sets item separator for concatenating item data.

.. js:function:: action()

   Opens action dialog.

.. js:function:: action(row, ..., command, outputItemSeparator)

   Runs command for items in current tab.

.. js:function:: popup(title, message, [time=8000])

   Shows popup message for given time in milliseconds.

   If ``time`` argument is set to -1, the popup is hidden only after mouse
   click.

.. js:function:: notification(...)

   Shows popup message with icon and buttons.

   Each button can have script and data.

   If button is clicked the notification is hidden and script is executed
   with the data passed as stdin.

   The function returns immediately (doesn't wait on user input).

   Special arguments:

   -  '.title' - notification title
   -  '.message' - notification message (can contain basic HTML)
   -  '.icon' - notification icon (path to image or font icon)
   -  '.id' - notification ID - this replaces notification with same ID
   -  '.time' - duration of notification in milliseconds (default is -1,
      i.e. waits for mouse click)
   -  '.button' - adds button (three arguments: name, script and data)

   Example:

   .. code-block:: js

       notification(
             '.title', 'Example',
             '.message', 'Notification with button',
             '.button', 'Cancel', '', '',
             '.button', 'OK', 'copyq:popup(input())', 'OK Clicked'
             )

.. js:function:: exportTab(fileName)

   Exports current tab into file.

   Throws an exception if export fails.

.. js:function:: importTab(fileName)

   Imports items from file to a new tab.

   Throws an exception if import fails.

.. js:function:: exportData(fileName)

   Exports all tabs and configuration into file.

   Throws an exception if export fails.

.. js:function:: importData(fileName)

   Imports all tabs and configuration from file.

   Throws an exception if import fails.

.. js:function:: String config()

   Returns help with list of available application options.

.. js:function:: String config(optionName)

   Returns value of given application option.

   Throws an exception if the option is invalid.

.. js:function:: String config(optionName, value)

   Sets application option and returns new value.

   Throws an exception if the option is invalid.

.. js:function:: String config(optionName, value, ...)

   Sets multiple application options and return list with values in format
   ``optionName=newValue``.

   Throws an exception if there is an invalid option in which case it won't
   set any options.

.. js:function:: bool toggleConfig(optionName)

   Toggles an option (true to false and vice versa) and returns the new value.

.. js:function:: String info([pathName])

   Returns paths and flags used by the application.

   E.g. following command prints path to configuration file.

   .. code-block:: bash

       copyq info config

.. js:function:: Value eval(script)

   Evaluates script and returns result.

.. js:function:: Value source(fileName)

   Evaluates script file and returns result of last expression in the script.

   This is useful to move some common code out of commands.

   .. code-block:: js

       // File: c:/copyq/replace_clipboard_text.js
       replaceClipboardText = function(replaceWhat, replaceWith)
       {
           var text = str(clipboard())
           var newText = text.replace(replaceWhat, replaceWith)
           if (text != newText)
               copy(newText)
       }

   .. code-block:: js

       source('c:/copyq/replace_clipboard_text.js')
       replaceClipboardText('secret', '*****')

.. js:function:: currentPath([path])

   Set current path.

.. js:function:: String currentPath()

   Get current path.

.. js:function:: String str(value)

   Converts a value to string.

   If ByteArray object is the argument, it assumes UTF8 encoding. To use
   different encoding, use ``toUnicode()``.

.. js:function:: ByteArray input()

   Returns standard input passed to the script.

.. js:function:: String toUnicode(ByteArray, encodingName)

   Returns string for bytes with given encoding.

.. js:function:: String toUnicode(ByteArray)

   Returns string for bytes with encoding detected by checking Byte Order Mark (BOM).

.. js:function:: ByteArray fromUnicode(String, encodingName)

   Returns encoded text.

.. js:function:: ByteArray data(mimeType)

   Returns data for automatic commands or selected items.

   If run from menu or using non-global shortcut the data are taken from
   selected items.

   If run for automatic command the data are clipboard content.

.. js:function:: ByteArray setData(mimeType, data)

   Modifies data for ``data()`` and new clipboard item.

   Next automatic command will get updated data.

   This is also the data used to create new item from clipboard.

   E.g. following automatic command will add creation time data and tag to
   new items.

   ::

       copyq:
       var timeFormat = 'yyyy-MM-dd hh:mm:ss'
       setData('application/x-copyq-user-copy-time', dateString(timeFormat))
       setData(mimeTags, 'copied: ' + time)

   E.g. following menu command will add tag to selected items.

   ::

       copyq:
       setData('application/x-copyq-tags', 'Important')

.. js:function:: ByteArray removeData(mimeType)

   Removes data for ``data()`` and new clipboard item.

.. js:function:: String[] dataFormats()

   Returns formats available for ``data()``.

.. js:function:: print(value)

   Prints value to standard output.

.. js:function:: serverLog(value)

   Prints value to application log.

.. js:function:: abort()

   Aborts script evaluation.

.. js:function:: fail()

   Aborts script evaluation with nonzero exit code.

.. js:function:: setCurrentTab(tabName)

   Focus tab without showing main window.

.. js:function:: selectItems(row, ...)

   Selects items in current tab.

.. js:function:: String selectedTab()

   Returns tab that was selected when script was executed.

   See `Selected Items`_.

.. js:function:: int[] selectedItems()

   Returns selected rows in current tab.

   See `Selected Items`_.

.. js:function:: Item selectedItemData(index)

   Returns data for given selected item.

   The data can empty if the item was removed during execution of the
   script.

   See `Selected Items`_.

.. js:function:: bool setSelectedItemData(index, item)

   Set data for given selected item.

   Returns false only if the data cannot be set, usually if item was
   removed.

   See `Selected Items`_.

.. js:function:: Item[] selectedItemsData()

   Returns data for all selected items.

   Some data can be empty if the item was removed during execution of the
   script.

   See `Selected Items`_.

.. js:function:: setSelectedItemsData(item[])

   Set data to all selected items.

   Some data may not be set if the item was removed during execution of the
   script.

   See `Selected Items`_.

.. js:function:: int currentItem(), int index()

   Returns current row in current tab.

   See `Selected Items`_.

.. js:function:: String escapeHtml(text)

   Returns text with special HTML characters escaped.

.. js:function:: Item unpack(data)

   Returns deserialized object from serialized items.

.. js:function:: ByteArray pack(item)

   Returns serialized item.

.. js:function:: Item getItem(row)

   Returns an item in current tab.

.. js:function:: setItem(row, text|item)

   Inserts item to current tab.

.. js:function:: String toBase64(data)

   Returns base64-encoded data.

.. js:function:: ByteArray fromBase64(base64String)

   Returns base64-decoded data.

.. js:function:: ByteArray md5sum(data)

   Returns MD5 checksum of data.

.. js:function:: ByteArray sha1sum(data)

   Returns SHA1 checksum of data.

.. js:function:: ByteArray sha256sum(data)

   Returns SHA256 checksum of data.

.. js:function:: ByteArray sha512sum(data)

   Returns SHA512 checksum of data.

.. js:function:: bool open(url, ...)

   Tries to open URLs in appropriate applications.

   Returns true only if all URLs were successfully opened.

.. js:function:: FinishedCommand execute(argument, ..., null, stdinData, ...)

   Executes a command.

   All arguments after ``null`` are passed to standard input of the
   command.

   If argument is function it will be called with array of lines read from
   stdout whenever available.

   E.g. create item for each line on stdout:

   .. code-block:: js

       execute('tail', '-f', 'some_file.log',
               function(lines) { add.apply(this, lines) })

   Returns object for the finished command or ``undefined`` on failure.

.. js:function:: String currentWindowTitle()

   Returns window title of currently focused window.

.. js:function:: Value dialog(...)

   Shows messages or asks user for input.

   Arguments are names and associated values.

   Special arguments:

   -  '.title' - dialog title
   -  '.icon' - dialog icon (see below for more info)
   -  '.style' - Qt style sheet for dialog
   -  '.height', '.width', '.x', '.y' - dialog geometry
   -  '.label' - dialog message (can contain basic HTML)

   .. code-block:: js

       dialog(
         '.title', 'Command Finished',
         '.label', 'Command <b>successfully</b> finished.'
         )

   Other arguments are used to get user input.

   .. code-block:: js

       var amount = dialog('.title', 'Amount?', 'Enter Amount', 'n/a')
       var filePath = dialog('.title', 'File?', 'Choose File', new File('/home'))

   If multiple inputs are required, object is returned.

   .. code-block:: js

       var result = dialog(
         'Enter Amount', 'n/a',
         'Choose File', new File(str(currentPath))
         )
       print('Amount: ' + result['Enter Amount'] + '\n')
       print('File: ' + result['Choose File'] + '\n')

   Editable combo box can be created by passing array. Current value can be
   provided using ``.defaultChoice`` (by default it's the first item).

   .. code-block:: js

       var text = dialog('.defaultChoice', '', 'Select', ['a', 'b', 'c'])

   List can be created by prefixing name/label with ``.list:`` and passing
   array.

   .. code-block:: js

       var items = ['a', 'b', 'c']
       var selected_index = dialog('.list:Select', items)
       if (selected_index)
           print('Selected item: ' + items[selected_index])

   Icon for custom dialog can be set from icon font, file path or theme.
   Icons from icon font can be copied from icon selection dialog in Command
   dialog or dialog for setting tab icon (in menu 'Tabs/Change Tab Icon').

   .. code-block:: js

       var search = dialog(
         '.title', 'Search',
         '.icon', 'search', // Set icon 'search' from theme.
         'Search', ''
         )

.. js:function:: String[] settings()

   Returns array with names of all custom user options.

.. js:function:: Value settings(optionName)

   Returns value for a custom user option.

.. js:function:: settings(optionName, value)

   Sets value for a new custom user option or overrides existing one.

.. js:function:: String dateString(format)

   Returns text representation of current date and time.

   See
   `QDateTime::toString() <http://doc.qt.io/qt-5/qdatetime.html#toString>`__
   for details on formatting date and time.

   Example:

   .. code-block:: js

       var now = dateString('yyyy-MM-dd HH:mm:ss')

.. js:function:: Command[] commands()

   Return list of all commands.

.. js:function:: setCommands(Command[])

   Clear previous commands and set new ones.

   To add new command:

   .. code-block:: js

       var cmds = commands()
       cmds.unshift({
               name: 'New Command',
               automatic: true,
               input: 'text/plain',
               cmd: 'copyq: popup("Clipboard", input())'
               })
       setCommands(cmds)

.. js:function:: Command[] importCommands(String)

   Return list of commands from exported commands text.

.. js:function:: String exportCommands(Command[])

   Return exported command text.

.. js:function:: NetworkReply networkGet(url)

   Sends HTTP GET request.

   Returns reply.

.. js:function:: NetworkReply networkPost(url, postData)

   Sends HTTP POST request.

   Returns reply.

.. js:function:: ByteArray env(name)

   Returns value of environment variable with given name.

.. js:function:: bool setEnv(name, value)

   Sets environment variable with given name to given value.

   Returns true only if the variable was set.

.. js:function:: sleep(time)

   Wait for given time in milliseconds.

.. js:function:: afterMilliseconds(time, function)

   Executes function after given time in milliseconds.

.. js:function:: String[] screenNames()

   Returns list of available screen names.

.. js:function:: ByteArray screenshot(format='png', [screenName])

   Returns image data with screenshot.

   Default ``screenName`` is name of the screen with mouse cursor.

   You can list valid values for ``screenName`` with ``screenNames()``.

   Example:

   .. code-block:: js

       copy('image/png', screenshot())

.. js:function:: ByteArray screenshotSelect(format='png', [screenName])

   Same as ``screenshot()`` but allows to select an area on screen.

.. js:function:: String[] queryKeyboardModifiers()

   Returns list of currently pressed keyboard modifiers which can be 'Ctrl', 'Shift', 'Alt', 'Meta'.

.. js:function:: String iconColor()

   Get current tray and window icon color name.

.. js:function:: iconColor(colorName)

   Set current tray and window icon color name.

   Resets color if color name is empty string.

   Throws exception is the color name is not empty and invalid.

   .. code-block:: js

       // Flash icon for few moments to get attention.
       var color = iconColor()
       for (var i = 0; i < 10; ++i) {
         iconColor("red")
         sleep(500)
         iconColor(color)
         sleep(500)
       }

.. js:function:: String iconTag()

   Get current tray and window tag text.

.. js:function:: iconTag(tag)

   Set current tray and window tag text.

.. js:function:: String iconTagColor()

   Get current tray and window tag color name.

.. js:function:: iconTagColor(colorName)

   Set current tray and window tag color name.

   Throws exception is the color name is invalid.

.. js:function:: onClipboardChanged()

   Called when clipboard or X11 selection changes.

   Default implementation is:

   .. code-block:: js

       if (!hasData()) {
           updateClipboardData();
       } else if (runAutomaticCommands()) {
           saveData();
           updateClipboardData();
       } else {
           clearClipboardData();
       }

.. js:function:: onOwnClipboardChanged()

   Called when clipboard or X11 selection changes by a CopyQ instance.

   Owned clipboard data contains ``mimeOwner`` format.

   Default implementation calls ``updateClipboardData()``.

.. js:function:: onHiddenClipboardChanged()

   Called when hidden clipboard or X11 selection changes.

   Hidden clipboard data contains ``mimeHidden`` format set to ``1``.

   Default implementation calls ``updateClipboardData()``.

.. js:function:: bool runAutomaticCommands()

   Executes automatic commands on current data.

   If an executed command calls ``ignore()`` or have "Remove Item" or "Transform"
   check box enabled, following automatic commands won't be executed and the
   function returns false. Otherwise true is returned.

.. js:function:: clearClipboardData()

   Clear clipboard visibility in GUI.

   Default implementation is:

   .. code-block:: js

       if (isClipboard()) {
           setTitle();
           hideDataNotification();
       }

.. js:function:: updateTitle()

   Update main window title and tool tip from current data.

   Called when clipboard changes.

.. js:function:: updateClipboardData()

   Sets current clipboard data for tray menu, window title and notification.

   Default implementation is:

   .. code-block:: js

       if (isClipboard()) {
           updateTitle();
           showDataNotification();
           setClipboardData();
       }

.. js:function:: updateClipboardData()

   Clears current clipboard data for tray menu, window title and notification.

   Default implementation is:

   .. code-block:: js

       if (isClipboard()) {
           setTitle();
           hideDataNotification();
       }

.. js:function:: setTitle([title])

   Set main window title and tool tip.

.. js:function:: synchronizeToSelection(text)

   Synchronize current data from clipboard to X11 selection.

   Called automatically from clipboard monitor process if option
   ``copy_clipboard`` is enabled.

   Default implementation calls ``provideSelection()``.

.. js:function:: synchronizeFromSelection(text)

   Synchronize current data from X11 selection to clipboard.

   Called automatically from clipboard monitor process if option
   ``copy_selection`` is enabled.

   Default implementation calls ``provideClipboard()``.

.. js:function:: saveData()

   Save current data (depends on `mimeOutputTab`).

.. js:function:: bool hasData()

   Returns true only if some non-empty data can be returned by data().

   Empty data is combination of whitespace and null characters or some internal
   formats (`mimeWindowTitle`, `mimeClipboardMode` etc.)

.. js:function:: showDataNotification()

   Show notification for current data.

.. js:function:: hideDataNotification()

   Hide notification for current data.

.. js:function:: setClipboardData()

   Sets clipboard data for menu commands.

Types
-----

.. js:class:: ByteArray

   Wrapper for QByteArray Qt class.

   See `QByteArray <http://doc.qt.io/qt-5/qbytearray.html>`__.

   ``ByteArray`` is used to store all item data (image data, HTML and even
   plain text).

   Use ``str()`` to convert it to string. Strings are usually more
   versatile. For example to concatenate two items, the data need to be
   converted to strings first.

   .. code-block:: js

       var text = str(read(0)) + str(read(1))

.. js:class:: File

   Wrapper for QFile Qt class.

   See `QFile <http://doc.qt.io/qt-5/qfile.html>`__.

   To open file in different modes use:

   - `open()` - read/write
   - `openReadOnly()` - read only
   - `openWriteOnly()` - write only, truncates the file
   - `openAppend()` - write only, appends to the file

   Following code reads contents of "README.md" file from current
   directory.

   .. code-block:: js

       var f = new File('README.md')
       if (!f.openReadOnly())
         raise 'Failed to open the file: ' + f.errorString()
       var bytes = f.readAll()

   Following code writes to a file in home directory.

   .. code-block:: js

       var dataToWrite = 'Hello, World!'
       var filePath = Dir().homePath() + '/copyq.txt'
       var f = new File(filePath)
       if (!f.openWriteOnly() || f.write(dataToWrite) == -1)
         raise 'Failed to save the file: ' + f.errorString()

       // Always flush the data and close the file,
       // before opening the file in other application.
       f.close()

.. js:class:: Dir

   Wrapper for QDir Qt class.

   See `QDir <http://doc.qt.io/qt-5/qdir.html>`__.

.. js:class:: TemporaryFile

   Wrapper for QTemporaryFile Qt class.

   See `QTemporaryFile <https://doc.qt.io/qt-5/qtemporaryfile.html>`__.

   .. code-block:: js

       var f = new TemporaryFile()
       f.open()
       f.setAutoRemove(false)
       popup('New temporary file', f.fileName())

   To open file in different modes, use same open methods as for `File`.

.. js:class:: Item (Object)

   Object with MIME types of an item.

   Each property is MIME type with data.

   Example:

   .. code-block:: js

       var item = {}
       item[mimeText] = 'Hello, World!'
       item[mimeHtml] = '<p>Hello, World!</p>'
       write(mimeItems, pack(item))

.. js:class:: FinishedCommand (Object)

   Properties of finished command.

   Properties are:

   -  ``stdout`` - standard output
   -  ``stderr`` - standard error output
   -  ``exit_code`` - exit code

.. js:class:: NetworkReply (Object)

   Received network reply object.

   Properties are:

   -  ``data`` - reply data
   -  ``error`` - error string (set only if an error occurred)
   -  ``redirect`` - URL for redirection (set only if redirection is
      needed)
   -  ``headers`` - reply headers (array of pairs with header name and
      header content)

.. js:class:: Command (Object)

   Wrapper for a command (from Command dialog).

   Properties are same as members of `Command
   struct <https://github.com/hluk/CopyQ/blob/master/src/common/command.h>`__.

Objects
-------

.. js:data:: arguments (Array)

   Array for accessing arguments passed to current function or the script
   (``arguments[0]`` is the script itself).

.. js:data:: global (Object)

    Object allowing to modify global scope which contains all functions like
    ``copy()`` or ``add()``. This is useful for :ref:`commands-script`.

MIME Types
----------

Item and clipboard can provide multiple formats for their data. Type of
the data is determined by MIME type.

Here is list of some common and builtin (start with
``application/x-copyq-``) MIME types.

These MIME types values are assigned to global variables prefixed with
``mime``.

.. note::

   Content for following types is UTF-8 encoded.

.. js:data:: mimeText (text/plain)

   Data contains plain text content.

.. js:data:: mimeHtml (text/html)

   Data contains HTML content.

.. js:data:: mimeUriList (text/uri-list)

   Data contains list of links to files, web pages etc.

.. js:data:: mimeWindowTitle (application/x-copyq-owner-window-title)

   Current window title for copied clipboard.

.. js:data:: mimeItems (application/x-copyq-item)

   Serialized items.

.. js:data:: mimeItemNotes (application/x-copyq-item-notes)

   Data contains notes for item.

.. js:data:: mimeOwner (application/x-copyq-owner)

   If available, the clipboard was set from CopyQ (from script or copied items).

   Such clipboard is ignored in CopyQ, i.e. it won't be stored in clipboard
   tab and automatic commands won't be executed on it.

.. js:data:: mimeClipboardMode (application/x-copyq-clipboard-mode)

   Contains ``selection`` if data is from X11 mouse selection.

.. js:data:: mimeCurrentTab (application/x-copyq-current-tab)

   Current tab name when invoking command from main window.

   Following command print the tab name when invoked from main window.

   ::

       copyq data application/x-copyq-current-tab
       copyq selectedTab

.. js:data:: mimeSelectedItems (application/x-copyq-selected-items)

   Selected items when invoking command from main window.

.. js:data:: mimeCurrentItem (application/x-copyq-current-item)

   Current item when invoking command from main window.

.. js:data:: mimeHidden (application/x-copyq-hidden)

   If set to ``1``, the clipboard or item content will be hidden in GUI.

   This won't hide notes and tags.

   E.g. if you run following, window title and tool tip will be cleared.

   ::

       copyq copy application/x-copyq-hidden 1 plain/text "This is secret"

.. js:data:: mimeShortcut (application/x-copyq-shortcut)

   Application or global shortcut which activated the command.

   ::

       copyq:
       var shortcut = data(mimeShortcut)
       popup("Shortcut Pressed", shortcut)

.. js:data:: mimeColor (application/x-copyq-color)

   Item color (same as the one used by themes).

   Examples: #ffff00 rgba(255,255,0,0.5) bg - #000099

.. js:data:: mimeOutputTab (application/x-copyq-output-tab)

   Name of the tab where to store new item.

   The clipboard data will be stored in tab with this name after all
   automatic commands are run.

   Clear or remove the format to omit storing the data.

   E.g. to omit storing the clipboard data use following in an automatic
   command.

   .. code-block:: js

       removeData(mimeOutputTab)

   Valid only in automatic commands.

Selected Items
--------------

Functions that get and set data for selected items and current tab are
only available if called from Action dialog or from a command which is
in menu.

Selected items are indexed from top to bottom as they appeared in the
current tab at the time the command is executed.

Plugins
-------

Use ``plugins`` object to access functionality of plugins.

.. js:function:: plugins.itemsync.selectedTabPath()

   Returns synchronization path for current tab (mimeCurrentTab).

   .. code-block:: js

       var path = plugins.itemsync.selectedTabPath()
       var baseName = str(data(plugins.itemsync.mimeBaseName))
       var absoluteFilePath = Dir(path).absoluteFilePath(baseName)
       // NOTE: Known file suffix/extension can be missing in the full path.

.. js:class:: plugins.itemsync.tabPaths (Object)

   Object that maps tab name to synchronization path.

   .. code-block:: js

       var tabName = 'Downloads'
       var path = plugins.itemsync.tabPaths[tabName]

.. js:data:: plugins.itemsync.mimeBaseName (application/x-copyq-itemsync-basename)

   MIME type for accessing base name (without full path).

   Known file suffix/extension can be missing in the base name.

.. js:data:: plugins.itemtags.userTags (Array)

   List of user-defined tags.

.. js:function:: plugins.itemtags.tags(row, ...)

   List of tags for items in given rows.

.. js:function:: plugins.itemtags.tag(tagName, [rows, ...])

   Add given tag to items in given rows or selected items.

   See `Selected Items`_.

.. js:function:: plugins.itemtags.untag(tagName, [rows, ...])

   Remove given tag from items in given rows or selected items.

   See `Selected Items`_.

.. js:function:: plugins.itemtags.clearTags([rows, ...])

   Remove all tags from items in given rows or selected items.

   See `Selected Items`_.

.. js:function:: plugins.itemtags.hasTag(tagName, [rows, ...])

   Return true if given tag is present in any of items in given rows or
   selected items.

   See `Selected Items`_.

.. js:data:: plugins.itemtags.mimeTags (application/x-copyq-tags)

   MIME type for accessing list of tags.

   Tags are separated by comma.

.. js:function:: plugins.itempinned.isPinned(rows, ...)

   Returns true only if any item in given rows is pinned.

.. js:function:: plugins.itempinned.pin(rows, ...)

   Pin items in given rows or selected items or new item created from clipboard
   (if called from automatic command).

.. js:function:: plugins.itempinned.unpin(rows, ...)

   Unpin items in given rows or selected items.