Last active
July 16, 2022 19:14
-
-
Save thewhodidthis/918f0a45aeea42e1945a8cdea8bc5e2e to your computer and use it in GitHub Desktop.
Helps extract Bookmarks.plist data into JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# The goal is to construct a table out of two files of an equal | |
# number of lines, one for the urls and one for the titles, which are then | |
# merged together in pairs via paste(1) and converted to JSON via python. | |
# Copy Safari's bookmarks plist somewhere temporary. | |
cp ~/Library/Safari/Bookmarks.plist /tmp/Bookmarks.plist | |
# Use the property list utility to convert the copy from binary to XML text. | |
plutil -convert xml1 /tmp/Bookmarks.plist | |
# Extract the URLs in the Public bookmarks folder and order by most recent first, | |
# while prepending each with commercial at. | |
xpath -q -p '@' -e '/plist/dict/array/dict/key[.="WebBookmarkType"]/following-sibling::string[.="WebBookmarkTypeList"]/preceding-sibling::key[.="Title"]/following-sibling::string[contains(text(),"Public")]/parent::node()/array/dict/key[.="URLString"]/following-sibling::string[1]/text()' /tmp/Bookmarks.plist | tail -r > /tmp/@url.txt | |
# Extract the corresponding titles, prepend with an equals sign. | |
xpath -q -p '=' -e '/plist/dict/array/dict/key[.="WebBookmarkType"]/following-sibling::string[.="WebBookmarkTypeList"]/preceding-sibling::key[.="Title"]/following-sibling::string[contains(text(),"Public")]/parent::node()/array/dict/key[.="URLString"]/preceding-sibling::dict/key[.="title"]/following-sibling::string[1]/text()' /tmp/Bookmarks.plist | tail -r > /tmp/@title.txt | |
# Combine to incorporate potential line breaks in titles. | |
# TODO: Look into replacing split(1) with csplit(1). | |
paste -s -d " " /tmp/@url.txt /tmp/@title.txt | split -l 1 - | |
# Split again into separate files. | |
cat xaa | sed 's/ //g' | sed 's/@/\n/g' > /tmp/@url.txt | |
cat xab | sed 's/ //g' | sed 's/=/\n/g' > /tmp/@title.txt | |
paste /tmp/@url.txt /tmp/@title.txt > table.tsv | |
tail -n +2 table.tsv | python -c 'import csv, json, sys; print(json.dumps({ "links": [dict(r) for r in csv.DictReader(sys.stdin, fieldnames=["url", "title"], delimiter="\t")]}, indent=2, ensure_ascii=False))' | |
rm -rf /tmp/Bookmarks.plist /tmp/@url.txt /tmp/@title.txt table.tsv xaa xab |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment