IOS – Getting a text file with the url of open tabs

automationiossafariscript

I got used to this routine my idle times:

  • Open hacker news, SE, other sources on my iphone.
  • Open interesting links as new tabs.
  • Read for a while.
  • Leave tab to remind myself to save it somewhere.

The obvious conclusion is that I found myself hitting the 500 tabs limit pretty easily. The last time I opened them manually in my macbook through iCloud, and then used a nice script to save all links with their name and url in a text file for future parsing.

I'm getting close back again (488 at the time of writing), and I'm wondering if there's anything I can do with the shortcut app that can save me at least the manual opening of 500 tabs.

Disclaimer: I know I should use pocket or Instapaper or whatever, but what I need is a solution for the already opened tabs, not for the future.

Best Answer

You can do this with AppleScript and iCloud Tabs (synchronizes your Tabs between your iOS device and your Mac). The following code with parse through all of the tabs in all of your Safari windows and write an HTML link for each one on a new line.

-- Set up the initial HTML document
set output to "<!DOCTYPE html>
" & "<html lang=\"en\">
" & "  <head>
" & "  <meta charset=\"utf-8\">
" & " <title>Safari Tab URLs</title>
" & "</head>
" & "<body>
"


tell application "Safari"

    -- Count the number of Windows
    set numWindows to number of windows


    repeat with w from 1 to numWindows

        --Count the number of Tabs
        set numTabs to number of tabs in window w

        repeat with t from 1 to numTabs

            -- Set the Tab Name and URL values
            set tabName to name of tab t of window w
            set tabURL to URL of tab t of window w

            -- Create the Tab link and write the code
            set output to output & "    <a href=\"" & tabURL & "\">" & tabName & "</a>" & linefeed as string
        end repeat
    end repeat
end tell

-- Close out the HTML document
set output to output & "</body>
" & "</html>
"

-- Write the entire HTML document to TextEdit

tell application "TextEdit"
    activate
    make new document
    set the text of the front document to output
end tell