Topic: Switch light/dark theme with keybindings

I like the dark look of Crunchbang Linux, but on very sunny days the dark theme is a little bit
hard on the eyes. So I switch between a dark theme for the night and a light one for the day. I'm
aware of this "day and night theme" which switches the appearance of the screen time-controlled,
but I want to set it manually.

This is my solution:

First create a light Openbox theme file with the obconf command:

  $ obconf --archive /usr/share/themes/YourLightTheme

This creates the file YourLightTheme.obt in your working directory.
Then move this file to the theme folder in your home directory:

  $ mv YourLightTheme.obt ~/.themes/

Repeat these steps for a dark theme!


Paste the following lines to your ~/.config/openbox/rc.xml and adjust them to your needs:

    <!-- Set a light wallpaper and theme -->
    <keybind key="C-S-F1">
      <action name="Execute">
        <startupnotify>
          <enabled>true</enabled>
          <name>LightWallpaper</name>
        </startupnotify>
        <command>nitrogen --set-centered ~/images/backgrounds/LightWallpaper.jpg</command>
      </action>
    </keybind>
    <keybind key="C-S-F1">
      <action name="Execute">
        <startupnotify>
          <enabled>true</enabled>
          <name>LightTheme</name>
        </startupnotify>
        <command>obconf --install ~/.themes/YourLightTheme.obt</command>
      </action>
    </keybind>
    <!-- Set a dark wallpaper and theme -->
    <keybind key="C-S-F2">
      <action name="Execute">
        <startupnotify>
          <enabled>true</enabled>
          <name>DarkWallpaper</name>
        </startupnotify>
        <command>nitrogen --set-scaled ~/images/backgrounds/DarkWallpaper.png</command>
      </action>
    </keybind>
    <keybind key="C-S-F2">
      <action name="Execute">
        <startupnotify>
          <enabled>true</enabled>
          <name>DarkTheme</name>
        </startupnotify>
        <command>obconf --install ~/.themes/YourDarkTheme.obt</command>
      </action>
    </keybind>

Reconfigure Openbox: Menu -> Preferences -> Openbox Config -> Reconfigure

After that you can select the light setting with "Control + Shift + F1" and
the dark setting with "Control + Shift + F2"

Anyone know a way to change the theme.obt without the Openbox Configuration Manager popping up?
I'd appreciate that!

Acer Extensa 5220 || Debian "Squeeze" & Fluxbox | #! 10 "Statler" Alpha & Openbox
identi.ca/herrgruen

Re: Switch light/dark theme with keybindings

you could take the portion from my theme installer here http://crunchbanglinux.org/forums/topic … installer/ that sets the openbox theme, it manually edits the rc.xml.

edit: another option if it's only two themes you use is just have two rc.xml's and switch between them

I say never be complete, I say stop being perfect, I say lets evolve, let the chips fall where they may.

Re: Switch light/dark theme with keybindings

iggykoopa wrote:

you could take the portion from my theme installer here http://crunchbanglinux.org/forums/topic … installer/ that sets the openbox theme, it manually edits the rc.xml.

I will definitly try that! Thank you! smile

iggykoopa wrote:

edit: another option if it's only two themes you use is just have two rc.xml's and switch between them

Yes, I was thinking about that. But then I have to maintain two rc.xml files with every change I make.

Acer Extensa 5220 || Debian "Squeeze" & Fluxbox | #! 10 "Statler" Alpha & Openbox
identi.ca/herrgruen

Re: Switch light/dark theme with keybindings

it shouldn't be too bad with the rc.xml's since the only difference would be the theme, just make whatever changes you need to one, make a copy of it, then change the theme with obconf  on the original. You should be good to go with that.

I say never be complete, I say stop being perfect, I say lets evolve, let the chips fall where they may.

Re: Switch light/dark theme with keybindings

hi

i just made a little script, thanks ihhykoop for the idea

#!/bin/bash
#switch-theme.sh
#

cd /home/$USER/.config/openbox

if [ -e rc.xml.light ]; then 
    mv rc.xml rc.xml.dark
    mv rc.xml.light rc.xml
    echo "switching from dark to light"
else
    mv rc.xml rc.xml.light
    mv rc.xml.dark rc.xml
    echo "switching from light to dark"
fi

openbox --restart

exit 0

Re: Switch light/dark theme with keybindings

It's very nice and opens up a few wishful possibilities ...

http://omploader.org/tMjlmYw

to

http://omploader.org/tMjlmZA

....It now would obviously be great to call a script in the same way that changed, OB theme, GTK theme, Nitrogen or other desktop, Icon theme and a few other features all in once nice script.

I blog too much....       geek stuff LinuxMintDebian | linux noob stuff LinuxMintNoob | spiritual stuff Daily Cup of Tao

Re: Switch light/dark theme with keybindings

you could use my theme installer here http://crunchbanglinux.org/forums/topic … installer/ . I would just have to add some options so you could use it without the gui.

edit: the mouse theme doesn't update correctly until you log out and back in though, so that part probably won't work well.

I say never be complete, I say stop being perfect, I say lets evolve, let the chips fall where they may.

Re: Switch light/dark theme with keybindings

@morgenstern Nice and simple! Thank you!

But I made some little adjustments in your script. I changed the names of the rc-files to keep syntax highlighting and added wallpaper settings.

#!/bin/bash
# switch-theme.sh
#

cd /home/$USER/.config/openbox

if [ -e rc.light.xml ]; then 
    mv rc.xml rc.dark.xml
    mv rc.light.xml rc.xml
    nitrogen --set-centered ~/images/backgrounds/LightWallpaper.jpg
    echo "switching from dark to light"
else
    mv rc.xml rc.light.xml
    mv rc.dark.xml rc.xml
    nitrogen --set-scaled ~/images/backgrounds/DarkWallpaper.png
    echo "switching from light to dark"
fi

openbox --restart

exit 0
Acer Extensa 5220 || Debian "Squeeze" & Fluxbox | #! 10 "Statler" Alpha & Openbox
identi.ca/herrgruen

Re: Switch light/dark theme with keybindings

nice improvements gruen smile

Re: Switch light/dark theme with keybindings

Like many people I like a dark GTK theme but the killer is things like word processing and some forms where the contrast is all wrong.

It would be very sweet to have a menu item and or key combo that switched into a light or clean theme and then back to dark when needed.

Iggy seems to have the solution in his installer but I have not found time to look yet.

If no one makes this then I will eventually but if you beat me to it, please share ; )

I blog too much....       geek stuff LinuxMintDebian | linux noob stuff LinuxMintNoob | spiritual stuff Daily Cup of Tao

Re: Switch light/dark theme with keybindings

well this is brilliant (as all is from ukrama)

in your openbox menu alter the call to an App to be this

    bash -c 'GTK2_RC_FILES=/path/to/your/gtkrc application-command'

It will launch that specific App in a unique skin. So for example I can have my whole system in a dark theme but launch Open Office in a Light theme e.g.

    bash -c 'GTK2_RC_FILES=/usr/share/themes/Clearlooks/gtk-2.0/gtkrc ooffice -writer'

See the post below for my hack fail on a GTK theme switcher...

I blog too much....       geek stuff LinuxMintDebian | linux noob stuff LinuxMintNoob | spiritual stuff Daily Cup of Tao

Re: Switch light/dark theme with keybindings

My hackathon at a GTK switcher (please laugh and point me at an obvious and elegant solution :D )

#!/bin/bash
# switches GTK theme from dark to light (or back) by copying a settings file over the .gtkrc-2.0 file in users home
function my-switch {
    if [ -e /home/$USER/mybin/theme-is-light ]; then
            cp /home/$USER/mybin/theme-dark-user /home/$USER/.gtkrc-2.0
            sudo cp /home/$USER/mybin/theme-dark-root /root/.gtkrc-2.0
            mv /home/$USER/mybin/theme-is-light /home/$USER/mybin/theme-is-dark
            sudo killall Xorg
            exit
    else
            cp /home/$USER/mybin/theme-light-user /home/$USER/.gtkrc-2.0
            sudo cp /home/$USER/mybin/theme-light-root /root/.gtkrc-2.0
            mv /home/$USER/mybin/theme-is-dark /home/$USER/mybin/theme-is-light
            sudo killall Xorg
            exit
    fi
}
# give options to user
# warns how switching theme will restart X (until I fix this)
zenity --question --title="Switch theme" --text="This will restart X (programs will close)"
case $? in
    0)
    my-switch;
    ;;
    1)
        exit
    ;;
    -1)
        exit
    ;;
esac

Last edited by tawan (2009-10-21 09:06:23)

I blog too much....       geek stuff LinuxMintDebian | linux noob stuff LinuxMintNoob | spiritual stuff Daily Cup of Tao

Re: Switch light/dark theme with keybindings

tawan wrote:

in your openbox menu alter the call to an App to be this

    bash -c 'GTK2_RC_FILES=/path/to/your/gtkrc application-command'

It will launch that specific App in a unique skin. So for example I can have my whole system in a dark theme but launch Open Office in a Light theme e.g.

    bash -c 'GTK2_RC_FILES=/usr/share/themes/Clearlooks/gtk-2.0/gtkrc ooffice -writer'

That's an excellent tip, thank you! big_smile

Nothing right in the left brain. Nothing left in the right brain.

Re: Switch light/dark theme with keybindings

Switch from Light to Dark GTK theme from Openbox Menu (or shortcut or other script)

My above script has a horrible restart X in it - I realized that "thunar -q" was what I needed to do, so this is much cleaner...

Make an empty text file in home called .theme-is-light

Alter to a dark theme and copy the line from .gtkrc-2.0 that looks like this
gtk-theme-name="plasticarbon"
into an empty file - now save that as .theme-dark in home

Alter to a light theme and redo the above but save as .theme-light

Then make this script and save it somewhere.

#!/bin/bash
# switches GTK theme from dark to light (or back) by copying a settings file over the .gtkrc-2.0.mine file in users home
function my-switch {
    if [ -e /home/$USER/.theme-is-light ]; then
            cp /home/$USER/.theme-dark /home/$USER/.gtkrc-2.0.mine
            sudo cp /home/$USER/.theme-dark /root/.gtkrc-2.0.mine
            mv /home/$USER/.theme-is-light /home/$USER/.theme-is-dark
            thunar -q
            gksu thunar -q
            thunar
            exit
    else
            cp /home/$USER/.theme-light /home/$USER/.gtkrc-2.0.mine
            sudo cp /home/$USER/.theme-light /root/.gtkrc-2.0.mine
            mv /home/$USER/.theme-is-dark /home/$USER/.theme-is-light
            thunar -q
            gksu thunar -q
            thunar
            exit
    fi
}
# give options to user
# warns how switching theme will close thunar (has to on my set up or the thunar deamon makes the theme not change)
zenity --question --title="Switch theme" --text="Sorry this will restart any Thunar windows"
case $? in
    0)
    my-switch;
    ;;
    1)
        exit
    ;;
    -1)
        exit
    ;;
esac

You may want to call this from the menu or a keyboard shortcut - or even from a script such as conky that would change the theme based on the time of day.

PS of course you can blend that with the opening post that changes openbox theme and then you have a full script that can alter the full theme.
_____________
EDITED an oops, EDIT2 improved

Last edited by tawan (2010-02-09 22:40:11)

I blog too much....       geek stuff LinuxMintDebian | linux noob stuff LinuxMintNoob | spiritual stuff Daily Cup of Tao