Sunday, April 28, 2013

Thunar Custom Actions: Pictures Batch Resize with Zenity Progress Bar and Convert

This is e better version of my old picture resize script. It uses Zenity to ask for the resizing percentage and to show a progress bar while the script is working.

Screenshots

Resize Script Enter Percentage

Resize Script Progress Bar
Resize Script Work Completed

Bash Script


picresize.sh
 1 #!/bin/bash
 2 
 3 
 4 # paths, constants, etc.
 5 PICPATH="$1"
 6 TOTALPICS="$((($# - 1)))"
 7 STEP="$(((100 / $TOTALPICS)))"
 8 PROGR=0
 9 MINSIZE=1
10 MAXSIZE=500
11 
12 # get the new size percentage, check it and set output directory path
13 SIZE=""
14 while [ -z "$SIZE" ] || [[ $SIZE<$MINSIZE ]] || [[ $SIZE>$MAXSIZE ]]; do
15     SIZE="$(zenity --entry --title="Picture Resize" --text="Enter percentage...")"
16 
17     # exit if cancel pressed
18     [[ $? != 0 ]] && exit 0
19 
20     # check range
21     if [[ $SIZE<$MINSIZE ]] || [[ $SIZE>$MAXSIZE ]]; then
22         zenity --error --text="$SIZE% is outside the accepted range: 1% - 500%!"
23     fi
24 done
25 
26 OUTPATH="resize_$SIZE%"
27 
28 # check if output directory exists
29 [ -d "$PICPATH/$OUTPATH" ] || mkdir "$PICPATH/$OUTPATH"
30 shift
31 
32 # convert pics, show progress bar
33 while (( $# )); do
34     convert "$PICPATH/$1" -resize "$SIZE%" "$PICPATH/$OUTPATH/resized_$SIZE%-$1"
35     ((PROGR+=$STEP))
36     echo $PROGR
37     shift
38 done | zenity --progress --title="Picture Resize" --text="working..." --percentage=0 -
39                                  -auto-close
40 
41 # check return value and print message depending on it
42 [[ $? == 0 ]] && zenity --info --title="Picture Resize" --text="All done!" || zenity -
43        -error --text="Canceled!"

Add Thunar Custom Action

Please refer to this article for adding a custom action to Thunar.
Here is a screenshot of my custom action for this resizing script.

Note that the bash script needs two "thunar arguments" :
  1. Path to the pictures to be resized -> %d
  2. Pictures list -> %N


Saturday, April 27, 2013

Volume OSD with Openbox and Riitek Micro Keyboard

With this small bash script you can show a nice progress bar on the screen when changing the volume in a TV-like fashion.
The script uses osd_cat provided by the xosd package under Arch Linux (Please refer to your system specific package manager for install this package under other distributions).

In order to make this script work with Openbox you will have to edit the Openbox configuration file rc.xml file normally placed under /home/<user>/.config/openbox/rc.xml. I will show how I did it later on this post.

OSD Bash Script

A few function definitions make the main part simpler. They are mainly responsible of increasing/decreasing the volume and showing the on-screen message.
There are also some constants responsible for placing the message on a certain screen position, setting the font, etc.
test.sh
 1 #!/bin/bash
 2 
 3 
 4 
 5 # constants
 6 FONT='-*-fixed-*-*-*-*-100-*-*-*-*-*-*-*'
 7 COLOR="green"
 8 DELAY=4
 9 POS="bottom"
10 ALIGN="center"
11 BARMOD="percentage"
12 VOLTXT="Volume"
13 VOLMUTEDTXT="Muted"
14 VOLSTEP=2
15 
16 
17 
18 # kills an existing osd_cat process
19 # needed when holding down a key to force repaint of the onscreen message
20 preKill() {
21     killall osd_cat
22 }
23 
24 # gets the actual volume value
25 getVol() {
26     VOL="$(amixer sget Master | grep "Mono:" | awk '{ print $4 }' | sed -e 's/\[//' -
           e 's/\]//' -e 's/\%//')"
27 }
28 
29 # gets the actual volume value and prints is on the screen
30 # with a percent bar + a percent number
31 showVol() {
32     getVol
33     if [[ $VOL == 0 ]]; then
34         osd_cat --pos="$POS" --align="$ALIGN" --delay="$DELAY" --colour="$COLOR" --
                         font="$FONT" --barmode="$BARMOD" --text="$VOLMUTEDTXT $VOL%" --
                         percentage="$VOL"
35     else
36         osd_cat --pos="$POS" --align="$ALIGN" --delay="$DELAY" --colour="$COLOR" --
                         font="$FONT" --barmode="$BARMOD" --text="$VOLTXT $VOL%" --
                         percentage="$VOL"
37     fi
38 }
39 
40 # reises the master channel by "VOLSTEP"
41 volUp() {
42     amixer sset Master "$VOLSTEP+"
43 }
44 
45 # decreases the master channel by "VOLSTEP"
46 volDown() {
47     amixer sset Master "$VOLSTEP-"
48 }
49 
50 # mutes the master channel
51 volMute() {
52     amixer sset Master 0
53 }
54 
55 
56 # main part
57 preKill
58 
59 case "$1" in
60     "volup")
61             volUp
62             showVol
63             ;;
64     "voldown")
65             volDown
66             showVol
67             ;;
68     "mute")
69             volMute
70             showVol
71             ;;
72     *)
73             ;;
74 esac

Openbox rc.xml File

The rc.xml file has a keyboard section where custom keyboard shortcuts can be defined.

x.xml
 1     <keybind key="W-KP_Add">
 2       <action name="execute">
 3         <execute>/home/adrian/scripts/osd.sh volup</execute>
 4       </action>
 5     </keybind>
 6     <keybind key="W-KP_Subtract">
 7       <action name="execute">
 8         <execute>/home/adrian/scripts/osd.sh voldown</execute>
 9       </action>
10     </keybind>
11     <keybind key="W-m">
12       <action name="execute">
13         <execute>/home/adrian/scripts/osd.sh mute</execute>
14       </action>
15     </keybind>
Riitek Micro Keyboard

Besides the keyboard shortcuts, I wanted to use my Riitek Micro Keyboard RT-MWK03. When pressing the Vulume Up/Down/Mute key combination on the keyboard an X event is generated on the X server.
These X events can be used in the rc.xml file just the same way as any other keyboard shortcut. I used Xev to find out what events are generated when pressing the volume key combinations.
y.xml
 1     <keybind key="XF86AudioRaiseVolume">
 2       <action name="execute">
 3         <execute>/home/adrian/scripts/osd.sh volup</execute>
 4       </action>
 5     </keybind>
 6     <keybind key="XF86AudioLowerVolume">
 7       <action name="execute">
 8         <execute>/home/adrian/scripts/osd.sh voldown</execute>
 9       </action>
10     </keybind>
11     <keybind key="XF86AudioMute">
12       <action name="execute">
13         <execute>/home/adrian/scripts/osd.sh mute</execute>
14       </action>
15     </keybind>


Screenshots


Openbox OSD Script Volume 59%

Openbox OSD Script Muted Volume