Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Getting the reading list items in Safari

Hi,


I'd like to parse the reading list in Safari and get all the items.


Is it possible ?


Thx.

Posted on May 23, 2015 2:54 PM

Reply
12 replies

May 26, 2015 9:16 AM in response to Combo

As a complement, Jacques has a very keen mind for AppleScript problem solving. I suggest you use his working solution.


My Python script is now an AppleScript function that returns a list of AppleScript record entries for reading list Title, and URL. But once the data is returned to AppleScript, I haven't solved it yet. I can see the individual key value pairs in their record within the list, but so far, cannot get the AppleScript semantics resolved.

May 25, 2015 11:37 AM in response to Combo

When I post perfectly syntax and alignment corrected Python code to the Jive hosting system via this editor, Jive molests the Python code. So, I have written a Perl script that fixes the havoc wrought by Jive.


Copy and paste the Perl script into a new plain text TextEdit document. The first thing you do when TextEdit opens a new document window is choose Format > Make Plain Text. Now copy/paste the following Perl script into the TextEdit document, and save as jivefix.pl. Make it executable (chmod +x jivefix.pl).


The Python code only reads the Safari Bookmarks.plist file and targets the Reading List content. It outputs the Reading List item title, and its accompanying URL.


Perl Code:

#!/usr/bin/perl

use strict;

use warnings;

#

# Cleans up common Python format errors and warnings introduced by Jive hosting

# software on Apple Support Community. Paste Python code into plain text,

# TextEdit document with .py extension. No replacement for pep8 or autopep8

# Python syntax utilities if installed.

#

# Usage: jivefix.pl < jive-paste.py > jive-fixed.py

#

# VikingOSX, May 2015, Apple Support Community


while (<>) {


# remove trailing spaces at end of line

s/\s+\n$/\n/g;

# remove preceding space from col 1 newlines

s/^ \n$/\n/g;

# insert extra newline before function def's

s/^(def)/\n$1/g;

# if there are 5 - 6 leading spaces, make it 4.

s/^ {5,6}/ /g;

# force back to 8 spaces for alignment

s/^ {10}/ /g;

# roll up to 12 space alignment

s/^ {9,11}/ /g;

# make only 12 leading spaces for or continuation

s/^ {13,15}or/ /g;

# make only 12 leading spaces for and continuation

s/^ {13,15}and/ /g;

# ensure we print current line to stdout

print "$_";


}

# make additional ending newline if missing

print "\n" if eof !~ /\n/;


Python Code:

#!/usr/bin/python

# -*- coding: utf-8 -*-


import os

import sys

import subprocess

import plistlib


input = os.path.expanduser('~/Library/Safari/bookmarks.plist')


# Read and parse the bookmarks file

pipe = (subprocess.Popen(('/usr/bin/plutil', '-convert', 'xml1', '-o',

'-', input), shell=False, stdout=subprocess.PIPE).stdout)

xml = plistlib.readPlist(pipe)

pipe.close()


section = (filter(lambda record: 'com.apple.ReadingList' ==

record.get('Title'), xml['Children']))

reading_list = section[0].get('Children')

if None in reading_list:

reading_list = []


articles = []

for item in reading_list:

print("{}\n{}\n".format(item['URIDictionary']['title'].encode('utf-8'),

item['URLString']))


sys.exit()


# eof

May 25, 2015 12:49 PM in response to VikingOSX

Thank you.


I'm not familiar with perl & python.


I'd like to integrate this function in an Automator workflow so I put your script in the "Run Shell Script" action with python selected to execute it. It works great without the perl script, just as expected but is it possible to put the result in a list object like :

{{title:"the title 1", url:"http://www.google.fr"}, {title:"the title 2", url:"http://www.yahoo.fr"}}


Etc... To pass it in another action ?


Thx.

May 26, 2015 5:32 AM in response to Combo

Combo wrote:


is it possible to put the result in a list object like :

{{title:"the title 1", url:"http://www.google.fr"}, {title:"the title 2", url:"http://www.yahoo.fr"}}



You can use "System Events" to read and parse a PLIST file

-------

set tfile to (path to library folder from user domain as text) & "Safari:bookmarks.plist"

set myRecord to {}

tell application "System Events"

repeat with i in (property list items of property list item "Children" of property list filetfile)

tell i to try

if value of property list item "Title" = "com.apple.ReadingList" then

repeat with thisDict in (get value of property list item "Children") -- get each reading list item

tell thisDict to set end of myRecord to {tURL:its URLString, tTitle:its |title| of URIDictionary} -- put the URL and the title into the record

end repeat

exit repeat

end if

end try

end repeat

end tell

return myRecord

Getting the reading list items in Safari

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.