Tag: automation

Automatically Open All Reading List Items

As part of my job, I write a weekly newsletter, and as part of that I need to keep up with the current news, read articles, and write summaries of them. I use daily emails from a few sources (such as the CyberWire and Politico’s Morning Cybersecurity) and a RSS reader to collect possible stories. A lot of time, I use my phone when I have a few spare moments and mark articles that might be worth reading more in-depth, and I use Safari’s Reading List to do that.Reading List has some problems, though. You can’t easily open every article in the Reading List. Since I generally clear it out every day, there might be anywhere from just a couple articles up to about 15-20 at the most. I wanted to be able to open every article and then clear the list, so that I can process the articles while I’m at work. Apple doesn’t provide a good way to do this, though, so I used a combination of AppleScript and Keyboard Maestro to automate it.

I chose AppleScript to open the articles. Apple doesn’t provide easy access to your Reading List. Instead, it’s saved as part of your Safari Bookmarks file. I used this post by Jacques Rioux as a template, and modified it a bit to just get the URLs of articles in the Reading List.

-- Sets tfile to look at Safari's bookmarks file, which contains the Reading List
set tfile to (path to library folder from user domain as text) & "Safari:bookmarks.plist"

-- Creats a blank list, which will later have Reading List items added to it.
set theURLs to {}

tell application "System Events"

    -- Check each item in the Safari bookmarks file 
    repeat with i in (property list items of property list item "Children" of property list file tfile)
        tell i to try

            -- Check the item to see if it is a part of the Reading List
            if value of property list item "Title" = "com.apple.ReadingList" then
                repeat with thisDict in (get value of property list item "Children")

                    -- Add item to list of URLs to open
                    tell thisDict to set end of theURLs to its URLString
                end repeat
                exit repeat
            end if
        end try
    end repeat
end tell

This created a list of URLs from my Reading List. Then, I used some more AppleScript to take that list of URLs and open them in Safari. First, this checks if the Reading List is empty. If there’s no articles in it, then the script stops. Otherwise, I take the first URL and open it in a new window, then I take each subsequent URL and open it in a new tab. This section of the script is based off of this answer on Stack Overflow.

tell application "Safari"
    -- If there are no items in the Reading List then do not open Safari
    -- TODO change this to pop up a notifcation that the Reading List is empty
    if theURLs = {} then
        return
    else

        -- Get first item of list so that it can be used to create a new window, while the rest of the list are used to create new tabs..
        set {firstURL, restURLs} to {item 1 of theURLs, rest of theURLs}
    end if

    -- Make new window with the first URL in the Reading List
    make new document at end of documents with properties {URL:firstURL}

    -- Make new tabs with each of the other URLs in the Reading List
    tell window 1
        repeat with theURL in restURLs
            make new tab at end of tabs with properties {URL:theURL}
        end repeat
    end tell
end tell

I then used Keyboard Maestro in order to run this script. This let me launch it from a hotkey shortcut, or what I’ve been doing more often, using the Keyboard Maestro menubar icon to run it. After the AppleScript is executed, then I activate Safari and use Keyboard Maestro’s ability to choose menu items to open a new tab.

blog_openReadingList_1

Since my new tabs open with the Reading List visible in the side bar, then I can just right-click an article on the sidebar and clear my Reading List.

blog_openReadingList_2

In the future, I’d like to automate the clearing of items in the Reading List. Apple doesn’t provide an easy way to do so programmatically, and so I think I might have to do it by using Keyboard Maestro’s GUI scripting. I don’t like to do that because the scripts aren’t very resilient, and often break when anything is updated. I’ll have to see how annoyed I am by having to right-click in order to clear the Reading List, and my level of annoyance at that will determine whether I put the time in to try to automate that part of the process.