Skip to content

Instantly share code, notes, and snippets.

@BlogBlocks
Last active July 18, 2018 03:42
Show Gist options
  • Save BlogBlocks/a6f63d3b7d4122b03df92616cdbd231b to your computer and use it in GitHub Desktop.
Save BlogBlocks/a6f63d3b7d4122b03df92616cdbd231b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"source": [
"import clipit\n",
"help(clipit)"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Help on package clipit:\n",
"\n",
"NAME\n",
" clipit\n",
"\n",
"FILE\n",
" /home/jack/anaconda2/lib/python2.7/site-packages/clipit/__init__.py\n",
"\n",
"PACKAGE CONTENTS\n",
" clip\n",
" tkinter\n",
" x11\n",
"\n",
"FUNCTIONS\n",
" main()\n",
" Entry point for cli.\n",
"\n",
"DATA\n",
" __author__ = 'Jack Northrup'\n",
" __license__ = 'MIT'\n",
" __title__ = 'clipboard'\n",
" __version__ = '0.2.2'\n",
"\n",
"VERSION\n",
" 0.2.2\n",
"\n",
"AUTHOR\n",
" Jack Northrup\n",
"\n\n"
]
}
],
"execution_count": 26,
"metadata": {}
},
{
"cell_type": "code",
"source": [
"!mkdir /home/jack/anaconda2/lib/python2.7/site-packages/clipit"
],
"outputs": [],
"execution_count": null,
"metadata": {
"collapsed": true
}
},
{
"cell_type": "code",
"source": [
"%%writefile /home/jack/anaconda2/lib/python2.7/site-packages/clipit/clip.py\n",
"\n",
"\"\"\" Copy + Paste using X11\n",
"\"\"\"\n",
"import subprocess\n",
"import sys\n",
"\n",
"def main():\n",
" # USE - python clip.py\n",
" if sys.argv[1:]: # called with input arguments\n",
" copy(' '.join(sys.argv[1:]))\n",
" elif not sys.stdin.isatty(): # piped in input\n",
" copy('\\n'.join(sys.stdin.readlines()))\n",
" else: # paste output\n",
" print(paste())\n",
"\n",
"def copy(string, xsel=False):\n",
" \"\"\"Copy given string into system clipboard. If 'xsel' is True, this\n",
" will also copy into the primary x selection for middle click.\"\"\"\n",
" try:\n",
" _cmd = [\"xclip\", \"-selection\", \"clipboard\"]\n",
" subprocess.Popen(_cmd, stdin=subprocess.PIPE).communicate(\n",
" #string.encode('unicode'))\n",
" string.encode('ascii'))\n",
" if xsel:\n",
" _cmd = [\"xclip\", \"-selection\", \"primary\"]\n",
" subprocess.Popen(_cmd, stdin=subprocess.PIPE).communicate(\n",
" #string.encode('unicode'))\n",
" string.encode('ascii'))\n",
" except OSError as why:\n",
" print ('Can not find Xclip') \n",
"\n",
"def paste(xsel=False):\n",
" \"\"\"Returns system clipboard contents.\"\"\"\n",
" selection = \"primary\" if xsel else \"clipboard\"\n",
" try:\n",
" return subprocess.Popen([\"xclip\", \"-selection\", selection, \"-o\"], stdout=subprocess.PIPE).communicate()[0].decode(\"utf-8\")\n",
" except OSError as why:\n",
" print ('Can not find Xclip') \n"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Overwriting /home/jack/anaconda2/lib/python2.7/site-packages/clipit/clip.py\n"
]
}
],
"execution_count": 2,
"metadata": {}
},
{
"cell_type": "code",
"source": [
"%%writefile /home/jack/anaconda2/lib/python2.7/site-packages/clipit/tkinter.py\n",
"\"\"\"\n",
"Copy + Paste Linux\n",
"parts from @ http://code.activestate.com/recipes/150115/\n",
"\"\"\"\n",
"try:\n",
" from Tkinter import Tk\n",
"except ImportError as why:\n",
" raise TkinterNotFound\n",
"\n",
"def copy(string, **kwargs):\n",
" \"\"\"Copy given string into system clipboard.\"\"\"\n",
" window = Tk()\n",
" window.withdraw()\n",
" window.clipboard_clear()\n",
" window.clipboard_append(string)\n",
" window.destroy()\n",
" return\n",
"\n",
"def paste(**kwargs):\n",
" \"\"\"Returns system clipboard contents.\"\"\"\n",
" window = Tk()\n",
" window.withdraw()\n",
" d = window.selection_get(selection = 'CLIPBOARD')\n",
" return d"
],
"outputs": [],
"execution_count": null,
"metadata": {
"collapsed": true
}
},
{
"cell_type": "code",
"source": [
"import clipit\n",
"help(clipit.clip)"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Help on module clipit.clip in clipit:\n",
"\n",
"NAME\n",
" clipit.clip - Copy + Paste in X11\n",
"\n",
"FILE\n",
" /home/jack/anaconda2/lib/python2.7/site-packages/clipit/clip.py\n",
"\n",
"FUNCTIONS\n",
" copy(string, xsel=False)\n",
" Copy given string into system clipboard. If 'xsel' is True, this\n",
" will also copy into the primary x selection for middle click.\n",
" \n",
" main()\n",
" Entry point for cli.\n",
" \n",
" paste(xsel=False)\n",
" Returns system clipboard contents.\n",
"\n\n"
]
}
],
"execution_count": 33,
"metadata": {}
},
{
"cell_type": "code",
"source": [
"import clipit\n",
"from clipit import clip\n",
"clip.copy(\"This is a test\")"
],
"outputs": [],
"execution_count": 4,
"metadata": {}
},
{
"cell_type": "code",
"source": [
"clip.paste()"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": [
"u'This is a test'"
]
},
"metadata": {}
}
],
"execution_count": 5,
"metadata": {}
},
{
"cell_type": "code",
"source": [
"clip.paste()"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": [
"u'This is a test'"
]
},
"metadata": {}
}
],
"execution_count": 6,
"metadata": {}
},
{
"cell_type": "code",
"source": [
"%reset -f"
],
"outputs": [],
"execution_count": 37,
"metadata": {
"collapsed": true
}
},
{
"cell_type": "code",
"source": [
"import clipit\n",
"from clipit import x11"
],
"outputs": [],
"execution_count": 20,
"metadata": {
"collapsed": true
}
},
{
"cell_type": "code",
"source": [
"x11.copy(\"this is a test\")"
],
"outputs": [],
"execution_count": 21,
"metadata": {
"collapsed": true
}
},
{
"cell_type": "code",
"source": [
"x11.paste()"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 22,
"data": {
"text/plain": [
"u'this is a test'"
]
},
"metadata": {}
}
],
"execution_count": 22,
"metadata": {}
},
{
"cell_type": "code",
"source": [],
"outputs": [],
"execution_count": null,
"metadata": {
"collapsed": true
}
}
],
"metadata": {
"kernelspec": {
"name": "python2",
"language": "python",
"display_name": "Python 2"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"name": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13",
"file_extension": ".py",
"codemirror_mode": {
"version": 2,
"name": "ipython"
}
},
"kernel_info": {
"name": "python2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment