Script to open all Reading List items in Safari and then import to EagleFiler

Thanks for looking at this Michael. I am still not able to solve the permissions issue, but I have automated the workaround. This could still use some cleaning up, but at least it works as I expect again. Please note where you need to enter your own <username> in the file/folder paths.



-- 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: 2019-08-20
-- Added code to copy Bookmarks.plist from Library to Downloads folder to automate the workaround
-- I tried to find a solution to the permissions issue, but ... I am still troubleshooting.
-- Added code to extend the timeout to 300 seconds, but I have not verified that this works.

--Modified: 2019-08-06
-- Updated forum post with workaround

-- 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"

try
	tell application "Finder"
		delete file "Macintosh HD:Users:**<username>**:Downloads:Bookmarks.plist"
	end tell
end try

tell application "Finder"
	copy file "Macintosh HD:Users:**<username>**:Library:Safari:Bookmarks.plist" to folder "Macintosh HD:Users:**<username>**:Downloads"
end tell

set tfile to "/Users/**<username>**/Downloads/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
		
		with timeout of 300 seconds
			
			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 timeout
	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

tell application "Finder"
	delete file "Macintosh HD:Users:**<username>**:Downloads:Bookmarks.plist"
end tell