GUIテキストエディター/ボックスへの自動貼り付け


3

GUIから選択したテキスト(ブラウザ、テキストエディタなどの任意の数のアプリ)を処理し、1つのキーボードコマンドを使用して選択したテキストを処理済みのテキストに自動的に置き換えるスクリプトを作成しようとしています。

xselでクリップボードまたはプライマリバッファを置き換えることができますが、可能であれば、別のキーボードショートカットを使用して貼り付ける必要はありません。

ワークフロー:

  • テキストを選択
  • ヒットキーコマンド
  • Bashスクリプトがテキストを処理する
  • BashスクリプトはテキストをGUIエディターに貼り付け、選択を置き換えます(<-これは私が理解できないことです)
  • フィン

xselは貼り付けを許可することを読みましたが、私の知る限り、それはテキストをcliに出力すること(実際には貼り付けではない)または他の何かにリダイレクトすることのみを許可します。これは私が必要とするものかもしれませんが、どうすればよいかわかりません(つまり、出力をリダイレクトして、GUIで選択したテキストを自動的に置き換えるにはどうすればよいですか?)

ありがとうございます。

詳細情報:

  • Xubuntu 19.10を使用しています
  • 単純なテキストに置き換えたいだけです(たとえば、小文字を大文字に変更します)
  • これをシンプルに保ちましょう-(キーコマンドを使用して)bashスクリプトからテキストを取得して、現在カーソルがあるGUIテキストエディター/エリアに貼り付け、選択したテキストがある場合はそれを置き換えますか?
2

Since you are using Xorg and ctrl+v ist almost universally the shortcut for paste, the easiest solution is xdotool (available from the official repositories):

xdotool key --clearmodifiers "ctrl+v"

The --clearmodifiers flag attempts to clear all modifiers (ctrl,alt...) before issuing the key presses and restores them afterwards. This is useful when you invoke your script via a keyboard shortcut, because otherwise the application will also see the modifiers you pressed for your shortcut, and e.g. ctrl+alt+v may result in a different command.

Sometimes, this can result in a race condition when you release the modifiers before xdotool restores them, making them stuck. In that case, just press and release the stuck modifiers to unstick them.

xdotool can do a lot more. Read man xdotool to get an overview. It is also often used in combination with xbindkeys, which lets you configure keyboard shortcuts more versatilely than e.g. gnome and also enables you to bind actions to mouse buttons.


4

While bash and xdotool will definitely handle a simple case like this, this is exactly the sort of thing AutoKey was designed to do.

In AutoKey, you can define a simple script written in Python using the AutoKey API to do this. This can be assigned to a hotkey and will run whenever that hotkey is pressed. It would look something like this (untested.)

field = clipboard.get_selection()
field = field.lower()
clipboard.fill_selection(field)
keyboard.send_keys("<ctrl>+v")

which gets the selected text into a variable, converts it to lower case, puts it back in the clipboard, and then pastes it back into the current selection where it originally came from. (If your application window is a terminal, then you'd have to use Ctrl+Shift+v instead.)

Doing it this way has several advantages: It's arguably easier to do than modifying your keyboard. You can easily change the hotkey in the AutoKey GUI. You can define a window filter so the hotkey only works in the windows of your choice. You can turn this functionality on and off at will. And, since you have the entire power of Python at your disposal, you do almost anything else you can think of.

If you had just wanted to substitute one phrase for another, an AutoKey phrase would have done that without writing a single line of code, but since you needed to manipulate the text, a script was required.

Note: Debian and derivative distributions (Ubuntu ...) currently provide a very old version of AutoKey. This is fixed in Debian testing and will be fixed in Ubuntu 20.04. For now, you can easily install the package with these instructions.

For assistance, visit our support forum.


2

I agree with Joe that AutoKey would be perfect for what you want to do. I thought I'd include another example script. This one uses string formatting to also change the selected text to lower case (you could have it do whatever you like instead of changing case):

selection = clipboard.get_selection()
keyboard.send_keys("%s" % selection.lower())