-
Notifications
You must be signed in to change notification settings - Fork 0
/
rlist.pl
executable file
·82 lines (57 loc) · 1.83 KB
/
rlist.pl
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#! /usr/bin/perl -w
# Craig Tanis
# craig-tanis@utc.edu
# http://weblar.org
#
# http://github.com/ctanis/rlist
# Wed Feb 8 09:04:06 2012
use strict;
use Carp;
use XML::Simple;
use File::Temp;
# handy for kludging your way through XML!
#use Data::Dumper;
sub get_reading_list() {
my $xml_parser = XML::Simple->new();
## dump the bookmarks plist as xml
my $xml = `/usr/libexec/PlistBuddy ~/Library/Safari/Bookmarks.plist -x -c print`;
my $bmarks = $xml_parser->XMLin($xml)
or carp;
## this is obviously a hacky way to get at the desired data (strings
## and dicts all over the place) but it seems to work
my @bmark_groups = @{ $bmarks->{'dict'}->{'array'}->{'dict'} };
my @rlist=();
for my $bg (@bmark_groups) {
# pick the group with the right label
next unless grep /com\.apple\.ReadingList/, @{ $bg->{'string'} };
# extract the url and link text from each
for my $item (@{ $bg->{'array'}->{'dict'} }) {
my $url = ${ $item->{'string'} }[0];
my $text;
for my $choice (@{ $item->{'dict'}}) {
if ($choice->{'key'} eq 'title') {
$text = $choice->{'string'};
last;
}
}
# print "**$text** -> \"$url\"\n";
push @rlist, {'url' => $url, 'text' => $text};
}
}
return @rlist;
}
#print Dumper(@rlist);
## print contents to a temporary html file
my $fh = File::Temp->new( UNLINK=>0, SUFFIX => '.html' );
# if i wanted to have perl clean up the tmp file for me...
# my $fh = File::Temp->new( SUFFIX => '.html' );
print $fh '<html><title>Safari Reading List</title><body><ol>';
for my $b (get_reading_list()) {
print $fh '<li><a href="', $b->{'url'}, '">', $b->{'text'}, '</a></li>', "\n";
}
print $fh '</ol></body>';
$fh->close();
# open in system-configured browser
system("open $fh");
# # sleep for a little bit, so that tmp file can get deleted
# sleep 3;