scripts /

#!/usr/bin/env python
"""Some Firefox tools.

2010 (c) Andrey Mikhaylenko

Unfortunately we cannot access the bookmarks and history because Firefox locks
places.sqlite until you shut it down (i.e. it opens the database and keeps the
connection open). This is crazy.
import os
import json
from argh import *

def get_profile_dir():
        # example: ~/.mozilla/firefox-4.0/wa4cdf2v.default/
        path = os.path.expanduser(os.environ['FIREFOX_PROFILE_DIR'])
    except KeyError:
        raise RuntimeError('Environment variable FIREFOX_PROFILE_DIR must '
                           'contain full path to the desired Firefox profile.')
        assert os.path.exists(path)
        return path

def get_file(path):
    profile_dir = get_profile_dir()
    return os.path.join(profile_dir, path)

def tabs():
    "Prints current Firefox tabs (URLs and titles)."
    path = get_file('sessionstore.js')
    session = json.load(open(path))
    windows = session['windows']
    for window in windows:
        tabs = window['tabs']
        for tab in tabs:
            # grab the last entry of the tab history
            entry = tab['entries'][-1]
            yield entry.get('title', '-no title-')
            yield entry['url']
            yield ''

if __name__=='__main__':
    parser = ArghParser(description=__doc__)