#!/usr/bin/python3
# ====================================================================
# simple multiple clipboards
# move data in or out of the user's clipboard
#
# a. data in the user's clipboard is associated with a key and
# is saved to a json file.
# b. data in a json file associated with a key replaces
# whatever is currently in the user's clipboard.
# --------------------------------------------------------------------
# from: 3 Python Automation Projects - For Beginners
# www.youtube.com/watch?v=Oz3W-LKfafE
# --------------------------------------------------------------------
# upgrade pip: python.exe -m pip install --upgrade pip
# --------------------------------------------------------------------
# install clipboard module: pip3 install clipboard
# ====================================================================
import sys
import clipboard
import json
KEYS_ONLY = False
SAVED_DATA = 'clipboard.json'
# --------------------------------------------------------------------
# ---- save data to clipboard (json) file
# --------------------------------------------------------------------
def save_data(filepath,data):
with open(filepath,'w') as fl:
json.dump(data,fl)
# --------------------------------------------------------------------
# ---- return data from clipboard (json) file
# --------------------------------------------------------------------
def load_data(filepath):
try:
with open(filepath,'r') as fl:
data = json.load(fl)
return data
except:
return {}
# --------------------------------------------------------------------
# ---- main
# --------------------------------------------------------------------
# ---- commandline arguments?
if len(sys.argv) == 2:
command = sys.argv[1]
data = load_data(SAVED_DATA)
# ---- save command -----------------------------------
if command == 'save':
key = input('Enter a key: ').strip()
data[key] = clipboard.paste()
save_data(SAVED_DATA, data)
print('data saved!')
# ---- load command -----------------------------------
elif command == 'load':
key = input('Enter a key: ').strip()
if key in data:
clipboard.copy(data[key])
print('data copied to clipboard')
else:
print('key not found')
# ---- list command -----------------------------------
elif command == 'list':
for k in sorted(data.keys()):
if KEYS_ONLY:
print(k)
else:
print(f'{k:8} : {data[k]}')
# ---- unknown command --------------------------------
else:
print(f'unknown command ({command})')
else:
print('please pass exaclty one command (save, load, list)')