CLI Browser Management

Loren (mcint) February 09, 2023 [Terminal] #cli #browser #bash #config #history

Managing browser tabs from the cli. I use some rough tools to achive my ends. I can close windows, all or excluding the front few, and I can export tabs.

Closing windows

alias osajs='osascript -l JavaScript -e'

One of 5 lines in my canonical ~/.bash_history. Due to custom history management, bash never reads this file normally, and I load it manually when I want to do one of the commonly referenced functions. history -r ~/.bash_history, followed by Ctrl-r with fzf history search.

osajs 'Application("Brave").windows().slice(1).map(w=>w.close())'
osajs 'Application("Brave").windows().map(w=>w.close())'

Exporting tabs

When I want to save a list of open tabs, I run tabs-brave.

$ declare -fp tabs-brave
tabs-brave ()
{
    : written: 2023-01-05T16:46:50 mcint@mcint-mbp.local;
    _args="$@";
    _file=~/Documents/tabs/brave/tabs.$(date +%FT%T).${_args// /-}.log;
    echo -n "$_file";
    _count=$(osascript ~/.iclouds/com~apple~ScriptEditor2/Documents/Tabs-Brave.scpt | tee -a "$_file" | grep -ve '^#' -e '^$' | wc -l;);
    echo "$_count"
}

~/.iclouds/com~apple~ScriptEditor2/Documents/Tabs-Brave.scpt

-- use AppleScript to export tabs from chromium... or any? native browser on macOS

-- output tabs in format compatible with OneTab
-- faster, low-touch (but macOS-only) way of exporting tabs

set nl to linefeed
set cntT to 0
set cntW to 0
set out to "# tabs-brave. OneTab format." & nl
tell application "Brave Browser"
    set wts to properties of tabs of windows
    repeat with w in wts
        repeat with t in w
            set out to out & nl & URL of t & " | " & title of t
            set cntT to 1 + cntT
        end repeat
        set out to out & nl
        set cntW to 1 + cntW
    end repeat
end tell
set out to out & "# end w:" & cntW & ", t:" & cntT & nl
out

I could extend this post with an example of the exported tabs file, and links to OneTabs UI docs, or screenshots of my own usage. It's calling out for custom indexing or some longer term integration of what I spend my time reading and researching.