User Tools

Site Tools


session.sh

Session save/restore script with session editing, naming, generating, and loading. Loading a from a session can be chosed from latest session, list of all sessions, and picking a url from a session.

Github repo

#!/bin/bash

# Session script for uzbl
#
# Original author: Donald 'minozake' Johnson <LtDonny@gmail.com>.
#
# License: Do whatever you want.  CC0 if you need a specific license.
#
# Quick start: Put this into your your configuration file for uzbl:
#
#     @cbind gs = spawn @scripts_dir/session.sh @socket_dir generate
#     @cbind gl = spawn @scripts_dir/session.sh @socket_dir get
#     @cbind gL = spawn @scripts_dir/session.sh @socket_dir choose
#     @cbind ge = spawn @scripts_dir/session.sh @socket_dir edit
#     @cbind gp = spawn @scripts_dir/session.sh @socket_dir pick
#     @cbind gS = spawn @scripts_dir/session.sh @socket_dir generate_named
#
# TODO: Use a constant for `uzbl-browser' for other uzbl installs.
# TODO: Use better function names.
# TODO: Use better function location.
# TODO: Use constant for custom editor.
# TODO: Probably something else.
#
# Dependencies: zenity and socat for basic functionality.  xterm and vim for
# functionality that I just put into the script.  You can change those.


SOCKET_DIR=$1
CMD=$2
SESSION_DIR=${XDG_DATA_HOME:-$HOME/.data}/uzbl/sessions


function pick_from_session {
    session=$(ls $SESSION_DIR/* | tac | dmenu -l 20)
    uri=$(cat $session | dmenu -l 20)
    socket=$(ls $SOCKET_DIR/uzbl_socket_* | head -n1)

    uzbl-browser "$uri" &
}


function generate_session {
    # Generates a uzbl session and saves it to a session file based on date and
    # time.

    SESSION_FILE=$SESSION_DIR/uzbl_session_$(date +%F_%H%M%S)

    # Go through each socket and have it print the URL.  We append this to a file.
    for socket in $SOCKET_DIR/uzbl_socket_*; do
        echo 'print @uri' |
            socat - unix-connect:$socket |
            grep -v '^EVENT\|^ *$' >> $SESSION_FILE;
    done
}


function get_prev_session {
    # Gets latest session from the sessions in the session directory and opens
    # windows for it.

    if [ ! -d $SESSION_DIR ]; then
        exit 0
    fi

    latest_session=$(ls $SESSION_DIR/* | tail -n1)

    for uri in $(tac $latest_session); do
        uzbl-browser "$uri" &
    done
}


function generate_named_session {
    # Generates a uzbl session and saves it to a session file based on date and
    # time.

    SESSION_FILE=$SESSION_DIR/uzbl_session_$(date +%F_%H%M%S)

    SESSION_FILE=$(zenity --file-selection --filename "$SESSION_FILE")

    # Go through each socket and have it print the URL.  We append this to a file.
    for socket in $SOCKET_DIR/uzbl_socket_*; do
        echo 'print @uri' |
            socat - unix-connect:$socket |
            grep -v '^EVENT\|^ *$' >> $SESSION_FILE;
    done
}


function choose_session {
    session=$(ls $SESSION_DIR/* | tac | dmenu -l 20)

    for uri in $(cat $session); do
        uzbl-browser "$uri" &
    done
}


function edit_session {

    session=$(ls $SESSION_DIR/* | tac | dmenu -l 20)

    if [ ! "$session" ]; then
        exit
    fi

    xterm -rv -e "vim $session" &
}


function main {
    if [ ! -d $SESSION_DIR ]; then
        mkdir $SESSION_DIR
    fi


    if [ "$CMD" == "choose" ]; then
        choose_session
    elif [ "$CMD" == "edit" ]; then
        edit_session
    elif [ "$CMD" == "pick" ]; then
        pick_from_session
    elif [ "$CMD" == "generate" ]; then
        generate_session
    elif [ "$CMD" == "generate_named" ]; then
        generate_named_session
    elif [ "$CMD" == "get" ]; then
        get_prev_session
    else
        echo "print '$0: Invalid session commend.'"
    fi
}


main
session.sh.txt · Last modified: 2016/08/31 14:10 (external edit)