Last active
September 22, 2015 23:20
-
-
Save Jacob-Vlijm/cdb543d370c2ea4197cc to your computer and use it in GitHub Desktop.
Script to paste textual snippets in applications that past with the key combination Ctrl+V. It needs xsel and xdotool to be installed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Copyright (C) 2014 Jacob Vlijm | |
This program is free software: you can redistribute it and/or modify it under | |
the terms of the GNU General Public License as published by the Free Software | |
Foundation, either version 3 of the License, or any later version. This | |
program is distributed in the hope that it will be useful, but WITHOUT ANY | |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | |
A PARTICULAR PURPOSE. See the GNU General Public License for more details. You | |
should have received a copy of the GNU General Public License along with this | |
program. If not, see <http://www.gnu.org/licenses/>. | |
""" | |
import os | |
import subprocess | |
home = os.environ["HOME"] | |
directory = home+"/.config/snippet_paste" | |
if not os.path.exists(directory): | |
os.mkdir(directory) | |
# create file list with snippets | |
files = [ | |
directory+"/"+item for item in os.listdir(directory) \ | |
if not item.endswith("~") and not item.startswith(".") | |
] | |
# create string list | |
strings = [] | |
for file in files: | |
with open(file) as src: | |
strings.append(src.read()) | |
# create list to display in option menu | |
list_items = ["manage snippets"]+[ | |
(str(i+1)+". "+strings[i].replace("\n", " ").replace\ | |
('"', "'")[:20]+"..") for i in range(len(strings)) | |
] | |
# define (zenity) option menu | |
test= 'zenity --list '+'"'+('" "')\ | |
.join(list_items)+'"'\ | |
+' --column="text fragments" --title="Paste snippets"' | |
# process user input | |
try: | |
choice = subprocess.check_output(["/bin/bash", "-c", test]).decode("utf-8") | |
if "manage snippets" in choice: | |
subprocess.call(["nautilus", directory]) | |
else: | |
i = int(choice[:choice.find(".")]) | |
# copy the content of corresponding snippet | |
copy = "xclip -in -selection c "+"'"+files[i-1]+"'" | |
subprocess.call(["/bin/bash", "-c", copy]) | |
# paste into open frontmost file | |
paste = "xdotool key Control_L+v" | |
subprocess.Popen(["/bin/bash", "-c", paste]) | |
except Exception: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see for explanation: http://askubuntu.com/questions/543968/insert-text-blocks-everywhere