Skip to content

Instantly share code, notes, and snippets.

@Leo7654
Last active July 23, 2016 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leo7654/95ddefd8ca1025627c41dc99cdd1d5ce to your computer and use it in GitHub Desktop.
Save Leo7654/95ddefd8ca1025627c41dc99cdd1d5ce to your computer and use it in GitHub Desktop.
OSX: copy screenshots to clipboard

Copy ScreenShot To Clipboard For OS X

Why

  • On Os x we just can capture screenshot and save to either file or clipboard not both.
  • So I made script.

How

  1. Change screenshot dir to somewhere.
  2. Make a file clipboard copy script with python.
  3. Register a deamon, watching screenshot dir and run python script.
  4. Shot with Shift+Cammand+4.
brew install PyGTK python
defaults write com.apple.screencapture name "Screen Shot"
defaults write com.apple.screencapture location ~/screens/
killall SystemUIServer
cat > ~/Library/LaunchAgents/ss2CB.plist <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>copy Screen Shots to ClipBoard</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/python</string>
<string>/Users/$(whoami)/ss2cb.py</string>
</array>
<key>WatchPaths</key>
<array>
<string>~/screens/</string>
</array>
</dict>
</plist>
EOF
cat > ~/ss2cb.py <<<EOF
#! /usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk
import os
import sys
def copy_image(f):
assert os.path.exists(f), "file does not exist"
image = gtk.gdk.pixbuf_new_from_file(f)
clipboard = gtk.clipboard_get()
clipboard.set_image(image)
clipboard.store()
def all_files_under(path):
path = os.path.expanduser(path);
cur_path = path
for filename in os.listdir(path):
yield os.path.join(cur_path, filename)
if len(sys.argv) < 2:
file = max(all_files_under('~/screens/'), key=os.path.getctime)
else:
file = sys.argv[1]
copy_image(file);
print 'File: "' + file + '" Copied'
EOF
launchctl start ~/Library/LaunchAgents/ss2CB.plist
launchctl load ~/Library/LaunchAgents/ss2CB.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>copy Screen Shots to ClipBoard</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/python</string>
<string>/Users/WHOAMI/ss2cb.py</string> <<< CHANGE WHOAMI !!
</array>
<key>WatchPaths</key>
<array>
<string>~/screens/</string>
</array>
</dict>
</plist>
#! /usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk
import os
import sys
def copy_image(f):
assert os.path.exists(f), "file does not exist"
image = gtk.gdk.pixbuf_new_from_file(f)
clipboard = gtk.clipboard_get()
clipboard.set_image(image)
clipboard.store()
def all_files_under(path):
path = os.path.expanduser(path);
cur_path = path
for filename in os.listdir(path):
yield os.path.join(cur_path, filename)
if len(sys.argv) < 2:
file = max(all_files_under('~/screens/'), key=os.path.getctime)
else:
file = sys.argv[1]
copy_image(file);
print 'File: "' + file + '" Copied'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment