I’ve only been coding Python for ~12 hours total, so don’t expect this to be perfect. Knowing what I know about creating/testing other software and doing my best to scour through very light pynotify documentation, I’ve begun to build out this script to be more useful / portable / configurable. As the Version 0.6 indicates, I’m not quite at my goal yet and there is still more to learn to bring it up to that point.

I’m releasing this early on my blog just in case I caught any people yesterday who’ve been experimenting with my research / code so far. I’ll share it on github when I evolve it just a bit more.

Video of it in Action

#!/usr/bin/env python
#
# Title: scp-notifications
# Author: Tyler Mulligan (tyler@detrition.net)
# Date: 06/26/2010
# Version: 0.6
# Description:
# Used in combination with an event, such as an action or cronjob, this script
# will scp the latest file from a folder to your server.
#
# Optionally, it can copy the url to your clipboard and/or show a popup with a
# link to the file after succesfully uploading
#
# Orginally developed to be piped to from compiz screenshot tool
# http://interwebninja.com/videos/compiz-screenshot-piped-to-notification-daemon-for-upload.ogv
#
# The MIT License
#
# Copyright (c) 2010 Tyler Mulligan
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE
#
 
import pygtk
pygtk.require('2.0')
import pynotify
import gtk
import sys
import os
import subprocess
 
# Set Variables
#####################################
user = "user"
host = "server.com"
# All should have trailing slashes
lfolder = "/home/user/screenshots/"
hfolder = "/home/remote_user/screenshots/"
httplink = "http://"+host+"/screenshots/"
 
 
# Display
#####################################
t1 = 5000 # timeout for screenshot upload dialog
t2 = 3000 # timeout for screenshot preview
t3 = 7000 # timeout for link dialog
 
screenshot_preview = 1 # If using with the compiz screenshot plugin, you may want this
popup_link = 1 # another popup
copy_to_clipboard = 0 # automatically copy text to keyboard
 
# Position
#####################################
 
# Get screensize < < used for relative positioning
display = gtk.gdk.display_get_default()
screen = display.get_default_screen()
x = screen.get_width() - 1
y = screen.get_height() - 1
 
# 0 for Automatic Placement
"""
x1 = 0
y1 = 0
x2 = 0
y2 = 0
x3 = 0
y3 = 0
"""
# Define Relative Position (assuming top-right)
x1 = x-1
y1 = 12
x2 = x1
y2 = y1 + 100
x3 = x-1
y3 = 12
# Define Static (1920 puts it on my second monitor)
"""
x1 = 1919
y1 = 12
x2 = x1
y2 = y1 + 100
x3 = 1920
y3 = 12
"""
#####################################
 
def upload_cb(n, action):
    assert action == "upload" 
 
    subprocess.call(["scp", os.path.join(lfolder, f), '%s@%s:%s' % (user, host, hfolder)])
 
    # setup URL in 
    if copy_to_clipboard:
        clipboard = gtk.clipboard_get()
        clipboard.set_text(httplink + f)
        # make our data available to other applications
        clipboard.store()
 
    # Notification: Link for the clicking
    if popup_link:
        n3 = pynotify.Notification("Here is your link","<a href='" + httplink + f + "'>" + httplink + f + "")
 
        helper = gtk.Button()
        icon = helper.render_icon(gtk.STOCK_DIALOG_INFO, gtk.ICON_SIZE_DIALOG)
        n3.set_icon_from_pixbuf(icon)
 
        n3.set_urgency(pynotify.URGENCY_NORMAL)
        if x3:
            n3.set_hint("x", x3)
        if y3:
            n3.set_hint("y", y3)
        n3.set_timeout(t3)
        n3.connect("closed",closen3_cb)
 
        if not n3.show():
            print "Failed to send notification"
            sys.exit(1)
 
        closen1_cb(n1)
        closen2_cb(n2)
 
    gtk.main_quit()
    sys.exit(1)
 
# Notification 1 was closed
def closen1_cb(n):
    n1.close()
    if screenshot_preview:
        n2.close()
    gtk.main_quit()
 
# Notification 2 was closed
def closen2_cb(n):
    n2.close()
    gtk.main_quit()
 
# Notification 2 was closed
def closen3_cb(n):
    n3.close()
    gtk.main_quit()
 
# The Ignore button was clicked
def ignore_cb(n, action):
    assert action == "ignore"
 
    closen1_cb(n1)
    closen2_cb(n2)
    gtk.main_quit()
 
# Main
def main():
    gtk.main()
 
# Init
if __name__ == '__main__':
    if not pynotify.init("Notifier 'scp' Option"):
        sys.exit(1)
 
    # Get latest file and build uri
    start = os.path.abspath(lfolder)
    f = max([(os.path.getmtime(os.path.join(start,p)),p)
         for p in os.listdir(start)])[1]
    uri = lfolder + f
 
    # Notification: Upload to Server
    n1 = pynotify.Notification("Upload to Server?","Copy the file '" + f + "' to the server?")
 
    helper = gtk.Button()
    icon = helper.render_icon(gtk.STOCK_DIALOG_QUESTION, gtk.ICON_SIZE_DIALOG)
    n1.set_icon_from_pixbuf(icon)
 
    n1.set_urgency(pynotify.URGENCY_NORMAL)
    if x1:
        n1.set_hint("x", x1)
    if y1:
        n1.set_hint("y", y1)
    n1.set_timeout(t1)
    n1.add_action("upload", "Yes, Upload", upload_cb)
    n1.add_action("ignore", "Ignore", ignore_cb)
    n1.connect("closed",closen1_cb)
 
    if not n1.show():
        print "Failed to send notification"
        sys.exit(1)
 
    # Notification: Screenshot Preview
    if screenshot_preview:
        n2 = pynotify.Notification("Screenshot Preview", "", uri)
        n2.set_urgency(pynotify.URGENCY_LOW)
        if x2:
            n2.set_hint("x", x2)
        if y2:
            n2.set_hint("y", y2)
        n2.set_timeout(t2)
        n2.connect("closed",closen2_cb)
 
        if not n2.show():
            print "Failed to send notification"
            sys.exit(1)
 
    main()

I also rigged up a quick gallery script to parse the incoming images http://interwebninja.com/screenshots/ — I used AutoGeneratingGallery for this.