yemu
yemu

Reputation: 28279

send command to every running instance of urxvt

i'm trying to write a script that would change the color scheme of all running terminals (im using urxvt). the idea is to have a keyboard shortcut to switch between light and dark solarized themes. so far I tried dynamic-colors script https://github.com/sos4nt/dynamic-colors and it works as expected, I can change between light and dark solarized colors of the running terminal. I was thinking that maybe I could execute this command in every running instance of urxvt, but have no idea how to acomplish that. also please share any suggestions how I could tackle my main problem (creating a script that would change the colorscheme of all running terminals).

Upvotes: 1

Views: 865

Answers (2)

Neige
Neige

Reputation: 21

To change colors for all open terminals, put this script in a directory to your liking:

#!/bin/bash
DIR=$(dirname $0)

# get terminals opened by user
TERMINALS=($(ls -l /dev/pts/* | grep $USER | awk '{print $10}'))

if [[ -e "$DIR"/themes/"$1".sh ]]; then
  source "$DIR"/themes/"$1".sh

  MAX=`echo ${#TERMINALS[@]} - 1 | bc`
  for i in $(seq 0 $MAX); do
    printf "\033]10;$foreground\007" > ${TERMINALS[$i]}
    printf "\033]11;[$opacity]$background\007" > ${TERMINALS[$i]}
    printf "\033]12;$cursor\007" > ${TERMINALS[$i]}
    printf "\033]17;$highlight_bg\007" > ${TERMINALS[$i]}
    printf "\033]19;$highlight_fg\007" > ${TERMINALS[$i]}
    printf "\033]708;$border\007" > ${TERMINALS[$i]}
    for j in $(seq 0 15); do
      printf "\033]4;$j;${colors[j]}\007" > ${TERMINALS[$i]}
    done
  done
else
  echo "No theme named "$1" available!"
fi

Create a subdirectory called »themes«, e. g. /path/to/colorScript/themes, and create color themes by adjusting the following template to your favorite colors:

opacity=100
foreground="#272727"
background="#F0DCC9"
cursor="#2B2B2B"
mouse_foreground=""
highlight_bg=""
highlight_fg="" # works only if highlight_bg is set
border=""

# defining color0, …, color15
colors=(
"#000000"
"#CC2929"
"#29CC29"
"#A64934"
"#2929CC"
"#0D5F2B"
"#566F28"
"#ffffff"
"#ffff00"
"#C12D38"
"#7F2E2E"
"#ED0C11"
"#C0471B"
"#990000"
"#778087"
"#ffffff"
)

Save those templates as shell scripts (*.sh). The line

source "$DIR"/themes/"$1".sh

in the first code box of my post will import all variables defined in the color script and use them afterwards.

Execute the script for changing the terminal colors by typing

bash /path/to/colorScript/scriptName.sh "Name of your theme"

where "Name of your theme" is the exact name of the color theme in your ./themes subdirectory (without the *.sh extension).

If you want to invoke a terminal with one of your color themes instead of the default values defined in ~/.Xresources or ~/.Xdefaults, you just type:

urxvt -e bash -c 'bash /path/to/colorScript/change.sh "Name of your theme" && bash'

The line above can, of course, be used for launchers, as well. Putting the »&& bash« at the end of this line prevents the new terminal window from closing immediately and will make it respond to your input.

Upvotes: 1

yemu
yemu

Reputation: 28279

currently i'm using dynamic-colors script https://github.com/sos4nt/dynamic-colors and this bit of python code changes all terminals to light or dark (version)

pts = os.listdir('/dev/pts/')
for each_pts in pts:
     if is_number(each_pts):
         subprocess.call('echo "`.dynamic-colors/bin/dynamic-colors switch solarized-{0}`" > /dev/pts/{1}'.format(version,each_pts), shell=True)

Upvotes: 2

Related Questions