TextExpander: List Browser Tabs

Like many of us, the Internet is my go to place for immediate access information. It allows me to quickly research topics via my web browser, but for more involved topics it can be difficult for me to get enough detail in a single sitting, or I may need to pass on some details elsewhere (e.g. a reference list for a document/post/article). But when I have several tabs open (potentially across several windows) it can be a pain to manually list out the links and pages. To that end I created an AppleScript based TextExpander snippet to produce such a list.

Overview

At a high level the snippet works by working through the available browser windows and creating a list of browser tabs. For each tab the snippet then fetches the page’s title and web address (URL). These are then formatted and the formatted list returned for insertion by TextExpander.

Note that because this is AppleScript based it will not work on iOS only OS X devices.

The snippet (specified below) is intended to be customised using a number of settings to meet your specific needs. This means you can set-up the snippet multiple times and just vary the settings to get what you need.

Settings

strBrowser

This string is used to specify which browser to work with. Google Chrome can be specified by setting this string to “Chrome” or to Apple Safari by setting this string to “Safari”. If you favour another browser such as Mozilla Firefox or Opera then it should be possible to extend the functionality of the script to accommodate it (if the browser is scriptable via AppleScript).

strStyle

There are eight in-built formatting styles set by specifying the strStyle variable. These include plain text, hyperlinked HTML, hyperlinked Markdown and various list formatting styles.

Style: Text

Setting the string to “Text” will cause the tab entries to be listed out with the title and URL on consecutive lines and a blank line in between.

Style: HTML-P

Setting the string to “HTML-P” will format the tab entries as HTML. Each tab entry will display the title as the text and will link to the URL. Each tab entry will be enclosed in HTML paragraph tags meaning that when rendered as HTML there will typically be large gaps between entries.

Note: Applying CSS styling may affect any of the HTML or Markdown based output styles when rendering as HTML.

Style: HTML-BR

Setting the string to “HTML-BR” will format the tab entries as HTML. Each tab entry will display the title as the text and will link to the URL. Each tab entry will end with an HTML line break tag meaning that when rendered as HTML consecutive entries will be displayed on consecutive lines.

Style: HTML-UL

Setting the string to “HTML-UL” will format the tab entries as HTML. Each tab entry will display the title as the text and will link to the URL. Each tab entry will be displayed as a bulleted (unordered) list item when rendered as HTML.

Style: HTML-OL

Setting the string to “HTML-OL” will format the tab entries as HTML. Each tab entry will display the title as the text and will link to the URL. Each tab entry will be displayed as a numbered (ordered) list item when rendered as HTML.

Style: MD

Setting the string to “MD” will format the tab entries as Markdown. Each tab entry will be set out as a Markdown hyperlink with the title being used for the link text and the link title and the URL being set as the link URL. Each tab entry will be on a single line and when rendered as HTML would be be displayed on consecutive lines.

Style: MD-UL

Setting the string to “MD-UL” will format the tab entries as Markdown. Each tab entry will be set out as a Markdown hyperlink with the title being used for the link text and the link title and the URL being set as the link URL. Each tab entry will be on a single line and prefixed with an asterisk; which when rendered as HTML would will be displayed as a bulleted (unordered) list item.

Style: MD-OL

Setting the string to “MD-OL” will format the tab entries as Markdown. Each tab entry will be set out as a Markdown hyperlink with the title being used for the link text and the link title and the URL being set as the link URL. Each tab entry will be on a single line and prefixed with a 1 followed by a period (“1.”); which when rendered as HTML would will be displayed as a numbered (ordered) list item.

strLineEnd

The strLineEnd variable is intended to be included at the end of each tab entry. By default (below) it is set as a period (“.”), but you could set it to be an empty string (“”) or any other set of characters you like. For hyperlinked entries (styles other than “Text”), the line end string will be added after the entry and not be included in the hyperlinked text. For the non-hyperlinked entries (style of “Text”), the line end string is included after the title but it is not included after the URL.

Text Substitution

If you examine the AppleScript closely you may notice that there is also a text based substitution routine applied to the tab title. This is set to replace double quotes with single quotes as double quotes are used in setting attributes in HTML and MarkDown links.

Snippet Details

With the above in mind, the snippet details presented below are intended to be amended to suit your needs. Create a snippet and set the settings as you require (below it is defaulted to producing a bulleted Markdown list from Chrome with entries ending with a period), assign a suitable abbreviation then simply replicate and amend for any further variations you might require - I have a half dozen or so combinations I tend to use.

Label Browser Tab List - Chrome (AS)
Description List the open Chrome browser tabs in a hyperlinked Markdown bullet list with entries ending with a period.
Abbreviation CTMDUL.
Content
-- Initialise the script settings
-- Set the browser.
--   Valid options are [ "Chrome" | "Safari" ]
set strBrowser to "Chrome"
-- Set the output style.
--   Valid options are [ "Text" | "HTML-P" | "HTML-BR" | "HTML-UL" | "HTML-OL" | "MD" | "MD-UL" | "MD-OL" ]
--     Text = Title on one line, URL on next line, blank line.
--     HTML-P = Title as HTML hyperlink with one link per paragraph.
--     HTML-BR = Title as HTML hyperlink with one link per line.
--     HTML-UL = Title as HTML hyperlink with one link per entry in a bulleted list.
--     HTML-OL = Title as HTML hyperlink with one link per entry in a numbered list.
--     MD = Title as Markdown hyperlink with one link per line.
--     MD-UL = Title as Markdown hyperlink with one link per entry in a bulleted list.
--     MD-OL = Title as Markdown hyperlink with one link per entry in a numbered list.
set strStyle to "MD-UL"
-- Set the line end.
--   This is added to the end of each line of output (but will not be included in the hyperlink
set strLineEnd to "."


-- Initialise the output string
set strOutput to ""

if strBrowser = "Chrome" then
    tell application "Google Chrome"
        -- Get all the Chrome Windows
        set listChromeWindows to every window
        -- Loop through every Chrome window
        repeat with objChromeWindow in listChromeWindows
            -- Get all the Chrome Tabs
            set listChromeTabs to every tab in objChromeWindow
            -- Loop through every Chrome tab
            repeat with objTab in listChromeTabs
                -- Retrieve the tab properties
                set strTitle to the title of objTab
                set strURL to the URL of objTab
                -- Change double to single quotes in the title as these are used in 
                -- specifying hyperlink attributes
                set strTitle to my Substitute(strTitle, "\"", "'")
                -- Call the top level handler to format the properties
                set strOutput to strOutput & my formatOutput(strTitle, strURL)
            end repeat
        end repeat
    end tell
else if strBrowser = "Safari" then
    tell application "Safari"
        -- Get all the Safari Windows
        set listSafariWindows to every window
        -- Loop through every Safari window
        repeat with objWindow in listSafariWindows
            set listSafariTabs to every tab in objWindow
            -- Get all the Safari Tabs
            repeat with objTab in listSafariTabs
                -- Retrieve the tab properties
                set strTitle to the name of objTab
                set strURL to the URL of objTab
                -- Change double to single quotes in the title as these are used in 
                -- specifying hyperlink attributes
                set strTitle to my Substitute(strTitle, "\"", "'")
                -- Call the top level handler to format the properties
                set strOutput to strOutput & my formatOutput(strTitle, strURL)
            end repeat
        end repeat
    end tell
else
    set strOutput to "ERROR: Unknown Browser"
end if


-- Wrap the output in the HTML for bulleted lists
if strStyle = "HTML-UL" then
    -- Remove the last return
    set strOutput to (characters 1 thru ((count of characters of strOutput) - 1) of text of strOutput) as text
    set strOutput to "" & return & strOutput & return & ""
end if


-- Wrap the output in the HTML for ordered lists
if strStyle = "HTML-OL" then
    -- Remove the last return
    set strOutput to (characters 1 thru ((count of characters of strOutput) - 1) of text of strOutput) as text
    set strOutput to "" & return & strOutput & return & ""
end if


-- Finally return the output
return strOutput
?display dialog strOutput buttons {"Ok"}


-- HELPERS
-- Format the tab properties
on formatOutput(p_strTitle, p_strURL)
    if my strStyle = "Text" then
        return p_strTitle & my strLineEnd & return & p_strURL & return & return
    else if my strStyle = "HTML-P" then
        return "" & p_strTitle & "" & my strLineEnd & "" & return
    else if my strStyle = "HTML-BR" then
        return "" & p_strTitle & "" & my strLineEnd & "
" & return
    else if my strStyle = "HTML-UL" then
        return tab & "" & p_strTitle & "" & my strLineEnd & "" & return
    else if my strStyle = "HTML-OL" then
        return tab & "" & p_strTitle & "" & my strLineEnd & "" & return
    else if my strStyle = "MD" then
        return "[" & p_strTitle & "](" & p_strURL & " \"" & p_strTitle & "\")" & my strLineEnd & return
    else if my strStyle = "MD-UL" then
        return "* [" & p_strTitle & "](" & p_strURL & " \"" & p_strTitle & "\")" & my strLineEnd & return
    else if my strStyle = "MD-OL" then
        return "1. [" & p_strTitle & "](" & p_strURL & " \"" & p_strTitle & "\")" & my strLineEnd & return
    else
        return "ERROR: Unknown Style"
    end if
end formatOutput

-- Replace one set of characters in a string with another set
on Substitute(p_strSource, p_strFind, p_strReplace)
    -- Save the initial delimiter
    set strInitialDelimiter to text item delimiters of AppleScript
    
    -- Carry out the substitution by delimiting using the find string and then
    -- converting back to a string using the new replacement text as the delimiter
    set AppleScript's text item delimiters to the p_strFind
    set the listTextItems to every text item of p_strSource
    set AppleScript's text item delimiters to the p_strReplace
    set strOutput to the listTextItems as string
    
    -- Restore the initial delimiter
    set AppleScript's text item delimiters to strInitialDelimiter
    
    -- Return the new string
    return strOutput
end Substitute

These snippets can be downloaded as part of the Thought Asylum web snippet group.

Author: Stephen Millard
Tags: | applescript | textexpander |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read