Wednesday, January 6, 2010

Importing Keywurl searches to Chrome

I've recently switched to using Chrome. I used Keywurl extensively with Safari. Here's a script that imports the Keywurl searches into Chrome:

#!/usr/bin/perl

use strict;
use warnings;

use Mac::PropertyList qw(parse_plist_file);
use DBI;

my $app_support = "$ENV{HOME}/Library/Application Support";

my $dbh = DBI->connect("dbi:SQLite:dbname=$app_support/Google/Chrome/Default/Web Data");

my $plist = parse_plist_file("$app_support/Keywurl/Keywords.plist");

my $keywords = $plist->{keywords};

$dbh->begin_work;

my $t = time;

my $sth = $dbh->prepare(qq{
    INSERT INTO keywords VALUES (
        NULL, -- id
        ?,    -- name
        ?,    -- keyword
        "",   -- favicon url
        ?,    -- url
        0,    -- show in default list
        0,    -- safe for auto replace
        "",   -- originating URL
        $t,   -- date created
        0,    -- usage count
        "",   -- input encodings
        "",   -- suggest url
        0,    -- prepopulate id
        0     -- autogenerate keyword
    )
});

foreach my $link ( keys %$keywords ) {
    my $data = $keywords->{$link};

    my $url = $data->{expansion}->value;

    $url =~ s/\{query\}/{searchTerms}/g;

    $sth->execute(
        $link, # name
        $link, # keyword
        $url,
    );
}

$dbh->commit;