やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

xsel でクリップボードを操作する

 標準で入っていてほしいコマンド。

手順

  1. インストール
  2. 使ってみる

1. インストール

sudo apt install -y xsel

1-1. バージョン確認

$ xsel --version
xsel version 1.2.0 by Conrad Parker <conrad@vergenet.net>

1-2. help

$ xsel --help
Usage: xsel [options]
Manipulate the X selection.

By default the current selection is output and not modified if both
standard input and standard output are terminals (ttys).  Otherwise,
the current selection is output if standard output is not a terminal
(tty), and the selection is set from standard input if standard input
is not a terminal (tty). If any input or output options are given then
the program behaves only in the requested mode.

If both input and output is required then the previous selection is
output before being replaced by the contents of standard input.

Input options
  -a, --append          Append standard input to the selection
  -f, --follow          Append to selection as standard input grows
  -i, --input           Read standard input into the selection

Output options
  -o, --output          Write the selection to standard output

Action options
  -c, --clear           Clear the selection
  -d, --delete          Request that the selection be cleared and that
                        the application owning it delete its contents

Selection options
  -p, --primary         Operate on the PRIMARY selection (default)
  -s, --secondary       Operate on the SECONDARY selection
  -b, --clipboard       Operate on the CLIPBOARD selection

  -k, --keep            Do not modify the selections, but make the PRIMARY
                        and SECONDARY selections persist even after the
                        programs they were selected in exit.
  -x, --exchange        Exchange the PRIMARY and SECONDARY selections

X options
  --display displayname
                        Specify the connection to the X server
  -t ms, --selectionTimeout ms
                        Specify the timeout in milliseconds within which the
                        selection must be retrieved. A value of 0 (zero)
                        specifies no timeout (default)

Miscellaneous options
  -l, --logfile         Specify file to log errors to when detached.
  -n, --nodetach        Do not detach from the controlling terminal. Without
                        this option, xsel will fork to become a background
                        process in input, exchange and keep modes.

  -h, --help            Display this help and exit
  -v, --verbose         Print informative messages
  --version             Output version information and exit

Please report bugs to <conrad@vergenet.net>.

1-2-1. key help

$ xdotool key --help
Usage: key [options] <keysequence> [keysequence ...]
--clearmodifiers     - clear active keyboard modifiers during keystrokes
--delay DELAY        - Use DELAY milliseconds between keystrokes
--repeat TIMES       - How many times to repeat the key sequence
--repeat-delay DELAY - DELAY milliseconds between repetitions
--window WINDOW      - send keystrokes to a specific window
Each keysequence can be any number of modifiers and keys, separated by plus (+)
  For example: alt+r

Any letter or key symbol such as Shift_L, Return, Dollar, a, space are valid,
including those not currently available on your keyboard.

If no window is given, and there are windows in the stack, %1 is used. Otherwise
the currently-focused window is used
This command consumes all arguments after it, so you cannot chain
 additional commands after it.

2. 使ってみる

2-1. 記憶領域

2-1-1. clipboard

$ echo "コピーしたい文字列。" | xsel -bi
$ xsel -bo
コピーしたい文字列。

 クリップボードコピーしたい文字列。がセットされた。テキストエディタなどでCtrl+Pキーでペーストするとコピーしたい文字列。が貼り付けられるはず。

2-1-2. primary

 クリップボードのデータはclipboard以外にもprimary, secondaryという仮想領域に保存することもできる。

$ echo "プライマリ・セレクション" | xsel -i
$ xsel -o
プライマリ・セレクション
$ echo "プライマリ・セレクション" | xsel -pi
$ xsel -po
プライマリ・セレクション

2-1-3. secondary

$ echo "セカンダリ・セレクション" | xsel -si
$ xsel -so
セカンダリ・セレクション

 全部使ってみる。

echo "username1" | xsel -pi
echo "password1" | xsel -si
echo "clip" | xsel -bi
xsel -po
xsel -so
xsel -bo

 プライマリにusername1セカンダリpassword1クリップボードclipを代入した。テキストエディタなどでCtrl+Pするとclipがペーストされるはず。

2-2. クリア

xsel -pc
xsel -sc
xsel -bc

 プライマリ、セカンダリクリップボードの内容をクリアする。

2-2. 追記

echo "クリップ" | xsel -ba
xsel -bo
echo "ボード" | xsel -ba
xsel -bo

 追記する。区切り文字は改行コード。

2-3. 応用

 現在日時をクリップボードにセットする。

date "+%Y-%m-%d %H:%M:%S" | xsel -bi
xsel -bo

対象環境

  • Raspbierry pi 3 Model B+
  • Raspbian stretch 9.0 2018-11-13
  • bash 4.4.12
    • zenity 3.28.1
    • whiptail 0.52.19
$ uname -a
Linux raspberrypi 4.14.98-v7+ #1200 SMP Tue Feb 12 20:27:48 GMT 2019 armv7l GNU/Linux