Category Archives: code

Clipboard Snippeteering

I work in IT and frequently you may need to send someone a screenshot. Maybe its the configuration screen you’re looking at or the output of a command. As many of you know this procedure usually involves hitting print-screen on your keyboard, pasting it into a photo editor like ms-paint, saving it as a jpeg and what… do you send it over IM with a direct connection? email it to them? I wanted a solution where I can easily take a screenshot of just an area of the screen, or the entire screen if I choose and have it automatically upload the screenshot to a webserver, and allow me to paste the URL to the screenshot instantly. Ideally, I would have it even put the URL IN my clipboard as well, as it turns out this is all exactly what I accomplished.

Screenshot Utility in CompizConfig Settings Manager

I run Ubuntu on my workstation with compiz. You will need to install compizconfig-settings-manager, xsel, and ImageMagick for this all to work. Below is the script to perform the screenshot auto-magic, when combined with the compiz screenshot plugin. I press the windows key on my keyboard and then click+drag I have an instant screenshot.

FNAME="/home/chrisha/Documents/snippets/$(date +%s|md5sum|awk '{print $1}').jpg"

/usr/bin/convert -bordercolor white -border 5 $1 $1
/usr/bin/convert -bordercolor black -border 1 $1 $1
/usr/bin/convert -quality 75 $1 $FNAME

scp $FNAME slice:public_html/dev.chrishaynie.com/html/snippets/

echo http://slice.chrishaynie.com/snippets/$(basename $FNAME)|xsel -i

rm -f $1

Now then, lets say you want to send a large block of text – maybe its a snippet of code, maybe its the output of a command. Taking a screenshot of text isn’t verfy efficient, which we can agree. Traditional solutions have been just pasting it into the chat window and spamming the receiver, using sites like pastebin.com, but now your text is publicly available, maybe you’re wanting to send a snippet of some proprietary code or personal conversation.

My buddy Tim over at http://h1tman.com/ advanced my above screenshot auto-upload to include text excerpts, which get uploaded as plain-text. You bind his script to a keyboard shortcut and from there, you select the text, hit the keyboard shortcut and you’re off and running. You can read his full howto in depth here http://www.h1tman.com/2010/08/clipboard-hacks-social-copypasta

Now we get to the good part, I’ve combined both my original “screenshotuploader” with Tim’s “textshot” into what I’m going to call snippets. Use the same script in the screenshot plugin and in your keyboard shortcut for textshots, use the same url and same folder to store them all, in one combined script below.

#!/bin/bash

DIR="/home/chrisha/Documents/snippets/"
HOST="slice.chrishaynie.com"
HASH="$(date +%s|md5sum|awk '{print $1}')"
FNAME="$DIR$HASH"

if [ -z $1 ]
then
	FNAME="$FNAME.txt"
	xsel>$FNAME
else
	FNAME="$FNAME.jpg"
	/usr/bin/convert -bordercolor white -border 5 $1 $1
	/usr/bin/convert -bordercolor black -border 1 $1 $1
	/usr/bin/convert -quality 75 $1 $FNAME
	rm -f $1
fi

scp $FNAME $HOST:public_html/dev.chrishaynie.com/html/snippets/
echo http://slice.chrishaynie.com/snippets/$(basename $FNAME)|xsel -i
Posted in code | Tagged , , | Leave a comment

Python Threading

In order to speed up some of my scripts I’ve implemented threading using python. Some scripts now run in 1/10th the time they used to as a result.  Here I will try to illustrate some of the key points, hopefully it will be useful to anyone.

#!/usr/bin/python
import sys
import subprocess
from threading import Thread

class MyThread(Thread):
    def __init__(self,server,cmd):
        Thread.__init__(self)
        self.server = server
        self.cmd = cmd
        self.status = -1
    def run(self):
        proc = subprocess.Popen('ssh %s %s' % (self.server,self.cmd),
                       shell=True,
                       stdin=subprocess.PIPE,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.STDOUT,
                       )
        self.stdout, self.stderr = proc.communicate()

servers = ['shell1.example.net','ssh2.mhost.com','examplebox.net']
command = 'uptime'
cmdlist = []
for srv in servers:
    run = MyThread(srv,command)
    cmdlist.append(run)
    run.start()

for c in cmdlist:
    c.join()
    print "### %s:n%s" % (c.server,c.stdout),
print "## Done"

In my example we loop through servers[] and for each server[] we run the specified command on them, in their own thread! 3 servers in the above example, not a big deal, what if you have 20 servers? 40 servers? huge speed improvement.

The script can be easily extended to say… read the servers list in from a configuration file, or accept the ‘command’ from the command line arguments, (argv[1:]) etc.

Enjoy.

Posted in code | Tagged , , | Leave a comment

Bluetooth Proximity Lock for Macbook

I wanted to have my macbook automatically lock and unlock based on if I am in the room or not automatically. Best way to do that I figured would be bluetooth, since I always have my phone on me, and it has bluetooth. From there I proceeded to google.

http://web.mac.com/jhollington/technocrat/The_Technocrat/Entries/2007/3/18_Bluetooth_Proximity_Detection_on_OS_X.html

Pointed me to

http://www.apple.com/downloads/macosx/system_disk_utilities/proximity.html

he also includes a script to automate syncing your phone book and such too

Another utility in my searching, is called JackSMS

http://www.macupdate.com/info.php/id/21860

JackSMS does some cool stuff like, say someone tries to access your laptop while you’re not there – the built in isight camera will take their picture and email it then, so I wanted to use JackSMS as well.

Proximity allows you to set scripts for when you arrive or when you leave, so here are the applescripts I used:

lock.scpt:

tell application "JackSMS" to set lock screen to true

unlock.scpt:

tell application "JackSMS" to set lock screen to false

courtesy of: http://www.macosxhints.com/article.php?story=2006061914363693

update: a better proximity script, copied below, courtesy of:
http://pixelignition.net/better-proximity-applescript
lock.scpt:

global okflag
set okflag to false
set front_app to (path to frontmost application as Unicode text) -- So we can switch back to this after running the fade

-- check if iTunes is running
tell application "System Events"
        if process "iTunes" exists then
                set okflag to true --iTunes is running
        end if
end tell

if okflag is true then
        try
                tell application "iTunes"
                        set currentvolume to the sound volume
                        if (player state is playing) then
                                repeat
                                        --Fade down
                                        repeat with i from currentvolume to 0 by -1 --try by -4 on slower Macs
                                                set the sound volume to i
                                                delay 0.01 -- Adjust this to change fadeout duration (delete this line on slower Macs)
                                        end repeat
                                        pause
                                        --Restore original volume
                                        set the sound volume to currentvolume
                                        exit repeat
                                end repeat
                                set comment of current track to "Proximity Paused"
                        end if
                end tell
                tell application front_app
                        activate
                end tell
        on error
                beep
        end try
end if

tell application "JackSMS" to set jack status to "on"
delay 1
tell application "DeskShade"
        lock
end tell
end run


unlock.scpt:

tell application "ScreenSaverEngine" to quit
tell application "DeskShade"
        unlock
        quit
end tell
tell application "JackSMS"
        quit
end tell

global okflag
set okflag to false
set front_app to (path to frontmost application as Unicode text) -- So we can switch back to this after running the fade

-- check if iTunes is running
tell application "System Events"
        if process "iTunes" exists then
                set okflag to true --iTunes is running
        end if
end tell

if okflag is true then
        try
                tell application "iTunes"
                        set currentvolume to the sound volume
                        if comment of current track is "Proximity Paused" then
                                set comment of current track to ""
                                play
                                repeat with j from 0 to currentvolume by 2 --try by 4 on slower Macs
                                        set the sound volume to j
                                end repeat
                        end if
                end tell
                tell application front_app
                        activate
                end tell
        on error
                beep
        end try
end if

Posted in code | Tagged , , , , | 2 Comments