C-Command Software Forum

Script to open iCloud tabs in Safari and then import to EagleFiler

Hello all,

I’m not sure if I ever posted this script. It is similar to my Reading List script but pulls links directly from open iCloud tabs.

As with the Reading List script, I saved this script as an app and gave it elevated permissions: Security & Privacy -> Privacy settings -> Automation and Full Disk Access.

To use this script, you will want to modify the first line of code to include the names of all of your apple devices:

set theDeviceChoices to {"All", "iPadPro11", "iPadPro13", "iPhoneMini", "MacBookPro", "MacBookAir"}

On a Mac, find the name by navigating to: System Preferences -> Sharing -> Computer Name. On an iOS device: Settings -> General -> About -> Name. I don’t use spaces in my device names, so I’m not sure if that would cause any errors.

-- import-safari-icloud-tabs
-- 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
https://stackoverflow.com/questions/10064849/read-textfile-into-list-in-applescript
https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/CallCommandLineUtilities.html
*)
-- Last Modified: 2020-07-15

--
set theDeviceChoices to {"All", "iPadPro11", "iPadPro13", "iPhoneMini", "MacBookPro", "MacBookAir"}
set theFavoriteDevice to choose from list theDeviceChoices with prompt "Import Safari iCloud Tabs from selected device:" default items {"All"}

--
if theFavoriteDevice = {"All"} then
    set theQuery to "SELECT url FROM cloud_tabs"
else if theFavoriteDevice is false then
    error number -128
else
    set theQuery to "SELECT url 
    FROM cloud_tabs 
    WHERE device_uuid = (
        SELECT device_uuid
        FROM cloud_tab_devices
        WHERE device_name = '" & theFavoriteDevice & "')"
end if


--
set theURLs to {}
set theURLs to paragraphs of (do shell script "sqlite3 ~/Library/Safari/CloudTabs.db " & quoted form of theQuery)

-- Get the length of the URL list
set listSize to (count of theURLs)

-- Script activates 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 to EagleFiler." buttons {"Cancel", "Continue"} default button "Continue" cancel button "Cancel"
    set answer to button returned of result
end repeat

-- Script activates EagleFiler
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