#!/usr/bin/env python
import sys
import subprocess
# text file name as command line argument:
# http://www.tutorialspoint.com/python/python_command_line_arguments.htm
# run quote2.file.py text_file.txt
w_file = str(sys.argv[1])
pipe = subprocess.Popen(["xsel -o -p"], stdout=subprocess.PIPE, shell=True)
text = pipe.stdout.read()
text = text.strip() # removing potential whitespace before and after
subprocess.Popen(["xsel -c"], shell=True) # delete selection, to prevent printing it again
if text != '':
# write to end of file:
with open(w_file, "a") as f:
f.write('\n'*2+text)
# displaying notifications:
# http://superuser.com/questions/31917/is-there-a-way-to-show-notification-from-bash-script-in-ubuntu
# you could add icon if youd like
subprocess.Popen(["notify-send --expire-time=1800 \"Selection saved to file.\""], shell=True)