From 14f712f88b02035e213b338ec5825639c7cc00b0 Mon Sep 17 00:00:00 2001 From: ache Date: Mon, 11 Dec 2023 01:56:41 +0100 Subject: Add the clip2file tool --- Makefile | 2 +- clip2file.py | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 clip2file.py diff --git a/Makefile b/Makefile index d77e953..483effd 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -INSTALL_LIST=autoDHCP.sh autoWall.sh cmount.sh coWifi.sh cumount.sh imgs2pdf.sh light2.sh toMp3.sh bot4chan.py track.py autoMMS.py pyhttpd.py +INSTALL_LIST=autoDHCP.py autoWall.sh cmount.sh coWifi.sh cumount.sh imgs2pdf.sh light2.sh toMp3.sh bot4chan.py track.py autoMMS.py pyhttpd.py clip2file.py install: @if [ -z $(filter-out $@,$(MAKECMDGOALS)) ] ; then \ diff --git a/clip2file.py b/clip2file.py new file mode 100644 index 0000000..d62b725 --- /dev/null +++ b/clip2file.py @@ -0,0 +1,123 @@ +#!/bin/env python + +import subprocess +import datetime +import sys +import os +import argparse + + +""" + This program will write the clipboard to a file. + The format of the file is determined by the available targets and the + priority list. +""" + +OUTPUT_DIR = "~" + +def get_clipboard_content(target): + # Get the clipboard contents. + try: + clipfile = subprocess.check_output( + ['xclip', '-o', '-t', target, '-selection', 'clipboard'] + ) + except subprocess.CalledProcessError as e: + print(f"Failed to get clipboard contents: {e}", file=sys.stderr) + sys.exit(e.returncode) + # Check if the clipboard contents are empty. + if not clipfile: + print("Clipboard is empty.", file=sys.stderr) + sys.exit(1) + + return clipfile + +def get_available_targets(): + # Get the lists of available targets. + # Executes `xclip -o -t TARGETS -selection clipboard` and get the output + # as a list of strings. + try: + output = subprocess.check_output( + ['xclip', '-o', '-t', 'TARGETS', '-selection', 'clipboard'] + ) + except subprocess.CalledProcessError as e: + print(f"xclip failed: {e}", file=sys.stderr) + sys.exit(e.returncode) + + return output.decode('utf-8').split('\n') + # The output is a list of strings, each string is a target. + +def save_file(clipfile, filename, output_dir, ext): + # Write the clipboard contents to a file. + pathfile = os.path.expanduser(f"{output_dir}/{filename}.{ext}") + + try: + with open(pathfile, 'wb') as f: + try: + f.write(clipfile) + except IOError as e: + print(f"Failed to write to file {filename} in {output_dir}: {e}", file=sys.stderr) + sys.exit(e.errno) + except IOError as e: + print(f"Failed to open file {filename} in {output_dir}: {e}", file=sys.stderr) + + +def main(filename, output_dir): + # The priority of the target. + # The first target in the list is the most important. + # The final target will be the first of this list matching the available targets + priority = [ + ("image/avif", "avif"), + ("image/png", "png"), + ("text/plain", "txt"), + ("UTF8_STRING", "txt"), + ] + + targets = get_available_targets() + + # Get the first target in the priority list that is available. + # If no target is found, the script exits. + target = None + ext = None + for f, e in priority: + if f in targets: + target, ext = f, e + break + + if target is None or not isinstance(target, str): + print("No supported clipboard target found.", file=sys.stderr) + sys.exit(1) + + clipfile = get_clipboard_content(target) + + save_file(clipfile, filename, output_dir, ext) + + print(f"Saved clipboard contents to {output_dir}/{filename}.{ext}") + + +if __name__ == '__main__': + date = datetime.datetime.now().isoformat() + default_filename = f"{date}_clip" + + parser = argparse.ArgumentParser( + description="Save clipboard contents to a file." + ) + # Select the directory + parser.add_argument( + '-d', '--directory', + help="Directory to save the clipboard contents to.", + default=OUTPUT_DIR, + type=str + ) + # Select the filename + parser.add_argument( + '-f', '--filename', + help="Filename to save the clipboard contents to.", + default=default_filename, + type=str + ) + + args = parser.parse_args() + + main(args.filename, args.directory) + + -- cgit v1.2.3