diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2024-11-17 17:12:27 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2024-11-17 17:22:56 +1300 |
commit | e868ab35739fec132097b4e811a504ccf2c0ba50 (patch) | |
tree | dc4144e6638bfc5be5c84c74b38e9b90ce58ccff | |
parent | Refactor keyboard automation (diff) | |
download | vixf-e868ab35739fec132097b4e811a504ccf2c0ba50.tar.gz vixf-e868ab35739fec132097b4e811a504ccf2c0ba50.zip |
Bail out on empty copy
-rw-r--r-- | vixf.py3 | 17 |
1 files changed, 14 insertions, 3 deletions
@@ -10,6 +10,7 @@ text. import locale import os import subprocess +import sys import tempfile import time @@ -34,9 +35,19 @@ with keyboard.pressed(Key.ctrl): with keyboard.pressed(Key.ctrl): keyboard.type('c') +# Read clipboard +content_before = pyperclip.paste() + +# Stop here (error condition) if there's nothing in the clipboard +if len(content_before) == 0: + print( + f'{SELF}: Nothing to edit. Did the selection work?', + file=sys.stderr, + ) + sys.exit(1) with tempfile.NamedTemporaryFile(mode='w', delete_on_close=False) as tf: - tf.write(pyperclip.paste()) + tf.write(content_before) tf.close() env = os.environ @@ -59,9 +70,9 @@ with tempfile.NamedTemporaryFile(mode='w', delete_on_close=False) as tf: encoding = locale.getpreferredencoding() with open(tf.name, mode='r', encoding=encoding) as tfr: - content = tfr.read() + content_after = tfr.read() -pyperclip.copy(content) +pyperclip.copy(content_after) time.sleep(SLEEP) |