Convert Windows shortcuts into Ubuntu shortcuts
Here's another entry in the "dumb little scripts that work for me and may or may not be helpful to other folks" department…
I use both Windows and Ubuntu at home, gradually transitioning from the former to the latter. I've accumulated a bunch of Windows URL shortcuts, mostly things I wanted to read once so instead of bookmarking them, I dragged their links to my desktop. This creates .URL files which are simple little plain-text two-liners. It turns out that on Ubuntu, and probably similar *nix systems, web shortcuts are also simple little plain-text files. These files have the .desktop extension (though you won't see the extension by looking at the desktop).
I wanted a way to convert my .URL files to .desktop files so that I can just toss them on my Ubuntu desktop and double-click them the same way I would if I were on Windows. This cruddy little Python script does the trick.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #!/usr/bin/env python # shortcut_converter.py import os import sys TEMPLATE = """[Desktop Entry] Version=1.0 Encoding=UTF-8 Name=%(basename)s Type=Link URL=%(url)s Icon=gnome-fs-bookmark """ if __name__ == '__main__': for arg in sys.argv[1:]: print "Converting %s" % arg if not os.path.isfile(arg): print "*** %s is not a file" % arg continue (filepath, filename) = os.path.split(arg) (basename, extension) = os.path.splitext(filename) if extension.lower() != '.url': print "*** %s is not a URL file" % arg continue urlfile = open(arg) # strip() removes extra DOS/Windows carriage return lines = [line.strip() for line in urlfile.readlines()] urlfile.close() if lines[0] != '[InternetShortcut]': print "*** %s is not a valid URL file" % arg continue url = lines[1].split('URL=')[1] dtfile = open(os.path.join(filepath, '%s.desktop' % basename), 'w') print "Writing %s" % dtfile.name dtfile.write(TEMPLATE % locals()) dtfile.close() |
I used scp to pull over all my .URL files and then invoked the script thusly:
python shortcut_converter.py *.URL
worksforme!
Unescaping HTML in Python
Dear Future Me,
You've forgotten how to decode (or unescape) HTML or XML in Python again, haven't you? My, my, that old age does catch up with you.
Well, it turns out that xml.sax.saxutils.unescape() works like a charm. I'm certain that edge cases lurk here and there, so caveat, um, coder.
Jython scp
In spite of some open questions, I've been making some progress on my Jython-based transport tool. Right now it's pretty dumb and simple: it copies files to and fro via scp.
Being a newb at both Java and Jython made finding the right libraries a bit of a challenge, and so I'm posting some code here for folks in the same boat. It's not particularly pretty due to 1) wanting to get something working very quickly, and 2) weird errors when I try to make things prettier (such as getting rid of the hard-coded bits), but I'll resolve these soon.
Continue reading…
Jythons and Javas and bears, oh my!
It's hard to believe but I've been at the new job for six months already, a full half-year come the 29th. Some days it seems like I've been here forever; others like I'm still a rank newb. I haven't written terribly much about what I've been up to (but I assure you I've been busy). Let me rectify that.
The Transfer Problem
Two of the projects I've been working on relate to a fairly general problem that we like to call "transfer," which revolves around, well, transferring files to and fro. Sounds simple. Is simple. That is, until you start thinking about preservation and accounting for a highly heterogeneous network with idiosyncratic nodes, esoteric storage software, and differential firewall rules. And that's where it gets interesting (and problematic). Continue reading…
