v1.2.0.1-beta1
Clipboard Utility Example

Utility module for working with the system clipboard.

See the published documentation for a properly formatted version of this README.

This is really a useful utility for copying text to and from the system clipboard. It can write the contents of either a variable or a file to the clipboard, and also read the current clipboard contents into a file or return it as a string value.

As an example, it demonstrates the use of Process to launch an external program, wait for it to finish, and in the case of clipboardToValue(), read what the program printed/returned to standard output (in this case the contents of the clipboard).

The clipboard interactions are done by invoking the appropriate system utility based on which operating system the plugin is currently running on. On Windows this is the built-in clip command for writing to the clipboard, and a PowerShell command for reading. On MacOS the built-in pbcopy and pbpaste commands are used, while on Linux the xclip utility is used (which may require installation).

The module's functions could be used directly from a Module type plugin action, and they can also be useful in other modules. For an example of the latter, see the Color Picker script which imports this module for copying color values to/from the clipboard (and in fact why it was created, after I realized Touch Portal v3.1 lacked this functionality).

For example to copy a dynamic Value or State to the clipboard (here it is copy the current time in 24 hour format):

To copy a file's contents to the clipboard, you could use the same action but with the expression:

M.fileToClipboard("/path/to/file.txt")

Or to read the current clipboard contents and return the value as a Touch Portal State (here named "Clipboard_Value"):

Note
This code can also be found in the project's repository at
https://github.com/mpaperno/DSEP4TP/tree/main/resources/examples/Clipboard/
1// Since TP currently has no way to copy text values or file contents to or from clipboard,
2// this module provides these functions by using operating system features (or `xclip` utility on Linux).
3
4// Copies any string `value` to the clipboard. Linux requires `xclip` installed.
5export function valueToClipboard(value)
6{
7 var cmd;
8 if (DSE.PLATFORM_OS.startsWith("win"))
9 cmd = 'cmd /C "@echo.|set /p temp=' + value + '|clip"';
10 else if (DSE.PLATFORM_OS === "osx")
11 cmd = 'echo -n "' + value + '"|pbcopy';
12 // Linux requires 'xclip' installed.
13 else if (DSE.PLATFORM_OS === "linux")
14 cmd = 'echo -n "' + value + '"|xclip -selection clipboard';
15 else
16 return;
17
18 runCommand(cmd);
19}
20
21// Copies the contents of `file` to the clipboard. Linux requires `xclip` installed.
22export function fileToClipboard(file)
23{
24 var cmd;
25 if (DSE.PLATFORM_OS.startsWith("win"))
26 cmd = 'cmd /C "@type ""' + file + '"" | clip"';
27 else if (DSE.PLATFORM_OS === "osx")
28 cmd = 'cat "' + file + '" | pbcopy';
29 // Linux requires 'xclip' installed.
30 else if (DSE.PLATFORM_OS === "linux")
31 cmd = 'xclip -selection clipboard "' + file + '"';
32 else
33 return;
34
35 runCommand(cmd);
36}
37
38// Returns contents of clipboard as a string value. Linux requires `xclip` installed.
39export function clipboardToValue()
40{
41 var cmd;
42 if (DSE.PLATFORM_OS.startsWith("win"))
43 cmd = 'powershell.exe -Command "& { Get-Clipboard -Raw | Write-Host -NoNewLine }"';
44 else if (DSE.PLATFORM_OS === "osx")
45 cmd = 'pbpaste';
46 // Linux requires 'xclip' installed.
47 else if (DSE.PLATFORM_OS === "linux")
48 cmd = 'xclip -selection clipboard -o';
49 else
50 return "Unknown operating system!";
51
52 const p = runCommand(cmd);
53 return String(p.readAll());
54}
55
56// Writes contents of clipboard to `file`. Linux requires `xclip` installed.
57export function clipboardToFile(file)
58{
59 var cmd;
60 if (DSE.PLATFORM_OS.startsWith("win"))
61 cmd = 'powershell.exe -Command "& { Get-Clipboard -Raw | Write-Host -NoNewLine }" 1> "' + file + '"';
62 else if (DSE.PLATFORM_OS === "osx")
63 cmd = 'pbpaste > "' + file + '"';
64 // Linux requires 'xclip' installed.
65 else if (DSE.PLATFORM_OS === "linux")
66 cmd = 'xclip -selection clipboard -o > "' + file + '"';
67 else
68 return;
69
70 runCommand(cmd);
71}
72
73function runCommand(cmd)
74{
75 const p = new Process();
76 p.startCommand(cmd);
77 p.waitForFinished();
78 return p;
79}
The DSE object contains constants and functions related to the plugin environment....
String PLATFORM_OS
This property contains the name of the operating system running the plugin.
Definition: DSE.h:95
The Process class allows interaction with external processes, such as launching a system command or r...
Definition: Process.h:62
Extends String object.