Skip to content

Instantly share code, notes, and snippets.

@aryamccarthy
Created April 13, 2018 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aryamccarthy/dacb233d7f7e26b3675c20a35b20581e to your computer and use it in GitHub Desktop.
Save aryamccarthy/dacb233d7f7e26b3675c20a35b20581e to your computer and use it in GitHub Desktop.
Retrieve and list the files in the Safari reading list.
#!/usr/bin/env python
"""
Retrieve and list the files in the reading list.
"""
import plistlib
from shutil import copy
import subprocess
import os
from tempfile import gettempdir
import sys
import atexit
BOOKMARKS_PLIST = '~/Library/Safari/Bookmarks.plist'
BOOKMARKS_FILE = os.path.expanduser(BOOKMARKS_PLIST)
# Make a copy of the bookmarks and convert it from a binary plist to text
TEMP_DIRECTORY = gettempdir()
copy(BOOKMARKS_FILE, TEMP_DIRECTORY)
BOOKMARKS_FILE_COPY = os.path.join(TEMP_DIRECTORY, os.path.basename(BOOKMARKS_FILE))
atexit.register(lambda: os.remove(BOOKMARKS_FILE_COPY)) # Delete temp file when script finishes
CONVERTED = subprocess.call(['plutil', '-convert', 'xml1', BOOKMARKS_FILE_COPY])
SUCCESS_EXIT_CODE = 0
if CONVERTED != SUCCESS_EXIT_CODE:
print("Couldn't convert bookmarks plist from xml format")
sys.exit(CONVERTED)
with open(BOOKMARKS_FILE_COPY, mode='rb') as fp:
PLIST = plistlib.load(fp, fmt=None, use_builtin_types=False)
# There should only be one Reading List item, so take the first one
READING_LIST = [item for item in PLIST['Children']
if 'Title' in item and item['Title'] == 'com.apple.ReadingList'
][0]
for item in READING_LIST.get('Children', []):
if len(item["ReadingList"]) == 2:
print(item['URLString'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment