Hello all,
I have assembled an AppleScript to help automate the process of importing links from Safari’s Reading List to EagleFiler. I would appreciate feedback and any suggestions for improvement–it hasn’t failed on me yet, but I am especially interested in code or strategies to make it more robust.
Background: I often save dozens if not hundreds of links to the Reading List in Safari (mostly on iOS) over the course of a few months. To get these links into my archive library in EagleFiler, I then periodically open 10-20 in Safari tabs at a time and use the standard “Import Safari Tabs” AppleScript. This is a slow and rather labor intensive process. At various times, I have tried opening 30, 40, 50, …, 100 tabs at a time, but for me these large imports have not been uniformly successful (I haven’t looked into the problem in detail to know why the large import fails).
Assumptions: The script is saved in the EagleFiler script folder, Safari is open but all windows are closed***, EagleFiler is open and is the active application.
***This script works only on visible Safari windows, so if you have open windows that you do not want to import then just minimize them to the dock.
Workflow for this script:
- EagleFiler -> AppleScript menu item -> Import Safari Reading List (or whatever you named it)
- Safari will be selected as the active application and windows will start opening each with 12 tabs (if there are less then 12 links in the Reading List then that number of tabs will open in only one window)
- A dialog box will open and ask that you wait for all of the tabs to finish loading. This is also where you can review and close any unwanted tabs. Click “Continue” when you are ready to proceed.
- EagleFiler is selected as the active application. The script will loop through each window and import up to 12 tabs at a time. You can open the activity window to monitor the progress.
Additional steps: Unfortunately, this script does not clean up after itself. You will still need to close all of the Safari windows (this should be trivial, but I didn’t fit it in today). You will also need to open the Reading List in Safari and “Clear All Items …” (this seems much less trivial based on needing to edit the plist file?).
Citation links to the material that I used to assemble this script can be found in the header.
-- import-safari-reading-list
-- Modified From:
(*
http://c-command.com/scripts/eaglefiler/import-safari-tabs
https://workentin.wordpress.com/2016/01/10/automatically-open-all-reading-list-items/
https://discussions.apple.com/message/28304986#28304986
http://stackoverflow.com/questions/11706171/how-to-open-a-new-window-and-multiple-urls-in-safari-with-apple-script
https://gist.github.com/edenwaith/2213a764ccb091d6a03989f238efb63f
https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateListsofItems.html
https://discussions.apple.com/thread/6730717?start=0&tstart=0
https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_operators.html
*)
-- Last Modified: 2017-02-06
-- 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 {}
-- System Events lets you access the content of a plist
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
-- Get the length of the URL list
set listSize to count of theURLs
-- Script switches to Safari
activate application "Safari"
tell application "Safari"
-- This is the main loop
-- Loop opens as many new windows each with 12 tabs as necessary
-- Found that importing more than 12 tabs in EagleFiler can sometimes fail unexpectedly
set i to 1
repeat until i is greater than listSize
if i is greater than listSize then
exit repeat
else
-- Opens a new window with one URL
set {firstURL} to {item i of theURLs}
make new document at end of documents with properties {URL:firstURL}
set i to i + 1
end if
repeat with j from 1 to 11
if i is greater than listSize then
exit repeat
else
-- Opens additional tabs (up to total of 12) in the new Window
tell window 1
set {theURL} to {item i of theURLs}
make new tab at end of tabs with properties {URL:theURL}
end tell
set i to i + 1
end if
end repeat
end repeat
end tell
-- Open a dialog box to allow tab trimming before import
-- Also acts as a delay to allow URLs to open
set answer to ""
repeat while answer is equal to ""
display dialog "Check that all Safari tabs have loaded. Close any tabs that you do not want to import." buttons {"Continue"} default button 1
set answer to button returned of result
end repeat
activate application "EagleFiler"
-- Import to EagleFiler one window at a time
tell application "Safari"
set windowList to (every window whose visible is true)
repeat with windowVariable in windowList
set _urls to URL of every tab of windowVariable
tell application "EagleFiler"
import URLs _urls
end tell
end repeat
end tell