Skip to content

Instantly share code, notes, and snippets.

@SergKolo
Last active April 5, 2022 18:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SergKolo/ff9defd9cef4e704a64be505f28fa3cd to your computer and use it in GitHub Desktop.
Save SergKolo/ff9defd9cef4e704a64be505f28fa3cd to your computer and use it in GitHub Desktop.
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify
import subprocess
import os
import sys
def send_notif(n,title, text):
try:
if Notify.init(__file__):
# n = Notify.Notification.new("Notify")
n.update(title, text)
n.set_urgency(2)
if not n.show():
raise SyntaxError("sending notification failed!")
else:
raise SyntaxError("can't initialize notification!")
except SyntaxError as error:
print(error)
if error == "sending notification failed!":
Notify.uninit()
else:
Notify.uninit()
# This script is meant to be bound to keyboard shortcut
#def send_notification(title, text):
# Notify.init(sys.argv[0])
# n = Notify.Notification.new(title, text)
# n.show()
def run_cmd(cmdlist):
# function for running
try:
stdout = subprocess.check_output(cmdlist)
except subprocess.CalledProcessError:
print('Error')
print(cmdlist)
send_notif(sys.argv[0],"Clipboard error")
#sys.exit(1)
#pass
else:
if stdout:
return stdout
def main():
# get contents of both clipboards
clip = primary = ""
clip = run_cmd("xclip -o -sel clip".split())
primary = run_cmd("xclip -o -sel primary".split())
print(primary)
# write to temp file contents
# of both clipboards appended
temp_file = "/tmp/append.clip"
with open(temp_file, "w") as f:
try:
f.write( clip + primary )
except:
pass
if os.path.isfile(temp_file):
# Read the new contents into clipboard
read_file = "xclip -sel clip " + temp_file
subprocess.call(read_file.split())
# clean up
os.remove(temp_file)
if __name__ == '__main__':
notif = Notify.Notification.new("Notify")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment