A look at Lookit 0.3

Well, maybe “look” isn’t quite the right word, there’s not a lot of GUI to look at. However, I’ve spent a large portion of the day figuring out how to get around two of the more annoying issues in the current version, specifically, non-compositing window manager support and drawing the selection rectangle.

The new solution involves grabbing the mouse directly rather than letting the input be grabbed by an invisible window. That alone improves the application dramatically by allowing it to run without a compositing manager. With some luck, this will help with multimon support as well. Drawing the rectangle requires some lower level code, though, specifically using Xlib instead of GDK.

If there’s anything that you want to see implemented in the next release, make sure it’s mentioned on the Bug Tracker.

Lookit 0.2 Released

It’s a new project, and I’m moving fast. I just pushed Lookit 0.2 to bzr as well as the Lookit PPA. Changes include support for Imgur as an upload option, a warning when using a non-compositing window manager, Ubuntu Mono icons, and a few minor bugfixes. As always, *please* report any bugs to http://bugs.launchpad.net/lookit.

EDIT: I’ve moved to github, report bugs here: http://github.com/zachtib/lookit/issues

Lookit 0.1 Released

After around two weeks of coding in my freetime, I yesterday uploaded the first public release of my screenshot utility, Lookit. Unlike my bash script, this version has a proper GUI and no longer relies on external utilities like scrot and xclip. Instead, everything is done in native Python.

EDIT: Ubuntu 10.04 packages can be found on the project’s page on Launchpad: http://launchpad.net/lookit.

Other development is done at github: http://zachtib.github.com/lookit/

Generating Gnome Rotating Backgrounds

The version of Gnome in Ubuntu 10.04 has the ability to automatically change through a defined set of files. Unfortunately, there’s no simple way to do this via the gui (I originally assumed that you just assigned a folder to be your background) so I whipped up a quick Python script. This could probably have been done in bash, but I took the lazy way out. I’m also sure I could have used some existing XML library, but the file is fairly simple, so I just used file.write(). If any glaring errors are pointed out, or if I feel like implementing XML properly, I’m sure I’ll post a revised version sometime soon, but for now, here’s the program:

Here’s a link to the source, in case WordPress fubars Python’s indentation.

#!/usr/bin/python

import os, os.path, sys, optparse

IMG_FILETYPES = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg']

def gen_bg_xml(directory, duration=1795.0, transition=5.0):
    if not os.path.isdir(directory):
        print “Error: {0} is not a valid directory”.format(directory)
        sys.exit(2)

    xmlfile = os.path.join(directory, “background.xml”)
    xml = open(xmlfile, mode=’w')

    xml.write(“<background>\n”)
    xml.write(“  <starttime>\n”)
    xml.write(“    <year>2010</year>\n”)
    xml.write(“    <month>01</month>\n”)
    xml.write(“    <day>01</day>\n”)
    xml.write(“    <hour>00</hour>\n”)
    xml.write(“    <minute>00</minute>\n”)
    xml.write(“    <second>00</second>\n”)
    xml.write(“  </starttime>\n”)

    files = os.listdir(directory)
    l = files[:] # Copy the list so we have something to iterate through
    for f in l:
        name, ext = os.path.splitext(f)
        if not ext in IMG_FILETYPES:
            files.remove(f)
            continue

    for f in files:
        absf = os.path.join(directory, f)
        if not files.index(f) == 0:
            xml.write(“    <to>{0}</to>\n”.format(absf))
            xml.write(“  </transition>\n”)
        xml.write(“  <static>\n”)
        xml.write(“    <duration>{0}</duration>\n”.format(duration))
        xml.write(“    <file>{0}</file>\n”.format(absf))
        xml.write(“  </static>\n”)
        xml.write(“  <transition>\n”)
        xml.write(“    <duration>{0}</duration>\n”.format(transition))
        xml.write(“    <from>{0}</from>\n”.format(absf))
        if files.index(f) == len(files) – 1:
            xml.write(“    <to>{0}</to>\n”.format(
                      os.path.join(directory, files[0])))
            xml.write(“  </transition>\n”)
    xml.write(“</background>”)
    xml.flush()
    xml.close()

if __name__==”__main__”:
    p = optparse.OptionParser(usage=”usage: %prog [options] directory”)
    p.add_option(‘–duration’, ‘-d’, default=”1795.0″)
    p.add_option(‘–transition’, ‘-t’, default=”5.0″)
    options, arguments = p.parse_args()
    if not len(arguments) == 1:
        p.error(“incorrect number of arguments”)
    sys.exit(1)
    gen_bg_xml(arguments[0], options.duration, options.transition)

Whew, escaping all those tabs was annoying.

Fast screenshot sharing in Linux, Part 2

So, thanks to a post on twitter, I’ve found a better way to snag screenshots by using scrot instead of compiz. This doesn’t give the graphical corruption that imagemagick does while compiz is running, and has the added bonus a being able to single-click a window to grab the entire window quickly.

So, here’s my revised source:

#!/bin/bash

user=
server=
destdir=
httpstr=

filename=`scrot -s -b -e ‘echo $f’`

md5=`md5sum $filename`
if [ "$?" -ne 0 ]; then
    notify-send “Error” “Could not generate md5sum” -i error
    exit
fi

md5=${md5/ */}

scp $filename “$user@$server:$destdir${md5}.png”
if [ "$?" -ne 0 ]; then
    notify-send “Error” “Failed upload” -i error
    exit
fi

longurl=$httpstr$md5.png

shorturl=`wget http://is.gd/api.php?longurl=$longurl -O-`
if [ "$?" -ne 0 ]; then
    notify-send “Error” “Failed to shorten URL” -i error
    $shorturl=$longurl
fi

echo $shorturl | xclip -selection clipboard

notify-send “Upload Complete” $shorturl

rm $filename

Save it and bind the script to a hotkey and you should be good to go.

See the original post for the rest of the setup.

Fast screenshot sharing in Linux

When I’m using Windows or OS X, I use a utility called Tinygrab to quickly share screenshots over the internet, whether it’s via Twitter, IM, email, etc. The concept is simple: you press a key combination, select a region of your screen, and it uploads a screenshot of that area and gives you a URL for sharing. Unfortunately, they don’t (currently) support Linux, and so I decided to create my own version.

Since this was the result of an hour or two of bash scripting, it’s somewhat limited in what it can do. I’m planning on expanding it in the near future to support GUI configuration and not rely on Compiz in order to take the screenshot. Since Compiz causes some wackyness when it comes to grabbing screens, I’m using Compiz’s Screenshot plugin, which you can enable in CompizConfigSettingsManager.  By default, the shortcut is to hold Super (aka, the Windows key) and drag your mouse button to select an area to grab. You’ll need to set two options in order for my script to work. First, the directory to save the images to. I used ~/Desktop, since the script deletes the image after it has been uploaded, however you could also use something like /tmp if you like. Second, the command to be run on the screenshot after it has been saved, which is the script you see below. I saved it to ~/bin/upload_image, but again, you can call it whatever you like, just be sure to make the file executable.

The next step is to install the necessary dependencies. Since I’m reply on a few applications, you’ll have to have the following packages installed (this is on Ubuntu 10.04), but again, I’m hoping to change this in the near future: libnotify-bin and xclip.

The script also assumes you have passwordless ssh set up with your server (yes, you do need your own server for the moment, I’m hoping I can change this in future versions) You can easily do this in Ubuntu via the “Password and Encryption Keys” item under Accessories.

Now that all that is in place, it’s time for the script. You’ll notice there are a few values you need to fill in:

#!/bin/bash

user=
server=
destdir=
httpstr=

if [ ! -e $1 ]; then
    notify-send “Error” “File does not exist” -i error
    exit
fi

md5=`md5sum $1`
if [ "$?" -ne 0 ]; then
    notify-send “Error” “Could not generate md5sum” -i error
    exit
fi

md5=${md5/ */}

scp $1 “$user@$server:$destdir${md5}.png”
if [ "$?" -ne 0 ]; then
    notify-send “Error” “Failed upload” -i error
    exit
fi

longurl=$httpstr$md5.png

shorturl=`wget http://is.gd/api.php?longurl=$longurl -O-`
if [ "$?" -ne 0 ]; then
    notify-send “Error” “Failed to shorten URL” -i error
    $shorturl=$longurl
fi

echo $shorturl | xclip -selection clipboard

notify-send “Upload Complete” $shorturl

rm $1

Those values that you need to fill in are:

user: your username on the server

server: the address of the server you’re uploading your images to

destdir: a directory on that server that’s accessible via the web, such as /var/www/screengrabs/

httpstr: the url to that directory, for example: http://www.mysite.com/screengrabs/

Once all that’s set up, try it out. Hold Super and drag your mouse to select an image on your screen. Wait a few seconds and you should either get an “Update Complete” notification, or an error with what went wrong. If the upload was a success, you’ll have a url on your clipboard shortened with is.gd, ready for the pasting.

Enjoy

Update: Bonus: Here’s how you can make use of my script without needing Compiz. I haven’t tested it yet, but it should work. You’ll need Imagemagick installed.

#!/bin/bash

import /tmp/screenshot.png

/path/to/other/script /tmp/screenshot.png

Save that and bind it to a hotkey and you should be good to go.