Skip to content

Instantly share code, notes, and snippets.

@richarddas
Forked from robmathers/readinglisturls.py
Last active November 16, 2023 21:24
Show Gist options
  • Save richarddas/7e67a3edd47cd700b1e26e5abec909bd to your computer and use it in GitHub Desktop.
Save richarddas/7e67a3edd47cd700b1e26e5abec909bd to your computer and use it in GitHub Desktop.
Prints out URLs of items in Safari’s Reading List
#!/usr/bin/env python3
import plistlib
from shutil import copy
import subprocess
import os
from tempfile import gettempdir
import sys
import atexit
BOOKMARKS_PLIST = '~/Library/Safari/Bookmarks.plist'
bookmarksFile = os.path.expanduser(BOOKMARKS_PLIST)
# Make a copy of the bookmarks and convert it from a binary plist to text
tempDirectory = gettempdir()
copy(bookmarksFile, tempDirectory)
bookmarksFileCopy = os.path.join(tempDirectory, os.path.basename(bookmarksFile))
def removeTempFile():
os.remove(bookmarksFileCopy)
atexit.register(removeTempFile) # Delete the temp file when the script finishes
converted = subprocess.call(['plutil', '-convert', 'xml1', bookmarksFileCopy])
if converted != 0:
print("Couldn't convert bookmarks plist from xml format")
sys.exit(converted)
# Use plistlib.load() for Python 3
with open(bookmarksFileCopy, 'rb') as fp:
plist = plistlib.load(fp)
# There should only be one Reading List item, so take the first one
readingList = [item for item in plist['Children'] if 'Title' in item and item['Title'] == 'com.apple.ReadingList'][0]
if 'Children' in readingList:
for item in readingList['Children']:
print(item['URLString'])
@richarddas
Copy link
Author

Updated for Python 3.

  • updated env import on line 1.
  • added parenthesis to print statements.
  • use plistlib.load() for Python 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment