User Tools

Site Tools


uzbl_session.sh

My Session Saver

Description

*NOTE* I've added a small hack to keep this session script working with the last version of uzbl. Not sure who added the [out of date] flag but I think it can be removed – I'm not 100% sure though so, I'm not going to just go and do it :). */NOTE*

This turned out to be just a slight variation on the existing session saving scripts. I actually wrote it without looking at any others (I swear). The differences are slight and include:

* When saving the session, the currently fucused window is killed/saved last; this way, when restoring in the same order, this window will be focused again

* Use of socat instead of uzblctrl -s

* Option to close without saving

* If session file doesn't exist, just launches a new uzbl

* Can pass any additional uzbl commandline options when launching

* Launches with uzbl's 7 default args; this allows the script to be extended if desired (to what, I'm not sure yet).

note: you should keep the session file outside of /tmp if you want to keep your session across reboots.

3/23/2010 - updated with my new version; check revisions if you want the old way.

Requirements

* socat

Installation

Save the script, edit config as instructed in the script's comments

Script

#!/bin/bash
#
# http://pbrisbin.com:8080/pages/uzbl-scripts.html
#
# pbrisbin 2010
#
# an alternative session script
#
# @cbind WQ @scripts_dir/session save
# @cbind QQ @scripts_dir/session kill
#
###
 
### Utilities
# send commands from stdin to one socket
to_socket() {
  local IFS=$'\n' socket="$1"
 
  while read -r; do
    # send command to socket and filter "EVENT" and blank responses
    echo "$REPLY" | socat - unix-connect:$socket | grep -v '^EVENT\|^\ *$' 2>/dev/null
  done
}
 
# send commands from stdin to all uzbl sockets
to_all_sockets() {
  local tmp socket
 
  # a file is needed to reuse stdin for multiple sockets
  tmp='/tmp/command'
  cat /dev/stdin > "$tmp"
 
  for socket in $socket_dir/uzbl_socket_*; do
    to_socket $socket < "$tmp"
  done
 
  rm "$tmp"
}
 
### Session functions
# this may not be needed as the project
# matures, but it doesn't hurt anyway
cleanup() {
  # kill any zombies
  for pid in $(pgrep uzbl); do kill $pid; done
 
  # remove some trash
  rm -rf $socket_dir/uzbl_socket_*
  rm -rf $fifo_dir/uzbl_fifo_*
}
 
# quit all, save uris
save_session() {
  rm -rf "$session_file"
 
  echo -e "print@uri\nexit" | to_all_sockets > "$session_file" || exit 1
 
  cleanup
}
 
# quit all, no save
kill_session() {
  rm -rf "$session_file"
 
  echo 'exit' | to_all_sockets || exit 1
 
  cleanup
}
 
# open uzbl
open_session() {
  local IFS=$'\n'
 
  if [ -s "$session_file" ]; then
    while read -r url; do
      [ -n "$url" ] && $uzbl $uzbl_opts -u "$url" &>/dev/null &
    done < "$session_file"
 
  else
    $uzbl $uzbl_opts &>/dev/null &
 
  fi
}
 
### Constants
socket_dir='/tmp'
session_file="$XDG_DATA_DIR/uzbl/session"
uzbl='/usr/bin/uzbl-browser'
uzbl_opts=''
 
### Arguments
all_args=( "$@" )
 
# uzbl-called
if [ ${#all_args[@]} -ge 7 ]; then
  uzbl_config="${all_args[0]}" # path to config
  uzbl_pid="${all_args[1]}"    # current pid
  uzbl_xwinid="${all_args[2]}" # current xwin id
  uzbl_fifo="${all_args[3]}"   # path to fifo
  uzbl_socket="${all_args[4]}" # path to socket
  uzbl_url="${all_args[5]}"    # current page url
  uzbl_title="${all_args[6]}"  # current page title
 
  args=( "${all_args[@]:7}" )  # all other args
 
# cli-called
else
  args=( "$@" ) # all args
fi
 
case "${args[0]}" in
  kill) kill_session ;;
  save) save_session ;;
  open) open_session ;;
esac
uzbl_session.sh.txt · Last modified: 2016/08/31 14:10 (external edit)