Skip to content

Instantly share code, notes, and snippets.

@sugmak
Created March 21, 2010 14:25
Show Gist options
  • Save sugmak/339325 to your computer and use it in GitHub Desktop.
Save sugmak/339325 to your computer and use it in GitHub Desktop.
A Perl script to export Google Chrome's bookmark to HTML in order to use from LaunchBar
#!/usr/bin/env perl
use strict;
use warnings;
use JSON;
use utf8;
use Encode;
# use Data::Dumper;
my $chrome_bookmark_file = $ENV{'HOME'}.'/Library/Application Support/Google/Chrome/Default/Bookmarks';
my $new_bookmark_file = $ENV{'HOME'}.'/Documents/chrome_bookmarks.html';
my $json = &readfile($chrome_bookmark_file);
my $result = JSON->new->utf8(0)->decode(decode_utf8($json));
if ($result->{Error}) {
warn encode('utf-8', $result->{Error}{Message}), "¥n";
} else {
my $bookmarks = $result->{'roots'}->{'other'}->{'children'};
&create_chrome_bookmark_file($bookmarks);
}
sub readfile {
open(my $fh, '<', $_[0]) or die "Can't open file $_[0]: $!";
local $/;
<$fh>;
}
sub create_chrome_bookmark_file {
my $items = shift;
open my $fh ,'>', $new_bookmark_file
or die "Can't open file $new_bookmark_file: $!";
print $fh '<html><head></head><body>';
print $fh '<dl>';
foreach my $item(@$items){
if($item->{'type'} eq "url"){
print $fh '<dt>';
print $fh '<a href="'.$item->{'url'};
print $fh '" >';
print $fh encode('utf-8', $item->{'name'});
print $fh '</a>';
print $fh '</dt>';
}
}
print $fh '</dl>';
print $fh '</body></html>';
close($fh);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment