bashScripts

Some random scripts for specific daily tasks.
git clone git://git.thepablogq.xyz/bashScripts
Log | Files | Refs

commit a1fc6bdbe7a5cd66ad169986890425c1e368ab06
Author: Pablo Garro <pablopaulgq@gmail.com>
Date:   Thu, 14 Jan 2021 22:11:52 -0600

First Commit

Diffstat:
Akeylay | 13+++++++++++++
Amusic-split | 43+++++++++++++++++++++++++++++++++++++++++++
Anxt_mon | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atag_mp3 | 21+++++++++++++++++++++
Aupvol | 15+++++++++++++++
Awinfuncs.sh | 438+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 605 insertions(+), 0 deletions(-)

diff --git a/keylay b/keylay @@ -0,0 +1,13 @@ +#!/bin/sh + +var="$(setxkbmap -query | grep layout)" +var2="layout: us" + +xdotool key Caps_Lock + +if [ "$var" = "$var2" ] +then + setxkbmap -layout es +else + setxkbmap -layout us -variant altgr-intl -option nodeadkeys +fi diff --git a/music-split b/music-split @@ -0,0 +1,43 @@ +#!/bin/sh + +# Requires ffmpeg (audio splitting) and my `tag` wrapper script. + +[ ! -f "$2" ] && printf "The first file should be the audio, the second should be the timecodes.\\n" && exit + +echo "Enter the album/book title:"; read -r booktitle + +echo "Enter the artist/author:"; read -r author + +echo "Enter the publication year:"; read -r year + +inputaudio="$1" + +# Get a safe file name from the book. +escbook="$(echo "$booktitle" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g")" + +! mkdir -p "$escbook" && echo "Do you have write access in this directory?" && exit 1 + +# As long as the extension is in the tag script, it'll work. +ext="mp3" +#ext="${1#*.}" + +# Get the total number of tracks from the number of lines. +total="$(wc -l < "$2")" + +while read -r x; +do + end="$(echo "$x" | cut -d' ' -f1)" + [ -n "$start" ] && + echo "From $start to $end; $track $title" + file="$escbook/$(printf "%.2d" "$track")-$esctitle.$ext" + [ -n "$start" ] && echo "Splitting \"$title\"..." && ffmpeg -nostdin -y -loglevel -8 -i "$inputaudio" -ss "$start" -to "$end" -vn "$file" && + echo "Tagging \"$title\"..." && tag -a "$author" -A "$booktitle" -t "$title" -n "$track" -N "$total" -d "$year" "$file" + title="$(echo "$x" | cut -d' ' -f 2-)" + esctitle="$(echo "$title" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g")" + track="$((track+1))" + start="$end" +done < "$2" +# The last track must be done outside the loop. +echo "From $start to the end: $title" +file="$escbook/$(printf "%.2d" "$track")-$esctitle.$ext" +echo "Splitting \"$title\"..." && ffmpeg -nostdin -y -loglevel -8 -i "$inputaudio" -ss "$start" -vn "$file" diff --git a/nxt_mon b/nxt_mon @@ -0,0 +1,75 @@ +#!/bin/sh +# +# Move the current window to the next monitor. +# +# Also works only on one X screen (which is the most common case). +# +# Props to +# http://icyrock.com/blog/2012/05/xubuntu-moving-windows-between-monitors/ +# +# Unfortunately, both "xdotool getwindowgeometry --shell $window_id" and +# checking "-geometry" of "xwininfo -id $window_id" are not sufficient, as +# the first command does not respect panel/decoration offsets and the second +# will sometimes give a "-0-0" geometry. This is why we resort to "xwininfo". + +screen_width=$(xdpyinfo | awk '/dimensions:/ { print $2; exit }' | cut -d"x" -f1) +screen_height=$(xdpyinfo | awk '/dimensions:/ { print $2; exit }' | cut -d"x" -f2) +display_width=$(xdotool getdisplaygeometry | cut -d" " -f1) +display_height=$(xdotool getdisplaygeometry | cut -d" " -f2) +window_id=$(xdotool getactivewindow) + +# Remember if it was maximized. +window_horz_maxed=$(xprop -id "$window_id" _NET_WM_STATE | grep '_NET_WM_STATE_MAXIMIZED_HORZ') +window_vert_maxed=$(xprop -id "$window_id" _NET_WM_STATE | grep '_NET_WM_STATE_MAXIMIZED_VERT') + +# Un-maximize current window so that we can move it +wmctrl -ir "$window_id" -b remove,maximized_vert,maximized_horz + +# Read window position +x=$(xwininfo -id "$window_id" | awk '/Absolute upper-left X:/ { print $4 }') +y=$(xwininfo -id "$window_id" | awk '/Absolute upper-left Y:/ { print $4 }') + +# Subtract any offsets caused by panels or window decorations +x_offset=$(xwininfo -id "$window_id" | awk '/Relative upper-left X:/ { print $4 }') +y_offset=$(xwininfo -id "$window_id" | awk '/Relative upper-left Y:/ { print $4 }') +x=$(( x - x_offset)) +y=$(( y - y_offset)) + +# Compute new X position +new_x=$((x + display_width)) +# Compute new Y position +new_y=$((y + display_height)) + +# If we would move off the right-most monitor, we set it to the left one. +# We also respect the window's width here: moving a window off more than half its width won't happen. +width=$(xdotool getwindowgeometry "$window_id" | awk '/Geometry:/ { print $2 }'|cut -d"x" -f1) +if [ "$(( new_x + width / 2))" -gt "$screen_width" ]; then + new_x=$((new_x - screen_width)) +fi + +height=$(xdotool getwindowgeometry "$window_id" | awk '/Geometry:/ { print $2 }'|cut -d"x" -f2) +if [ "$((new_y + height / 2))" -gt "$screen_height" ]; then + new_y=$((new_y - screen_height)) +fi + +# Don't move off the left side. +if [ "$new_x" -lt 0 ]; then + new_x=0 +fi + +# Don't move off the bottom +if [ "$new_y" -lt 0 ]; then + new_y=0 +fi + +# Move the window +xdotool windowmove "$window_id" "$new_x" "$new_y" + +# Maximize window again, if it was before +if [ -n "${window_horz_maxed}" ] && [ -n "${window_vert_maxed}" ]; then + wmctrl -ir "$window_id" -b add,maximized_vert,maximized_horz +elif [ -n "${window_horz_maxed}" ]; then + wmctrl -ir "$window_id" -b add,maximized_horz +elif [ -n "${window_vert_maxed}" ]; then + wmctrl -ir "$window_id" -b add,maximized_vert +fi diff --git a/tag_mp3 b/tag_mp3 @@ -0,0 +1,21 @@ +#!/bin/bash +#mp3_names_script + +var=1 + +ls -v | grep .mp3 | cat -n | while read n f; do mv -n "$f" "$n.mp3"; done + +[ -z "$album" ] && echo "Nombre del Album?" && read -r album +[ -z "$year" ] && echo "Año del Album?" && read -r year +[ -z "$artist" ] && echo "Artista del Album?" && read -r artist + +while read p; do + #eyeD3 -t "$p" -n $var $var.mp3 + track=$(cut -d'|' -f1 <<<"$p" ) + composser=$(cut -d'|' -f2 <<<"$p" ) + echo "$track" + echo "$composser" + eyeD3 -t "$p" -n $var -b "$artist" -A "$album" -a "$composser" -Y "$year" -t "$track" $var.mp3 + ((var++)) + echo $var +done <$1 diff --git a/upvol b/upvol @@ -0,0 +1,15 @@ +#!/bin/sh + +if [ "$1" = "u" ] +then + pactl set-sink-volume alsa_output.pci-0000_00_1b.0.analog-stereo +5% | pkill -RTMIN+10 dwmblocks +elif [ "$1" = "d" ] +then + pactl set-sink-volume alsa_output.pci-0000_00_1b.0.analog-stereo -5% | pkill -RTMIN+10 dwmblocks +elif [ "$1" = "m" ] +then + pactl set-sink-mute alsa_output.pci-0000_00_1b.0.analog-stereo toggle | pkill -RTMIN+10 dwmblocks +else + echo heck +fi + diff --git a/winfuncs.sh b/winfuncs.sh @@ -0,0 +1,438 @@ +#!/bin/bash + +#todo: +# cancel for tile function +# determine what windows are maximized and re-max after the "window select" function +# determine what windows are non-resizable by the user so that the script doesn't resize them +# cascade also shaded windows + +# winfuncs.sh select +# winfuncs.sh tile +# winfuncs.sh tiletwo +# winfuncs.sh tiletwol +# winfuncs.sh tiletwor +# winfuncs.sh tilethree +# winfuncs.sh tilethreev +# winfuncs.sh stacktwo +# winfuncs.sh cascade +# winfuncs.sh showdesktop + +# set gaps (0 removes gaps) +outer_gaps=5 +inner_gaps=5 + +# set gaps for 'select' mode +expose_gaps=20 + +# set desktop dimensions +display_width=$(xdotool getdisplaygeometry | cut -d" " -f1) +display_height=$(xdotool getdisplaygeometry | cut -d" " -f2) + +# desktop height without panel(s) +desktop_height=$(xprop -root _NET_WORKAREA | awk '{ print $6 }' | cut -d"," -f1) + +# window decorations +window_id=$(xdotool getactivewindow) +titlebar_offset=$(xwininfo -id "$window_id" | awk '/Relative upper-left Y:/ { print $4 }') + +# top panel +top_bar=$(xprop -root _NET_WORKAREA | awk '{ print $4 }' | cut -d"," -f1) + +# bottom panel (not needed) +bottom_bar=`expr $display_height - $desktop_height - $top_bar` + +function get_desktop_dim { + if (( ${#DIM[@]} == 0 )) ; then + DIM=(`expr $display_width - $outer_gaps \* 2` `expr $desktop_height - $outer_gaps \* 2`) + fi +} + +# which workspace we're on +function get_workspace { + if [[ "$DTOP" == "" ]] ; then + DTOP=`xdotool get_desktop` + fi +} + +function is_desktop { + xwininfo -id "$*" | grep '"Desktop"' + return "$?" +} + +function get_visible_window_ids { + if (( ${#WDOWS[@]} == 0 )) ; then + WDOWS=(`xdotool search --desktop $DTOP --onlyvisible "" 2>/dev/null`) + fi +} + +function win_showdesktop { + get_workspace + get_visible_window_ids + + command="search --desktop $DTOP \"\"" + + if (( ${#WDOWS[@]} > 0 )) ; then + command="$command windowminimize %@" + else + command="$command windowraise %@" + fi + + echo "$command" | xdotool - +} + +function win_tile_two { + get_desktop_dim + + wid1=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid1" && return + + wid2=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid2" && return + + half_w=`expr ${DIM[0]} / 2` + win_h=${DIM[1]} + + commands="windowsize $wid1 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`" + commands="$commands windowsize $wid2 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`" + commands="$commands windowmove $wid1 $outer_gaps `expr $top_bar + $outer_gaps`" + commands="$commands windowmove $wid2 `expr $half_w + $outer_gaps + $inner_gaps` `expr $top_bar + $outer_gaps`" + commands="$commands windowraise $wid1" + commands="$commands windowraise $wid2" + + wmctrl -i -r $wid1 -b remove,maximized_vert,maximized_horz + wmctrl -i -r $wid2 -b remove,maximized_vert,maximized_horz + + echo "$commands" | xdotool - +} + +function win_tile_two_left { + get_desktop_dim + + wid1=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid1" && return + + wid2=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid2" && return + + half_w=`expr ${DIM[0]} / 3` + win_h=${DIM[1]} + + commands="windowsize $wid1 `expr $half_w \* 2 - $inner_gaps` `expr $win_h - $titlebar_offset`" + commands="$commands windowsize $wid2 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`" + commands="$commands windowmove $wid1 $outer_gaps `expr $top_bar + $outer_gaps`" + commands="$commands windowmove $wid2 `expr $half_w \* 2 + $outer_gaps + $inner_gaps` `expr $top_bar + $outer_gaps`" + commands="$commands windowraise $wid1" + commands="$commands windowraise $wid2" + + wmctrl -i -r $wid1 -b remove,maximized_vert,maximized_horz + wmctrl -i -r $wid2 -b remove,maximized_vert,maximized_horz + + echo "$commands" | xdotool - +} + +function win_tile_two_right { + get_desktop_dim + + wid1=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid1" && return + + wid2=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid2" && return + + half_w=`expr ${DIM[0]} / 3` + win_h=${DIM[1]} + + commands="windowsize $wid1 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`" + commands="$commands windowsize $wid2 `expr $half_w \* 2 - $inner_gaps` `expr $win_h - $titlebar_offset`" + commands="$commands windowmove $wid1 $outer_gaps `expr $top_bar + $outer_gaps`" + commands="$commands windowmove $wid2 `expr $half_w + $outer_gaps + $inner_gaps` `expr $top_bar + $outer_gaps`" + commands="$commands windowraise $wid1" + commands="$commands windowraise $wid2" + + wmctrl -i -r $wid1 -b remove,maximized_vert,maximized_horz + wmctrl -i -r $wid2 -b remove,maximized_vert,maximized_horz + + echo "$commands" | xdotool - +} + +function win_stack_two { + get_desktop_dim + + wid1=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid1" && return + + wid2=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid2" && return + + win_w=${DIM[0]} + half_h=`expr ${DIM[1]} / 2` + + commands="windowsize $wid1 `expr $win_w` `expr $half_h - $titlebar_offset - $inner_gaps`" + commands="$commands windowsize $wid2 `expr $win_w` `expr $half_h - $titlebar_offset - $inner_gaps`" + commands="$commands windowmove $wid1 $outer_gaps `expr $top_bar + $outer_gaps`" + commands="$commands windowmove $wid2 $outer_gaps `expr $half_h + $top_bar + $outer_gaps + $inner_gaps`" + commands="$commands windowraise $wid1" + commands="$commands windowraise $wid2" + + wmctrl -i -r $wid1 -b remove,maximized_vert,maximized_horz + wmctrl -i -r $wid2 -b remove,maximized_vert,maximized_horz + + echo "$commands" | xdotool - +} + + +function win_tile_three { + get_desktop_dim + + wid1=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid1" && return + + wid2=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid2" && return + + wid3=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid3" && return + + win_h=${DIM[1]} + half_w=`expr ${DIM[0]} / 2` + half_h=`expr ${win_h} / 2` + + commands="windowsize $wid1 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`" + commands="$commands windowsize $wid2 `expr $half_w - $inner_gaps` `expr $half_h - $titlebar_offset - $inner_gaps`" + commands="$commands windowsize $wid3 `expr $half_w - $inner_gaps` `expr $half_h - $titlebar_offset - $inner_gaps`" + commands="$commands windowmove $wid1 $outer_gaps `expr $top_bar + $outer_gaps`" + commands="$commands windowmove $wid2 `expr $half_w + $outer_gaps + $inner_gaps` `expr $top_bar + $outer_gaps`" + commands="$commands windowmove $wid3 `expr $half_w + $outer_gaps + $inner_gaps` `expr $half_h + $top_bar + $outer_gaps + $inner_gaps`" + commands="$commands windowraise $wid1" + commands="$commands windowraise $wid2" + commands="$commands windowraise $wid3" + + wmctrl -i -r $wid1 -b remove,maximized_vert,maximized_horz + wmctrl -i -r $wid2 -b remove,maximized_vert,maximized_horz + wmctrl -i -r $wid3 -b remove,maximized_vert,maximized_horz + + echo "$commands" | xdotool - +} + +function win_tile_three_v { + get_desktop_dim + + wid1=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid1" && return + + wid2=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid2" && return + + wid3=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid3" && return + + win_h=${DIM[1]} + half_w=`expr ${DIM[0]} / 3` + + commands="windowsize $wid1 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`" + commands="$commands windowsize $wid2 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`" + commands="$commands windowsize $wid3 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`" + commands="$commands windowmove $wid1 $outer_gaps `expr $top_bar + $outer_gaps`" + commands="$commands windowmove $wid2 `expr $half_w + $outer_gaps + $inner_gaps / 2` `expr $top_bar + $outer_gaps`" + commands="$commands windowmove $wid3 `expr $half_w \* 2 + $outer_gaps + $inner_gaps` `expr $top_bar + $outer_gaps`" + commands="$commands windowraise $wid1" + commands="$commands windowraise $wid2" + commands="$commands windowraise $wid3" + + wmctrl -i -r $wid1 -b remove,maximized_vert,maximized_horz + wmctrl -i -r $wid2 -b remove,maximized_vert,maximized_horz + wmctrl -i -r $wid3 -b remove,maximized_vert,maximized_horz + + echo "$commands" | xdotool - +} + +function win_tile { + get_workspace + get_visible_window_ids + + (( ${#WDOWS[@]} < 1 )) && return; + + get_desktop_dim + + # determine how many rows and columns we need + cols=`echo "sqrt(${#WDOWS[@]})" | bc` + rows=$cols + wins=`expr $rows \* $cols` + + if (( "$wins" < "${#WDOWS[@]}" )) ; then + cols=`expr $cols + 1` + wins=`expr $rows \* $cols` + if (( "$wins" < "${#WDOWS[@]}" )) ; then + rows=`expr $rows + 1` + wins=`expr $rows \* $cols` + fi + fi + + (( $cols < 1 )) && cols=1; + (( $rows < 1 )) && rows=1; + + win_w=`expr ${DIM[0]} / $cols` + win_h=`expr ${DIM[1]} / $rows` + + # do tiling + x=0; y=0; commands="" + for window in ${WDOWS[@]} ; do + wmctrl -i -r $window -b remove,maximized_vert,maximized_horz + + commands="$commands windowsize $window `expr $win_w - $inner_gaps \* 2` `expr $win_h - $titlebar_offset - $inner_gaps \* 2`" + commands="$commands windowmove $window `expr $x \* $win_w + $outer_gaps` `expr $y \* $win_h + $top_bar + $outer_gaps`" + + x=`expr $x + 1` + if (( $x > `expr $cols - 1` )) ; then + x=0 + y=`expr $y + 1` + fi + done + + echo "$commands" | xdotool - +} + +function expose { + get_workspace + get_visible_window_ids + + (( ${#WDOWS[@]} < 1 )) && return; + + get_desktop_dim + + # determine how many rows and columns we need + cols=`echo "sqrt(${#WDOWS[@]})" | bc` + rows=$cols + wins=`expr $rows \* $cols` + + if (( "$wins" < "${#WDOWS[@]}" )) ; then + cols=`expr $cols + 1` + wins=`expr $rows \* $cols` + if (( "$wins" < "${#WDOWS[@]}" )) ; then + rows=`expr $rows + 1` + wins=`expr $rows \* $cols` + fi + fi + + (( $cols < 1 )) && cols=1; + (( $rows < 1 )) && rows=1; + + win_w=`expr ${DIM[0]} / $cols` + win_h=`expr ${DIM[1]} / $rows` + + # do tiling + x=0; y=0; commands="" + for window in ${WDOWS[@]} ; do + wmctrl -i -r $window -b remove,maximized_vert,maximized_horz + + commands="$commands windowsize $window `expr $win_w - $expose_gaps \* 2` `expr $win_h - $titlebar_offset - $expose_gaps \* 2`" + commands="$commands windowmove $window `expr $x \* $win_w + $expose_gaps` `expr $y \* $win_h + $top_bar + $expose_gaps`" + + x=`expr $x + 1` + if (( $x > `expr $cols - 1` )) ; then + x=0 + y=`expr $y + 1` + fi + done + + echo "$commands" | xdotool - +} + +function win_cascade { + get_workspace + get_visible_window_ids + + (( ${#WDOWS[@]} < 1 )) && return; + + x=0; y=0; commands="" + for window in ${WDOWS[@]} ; do + wmctrl -i -r $window -b remove,maximized_vert,maximized_horz + + commands="$commands windowsize $window 1024 640" + commands="$commands windowmove $window `expr $x + $outer_gaps` `expr $y + $top_bar + $outer_gaps`" + + x=`expr $x + 100` + y=`expr $y + 80` + done + + echo "$commands" | xdotool - +} + +function win_select { + get_workspace + get_visible_window_ids + + (( ${#WDOWS[@]} < 1 )) && return; + + # store window positions and widths + i=0 + for window in ${WDOWS[@]} ; do + GEO=`xdotool getwindowgeometry $window | grep Geometry | sed 's/.* \([0-9].*\)/\1/g'`; + height[$i]=`echo $GEO | sed 's/\(.*\)x.*/\1/g'` + width[$i]=`echo $GEO | sed 's/.*x\(.*\)/\1/g'` + + # ( xwininfo gives position not ignoring titlebars and borders, unlike xdotool ) + POS=(`xwininfo -stats -id $window | grep 'geometry ' | sed 's/.*[-+]\([0-9]*[-+][0-9*]\)/\1/g' | sed 's/[+-]/ /g'`) + posx[$i]=${POS[0]} + posy[$i]=${POS[1]} + + i=`expr $i + 1` + done + + # tile windows + expose + + # select a window + wid=`xdotool selectwindow 2>/dev/null` + + is_desktop "$wid" && return + + # restore window positions and widths + i=0; commands="" + for (( i=0; $i<${#WDOWS[@]}; i++ )) ; do + commands="$commands windowsize ${WDOWS[i]} ${height[$i]} ${width[$i]}" + commands="$commands windowmove ${WDOWS[i]} ${posx[$i]} ${posy[$i]}" + done + + commands="$commands windowraise $wid" + + echo "$commands" | xdotool - +} + +for command in ${@} ; do + if [[ "$command" == "tile" ]] ; then + win_tile + elif [[ "$command" == "select" ]] ; then + win_select + elif [[ "$command" == "tiletwo" ]] ; then + win_tile_two + elif [[ "$command" == "tiletwol" ]] ; then + win_tile_two_left + elif [[ "$command" == "tiletwor" ]] ; then + win_tile_two_right + elif [[ "$command" == "stacktwo" ]] ; then + win_stack_two + elif [[ "$command" == "tilethree" ]] ; then + win_tile_three + elif [[ "$command" == "tilethreev" ]] ; then + win_tile_three_v + elif [[ "$command" == "cascade" ]] ; then + win_cascade + elif [[ "$command" == "showdesktop" ]] ; then + win_showdesktop + fi +done