Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Openbox 3: Pipe menu to switch themes
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Desktop Environments
View previous topic :: View next topic  
Author Message
lazarusrat
Guru
Guru


Joined: 17 Jul 2002
Posts: 305
Location: Lafayette, IN

PostPosted: Sat Feb 07, 2004 6:11 am    Post subject: Openbox 3: Pipe menu to switch themes Reply with quote

Here's another little shell script (plus awk script) I wrote for Openbox 3. It does two things:
1) If called with an argument, it sets the OB theme to that argument (e.g. type "obtheme TheBear" to switch to TheBear theme).
2) If called without arguments, it generates a pipe menu full of entries for your installed themes. Each entry will call the script with an argument, thus switching to that theme.

To use it, save the shell script somewhere in your path (e.g. /usr/local/bin/obtheme) and give it appropriate read and execute permissions. Save the awk script as /usr/local/share/obtheme.awk (That's the default location, but can be changed at the top of the shell script), and give it the appropriate read permissions. Then add an entry in your menu.xml like so:
Code:
<menu id="themes-menu" label="Themes" execute="obtheme" />


Enjoy. :)

Here's the shell script:
Code:
#!/bin/sh

#This script is used to generate an Openbox 3 (http://icculus.org/openbox)
#submenu to set the current theme. It uses an awk script to parse the
#rc file and change the theme, then makes OB reconfigure.
#If run with an argument, it sets the current theme to that argument.
#To use it, add an entry in your menu.xml somewhere like this:
# <menu id="themes-menu" label="Themes" execute="obtheme" />

#Directories in which to search for themes
#These are the ones Openbox looks in by default.
DIRS="$HOME/.local/share/themes $HOME/.themes /usr/share/themes"
#The awk script used to parse the rc file
AWKFILE=/usr/local/share/obtheme.awk
#The rc file. This is the Openbox default
RCFILE="$HOME/.config/openbox/rc.xml"
#The script creates a temporary file to store the menu entries.
#It only does this so that file can be passed through uniq and sort,
#because openbox doesn't like pipes in pipe menus.
TMPMENU=$HOME/.obmenu.tmp

#If an argument was passed in, that's the theme to set.
#So to change to theme foozwazzle, you could run 'obtheme foozwazzle'
if [ $1 ];
then
   #Copy the rc file.
   #1) so there's a backup
   #2) you can't redirect awk's output back into the file it's
   #   processing AFAIK
   cp $RCFILE $RCFILE.old
   #Get the current button layout from the rc file. If it's not
   #found, use OB's default.
   LAYOUT=$(grep -oe '<titlelayout>.*</titlelayout>' $RCFILE.old) || "0"
   if [ ${#LAYOUT} -eq 0 ];
   then
      LAYOUT="NLIMC"
   fi
   #run the awk command, redirecting the output to the rc file.
   awk -v t=$1 -v lay=$LAYOUT -f $AWKFILE $RCFILE.old > $RCFILE
   #Find out openbox' pid, and send it a SIGUSR2 signal.
   #This is the same signal that ObConf uses when it
   #changes a theme.
   OBPID=$(pgrep -u $USER openbox)
   /usr/bin/kill -s SIGUSR2 $OBPID
#Else means there were no command line arguments, so output
#the pipe menu.
else
   #Create the temporary file we'll use for sorting and uniq'ing the menu.
   touch $TMPMENU
   echo "<openbox_pipe_menu>"
   #Go through each directory where themes might be.
   for SUBDIR in $DIRS;
   do
      if [ -d $SUBDIR ];
      then
         for THEME in $SUBDIR/*;
         do
            #Make sure there's on OB-3 theme in the directory
            if [ -e $THEME/openbox-3/themerc ];
            then
               #Get just the name of the theme, create the
               #menu entry, and append it to the temp file.
               TNAME=${THEME##*/}
               START="<item label=\"$TNAME\"><action name=\"Execute\">"
               MID="<execute>obtheme $TNAME</execute>"
               END="</action></item>"
               echo "$START$MID$END" >> $TMPMENU
            fi
         done
      fi
   done
   #Make sure the menu has no duplicate entries and put it in
   #alphabetical order, ignoring case, then remove the temp file.
   sort -f $TMPMENU | uniq
   rm $TMPMENU
   echo "</openbox_pipe_menu>"
fi


And here's the awk script:
Code:
#This awk script is intended to work with obtheme.
BEGIN {c = 0; if (lay == 0) {lay = "NLIMC"}}
/<theme>/ {print "<theme>"; c = 1}
/<name>.*<\/name>/ && c == 1 {if (t == 0) {print $0} else {print "  <name>" t "</name>"}}
/<titlelayout>.*<\/titlelayout>/ && c == 1 {print "  " lay}
/<\/theme>/ { print "</theme>"; c = 2}
{if (c == 0) {print $0} else {if (c == 2) {c = 0}}}


EDIT: Updated for new kill format and switching from uniq | sort to sort | uniq
_________________
obpiper: pipe menu generator for openbox
obtheme: pipe menu to switch openbox themes


Last edited by lazarusrat on Fri Sep 01, 2006 2:52 am; edited 3 times in total
Back to top
View user's profile Send private message
TPC
Tux's lil' helper
Tux's lil' helper


Joined: 16 Sep 2003
Posts: 135
Location: Sweden

PostPosted: Sat Feb 07, 2004 1:31 pm    Post subject: Reply with quote

nice script, this is what I have been looking for :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Desktop Environments All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum