forked from spicyjack/perl-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MozSafBookmarks.pl
executable file
·330 lines (285 loc) · 8.74 KB
/
MozSafBookmarks.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/perl
#####################################################################
#
# MozSafBookmarks.pl - version 0.0.2
#
# Changelog:
#
# Jan 20, 2003 - Non-plain characters are translated to UTF-8
# encoding.
# - Added 'usage' output if you don't give the
# script enough arguments.
#
# This is a simple script to copy bookmarks from Mozilla to Safari.
#
# Originally tossed together by one guy at Borrowed Time, Inc.
# (http://www.bti.net). The code is as pretty as an airport
# (to paraphrase Douglas Adams). While remaining ugly, it is
# completely and totally free. Do with it what you will. We won't
# be held responsible for Bad Things happening to your system,
# though.
#
# Usage:
#
# cd <Whatever directory has the script>
# perl MozSafBookmarks.pl <Moz_bookmarks.html> <Saf_Bookmarks.plist>
#
# Each argument is a path to the respective bookmark file. The
# files must already exist.
#
# All Mozilla folders, subfolders and bookmarks are imported. They
# are all placed within a single folder named "Mozilla Bookmarks"
# in Safari, and it should be the last item in the bookmark folder
# list. You will probably have to edit them once they're imported;
# this thing ain't perfect.
#
# This script directly modifies the Safari bookmark file. So you
# should have write permissions on the file, obviously. Also, I would
# highly recommend that you backup the Safari bookmark file beforehand.
# Just to be safe....
#
# The typical location for the Mozilla bookmark file is buried in
# Library/Mozilla/ (starting from your home directory). Subdirectory
# names can change, but on my system it turned out to be:
#
# Library/Mozilla/Profiles/default/15jbitmt.slt/bookmarks.html
#
# Safari's bookmark file is a little easier to find. Again, assuming
# you're starting from your home directory, it should be:
#
# Library/Safari/Bookmarks.plist
#
#####################################################################
#####################################################################
# Constants and Globals
#####################################################################
$kMozillaBookmarkFolderName = 'Mozilla Bookmarks';
$kChildTag = '<!-- Children -->';
#####################################################################
# TextFixer (text)
#####################################################################
sub TextFixer
{
use utf8;
my $text = @_[0];
$text =~ s/&/&/gi;
$text =~ s/&/&/gi;
return $text;
}
#####################################################################
# CreateSafariBookmarkLeaf (title,url)
#####################################################################
sub CreateSafariBookmarkLeaf
{
my ($title,$url) = @_;
my $entry = '';
$title = &TextFixer($title);
$url = &TextFixer($url);
$entry .= "<dict>\n";
$entry .= " <key>URIDictionary</key>\n";
$entry .= " <dict>\n";
$entry .= " <key></key>\n";
$entry .= " <string>$url</string>\n";
$entry .= " <key>lastVisitedDate</key>\n";
$entry .= " <string></string>\n";
$entry .= " <key>title</key>\n";
$entry .= " <string>$title</string>\n";
$entry .= " </dict>\n";
$entry .= " <key>URLString</key>\n";
$entry .= " <string>$url</string>\n";
$entry .= " <key>WebBookmarkType</key>\n";
$entry .= " <string>WebBookmarkTypeLeaf</string>\n";
$entry .= "</dict>\n";
return $entry;
}
#####################################################################
# CreateSafariBookmarkFolder (title)
#####################################################################
sub CreateSafariBookmarkFolder
{
my $title = @_[0];
my $entry = '';
$entry .= "<dict>\n";
$entry .= " <key>Children</key>\n";
$entry .= " <array>\n";
$entry .= " $kChildTag\n";
$entry .= " </array>\n";
$entry .= " <key>Title</key>\n";
$entry .= " <string>$title</string>\n";
$entry .= " <key>WebBookmarkType</key>\n";
$entry .= " <string>WebBookmarkTypeList</string>\n";
$entry .= "</dict>\n";
return $entry;
}
#####################################################################
# ExtractMozillaBookmark (text)
#####################################################################
sub ExtractMozillaBookmark
{
my $text = @_[0];
my ($title,$url);
$text =~ /<A HREF="([^"]+)"/i;
$url = $1;
$text =~ /">(.*?)<\/A>/i;
$title = $1;
return ($title,$url);
}
#####################################################################
# ExtractMozillaBookmarkFolderName (text)
#####################################################################
sub ExtractMozillaBookmarkFolderName
{
my $text = @_[0];
my ($folderName);
$text =~ /">(.*?)<\/H3>/i;
$folderName = $1;
return $folderName;
}
#####################################################################
# ProcessMozillaBookmarkFile (mozillaBookmarkPath,currentFolderName)
#####################################################################
sub ProcessMozillaBookmarkFile
{
my ($mozillaBookmarkPath,$currentFolderName) = @_;
my ($bookmarkFolderInsert,@folderStack,$safariBookmarks);
if (-e $mozillaBookmarkPath)
{
if (open(MOZ,"$mozillaBookmarkPath"))
{
while (<MOZ>)
{
my $oneLine = $_;
if ($oneLine =~ /<DL>/i)
{
# Beginning of a new bookmark folder
if ($bookmarkFolderInsert ne '')
{
if ($safariBookmarks ne '')
{
chomp($safariBookmarks);
$safariBookmarks .= $kChildTag;
$bookmarkFolderInsert =~ s/$kChildTag/$safariBookmarks/;
}
push(@folderStack,$bookmarkFolderInsert);
}
$bookmarkFolderInsert = &CreateSafariBookmarkFolder($currentFolderName);
$safariBookmarks = '';
}
elsif ($oneLine =~ /<\/DL>/i)
{
# End of bookmark folder
chomp($safariBookmarks);
$safariBookmarks .= $kChildTag;
$bookmarkFolderInsert =~ s/$kChildTag/$safariBookmarks/;
$safariBookmarks = '';
if (scalar(@folderStack) > 0)
{
$safariBookmarks = $bookmarkFolderInsert;
$safariBookmarks =~ s/$kChildTag//og;
$bookmarkFolderInsert = pop(@folderStack);
}
}
elsif ($oneLine =~ /<DT><H3/i)
{
# Bookmark folder name
$currentFolderName = &ExtractMozillaBookmarkFolderName($oneLine);
}
elsif ($oneLine =~ /<DT><A/i)
{
# Bookmark entry
my ($title,$url) = &ExtractMozillaBookmark($oneLine);
if ($title ne '' && $url ne '' && $url =~ /^http/i)
{
$safariBookmarks .= &CreateSafariBookmarkLeaf($title,$url);
}
}
}
close(MOZ);
}
else
{
print STDERR "Error while opening '$mozillaBookmarkPath' for reading: $!\n";
exit;
}
}
else
{
print STDERR "Can't find Mozilla bookmark file '$mozillaBookmarkPath'\n";
exit;
}
# Cleanup stray tags
$bookmarkFolderInsert =~ s/$kChildTag//og;
return $bookmarkFolderInsert;
}
#####################################################################
# InsertNewPlistIntoSafari (safariBookmarkPath,newPlistSection)
#####################################################################
sub InsertNewPlistIntoSafari
{
my ($safariBookmarkPath,$newPlistSection) = @_;
my ($safariBookmarkContents);
if (-e $safariBookmarkPath)
{
# Read the bookmark file
if (open(SAF,"$safariBookmarkPath"))
{
while (<SAF>)
{
$safariBookmarkContents .= $_;
}
close(SAF);
}
else
{
print STDERR "Error while opening '$safariBookmarkPath' for reading: $!\n";
exit;
}
# Insert new plist section just before the end of the last plist array
$safariBookmarkContents =~ s/(.*)<\/array>/$1\n$newPlistSection<\/array>/s;
# Write the file back to disk
if (open(SAF,">$safariBookmarkPath"))
{
print SAF $safariBookmarkContents;
close(SAF);
}
else
{
print STDERR "Error while opening '$safariBookmarkPath' for writing: $!\n";
exit;
}
}
else
{
print STDERR "Can't find Safari bookmark file '$safariBookmarkPath'\n";
exit;
}
}
#####################################################################
# main
#####################################################################
{
my ($mozillaBookmarkPath,$safariBookmarkPath) = @ARGV;
if ($mozillaBookmarkPath eq '' || $safariBookmarkPath eq '')
{
my @progPath = split('/',$0);
print STDOUT "\n\nUsage: ".pop(@progPath)." <Mozilla bookmark path> <Safari bookmark path>\n";
print STDOUT "\n";
print STDOUT "Possible Mozilla bookmark files:\n";
print STDOUT "--------------------------------\n";
print STDOUT `find ~ -name bookmarks.html`;
print STDOUT "\n\n";
print STDOUT "Possible Safari bookmark files:\n";
print STDOUT "-------------------------------\n";
print STDOUT `find ~ -name Bookmarks.plist`;
}
else
{
my $newPlistSection = &ProcessMozillaBookmarkFile($mozillaBookmarkPath,$kMozillaBookmarkFolderName);
if ($newPlistSection ne '')
{
&InsertNewPlistIntoSafari($safariBookmarkPath,$newPlistSection);
print STDOUT "Completed.\n";
}
}
exit;
}