literate-dotfiles/dotfiles/scripts.org

520 lines
13 KiB
Org Mode
Raw Normal View History

2019-05-16 21:13:08 +02:00
#+TITLE: Scripts
* Wallpapers
:PROPERTIES:
:header-args: :tangle ~/scripts/wallpapers.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
#+begin_src bash
while true; do
nitrogen --set-zoom-fill --random ".wallpapers"
sleep 10m
done
#+end_src
* TODO [#A] LockScreen
2019-12-16 08:53:36 +01:00
:PROPERTIES:
:header-args: :tangle ~/scripts/lockscreen.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
2019-05-16 21:13:08 +02:00
- [ ] Does not work well with multiple screen
First, turn off dunst
#+begin_src bash
killall -SIGUSR1 dunst && echo "off" > /tmp/dunststatus;
#+end_src
Turn off the music if it is playing.
#+begin_src bash
MPC_STATE=$(mpc | sed -n '2p' | cut -d "[" -f2 | cut -d "]" -f1)
if [[ $MPC_STATE == "playing" ]]; then
mpc pause
fi
#+end_src
Then take a screenshot and process it.
2019-05-16 21:13:08 +02:00
#+begin_src bash
2019-06-22 17:33:42 +02:00
temp_file="/tmp/screen.png"
2019-12-15 10:51:42 +01:00
rm -f $temp_file
2019-06-22 17:33:42 +02:00
scrot $temp_file
convert $temp_file -scale 10% -scale 1000% $temp_file
#+end_src
2019-06-22 17:33:42 +02:00
Finally, lock the screen using =i3lock=.
#+begin_src bash
i3lock -e -n -i $temp_file && killall -SIGUSR2 dunst && echo "on" > /tmp/dunststatus
2019-06-22 17:33:42 +02:00
#+end_src
#+begin_src bash :tangle no
2019-05-16 21:13:08 +02:00
revert() {
xset dpms 0 0 0
}
trap revert HUP INT TERM
# turn off screen after 5 seconds
xset +dpms dpms 5 5 5
# Parameters
temp_file="/tmp/screen.png"
icon="$HOME/Pictures/Evil_Rick_Sprite.png"
width=1920
height=1080
blur_factor=6
lock_blur_factor=0
# Take the screen shot, blur the image and add the icon
ffmpeg -f x11grab -video_size "${width}x${height}" -y -i $DISPLAY -i $icon -filter_complex "boxblur=$blur_factor:$blur_factor,overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2,boxblur=$lock_blur_factor:$lock_blur_factor" -vframes 1 $temp_file
# Alternative
# scrot -d 1 $temp_file
# convert -blur 0x8 $temp_file $temp_file
# convert -composite $temp_file $icon -gravity South -geometry -20x1200 $temp_file
# Lock the screen with the image
i3lock --no-unlock-indicator --ignore-empty-password --show-failed-attempts --nofork --image=$temp_file
# Remove the screenshot
rm $temp_file
# Don't turn off screen when back from lock
revert
#+end_src
* Toggle Automatic Lock Screen
:PROPERTIES:
:header-args: :tangle ~/scripts/lockscreen-toggle.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
#+begin_src bash
if pgrep -x "xautolock" >/dev/null
then
pkill xautolock;
notify-send "Lock Screen Desactivated"
else
xautolock -locker "~/scripts/lockscreen.sh" -detectsleep -time 30 -notify 60 -notifier "dunstify --replace=31846 -u critical -t 10000 -- 'Locking Screen' '60 seconds'" &
notify-send "Lock Screen Activated"
fi
#+end_src
2019-05-16 21:13:08 +02:00
* Delete first page of PDF
:PROPERTIES:
:header-args: :tangle ~/scripts/pdf-delete-first-page.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
The requirement is to have =pdftk= installed.
#+begin_src bash
if [[ -f $1 && $1 == *.pdf ]]; then
# Argument if a file
2019-12-15 10:51:42 +01:00
# pdftk "$1" cat 2-end output /tmp/pdftk_out.pdf && mv /tmp/pdftk_out.pdf "$1"
stapler del "$1" 1 /tmp/pdftk_out.pdf && mv /tmp/pdftk_out.pdf "$1"
2019-05-16 21:13:08 +02:00
fi
#+end_src
* Lock / Exit / Suspend / ...
:PROPERTIES:
:header-args: :tangle ~/scripts/quit.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
#+begin_src bash
option=$(echo -e "Lock\nExit\nLogout\nSuspend\nHibernate\nReboot\nShutdown" | rofi -i -dmenu)
case "$option" in
"Lock")
i3exit lock ;;
"Exit")
i3exit switch_user ;;
"Logout")
i3exit logout ;;
"Suspend")
i3exit suspend ;;
"Hibernate")
i3exit hibernate ;;
"Reboot")
i3exit reboot ;;
"Shutdown")
i3exit shutdown ;;
esac
#+end_src
#+RESULTS:
* TODO [#C] Copy Figures
:PROPERTIES:
:header-args: :tangle ~/scripts/cp-figs.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
Things to do:
- [ ] Display all the figures at once and ask for confirmation to copy them all
- [ ] Display the not found figures
#+begin_src bash :results output
2019-12-15 10:51:42 +01:00
latexpath="$HOME/Cloud/thesis/latex/ressources/Figures";
2019-05-16 21:13:08 +02:00
figures=`awk 'match($0, /(fig.*\.(png|svg|pdf))/, a) {print a[1];}' $1 \
| awk '{ print gensub(/\s*\]\]\s*\|\s*\[\[\s*\.?\/?/, "\n", "g") }'`;
for figure in $figures
do
figurename=`echo $figure | awk 'match($0, /(fig.*\/)(.*\.(png|svg|pdf))/, a) {print a[2];}'`;
if [ -f $latexpath/$figurename ]
then
figurepath=`echo $figure | awk 'match($0, /(fig.*)\/(.*\.(png|svg|pdf))/, a) {print a[1];}'`;
# echo $latexpath/$figurename
# echo $figurepath/$figurename
cp $latexpath/$figurename $figurepath/$figurename
# read -r -p "Are You Sure? [Y/n] " input
# case $input in
# [yY][eE][sS]|[yY])
# cp $latexpath/$figurename $figurepath/$figurename
# ;;
# [nN][oO]|[nN])
# exit 1
# ;;
# *)
# echo "Invalid input..."
# exit 1
# ;;
# esac
fi
done
#+end_src
#+RESULTS:
2019-12-15 10:51:42 +01:00
2019-05-16 21:13:08 +02:00
* NAS
** Mount
:PROPERTIES:
:header-args: :tangle ~/scripts/nas.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
#+begin_src bash
if [ $1 == "mount" ]; then
if sudo -A mount 192.168.1.2:/volume1/Downloads/ /mnt/NAS/; then
dunstify --replace=58249 'NAS' 'Successfully mounted'
else
dunstify --replace=58249 'NAS' 'Error while mounted'
fi
elif [ $1 == "umount" ]; then
if sudo -A umount /mnt/NAS/; then
dunstify --replace=58249 'NAS' 'Successfully unmounted'
else
dunstify --replace=58249 'NAS' 'Error while unmounted'
fi
fi
#+end_src
* Buku Git Push
:PROPERTIES:
:header-args: :tangle ~/scripts/buku_git_push.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
#+begin_src bash
cd ~/.local/share/buku/
if [[ ! -z $(git status -s bookmarks.db) ]]
then
git add bookmarks.db
git commit -m "Changed bookmarks - $(date +%F)"
git push
dunstify --replace=79248 "Buku Git" "Updated"
exit
fi
#+end_src
* Icons Unicode
:PROPERTIES:
:header-args: :tangle ~/scripts/icons.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
#+begin_src bash
command -v xclip >/dev/null 2>&1 || { echo >&2 "I require xclip but it's not installed. Aborting."; exit 1; }
chosen=$(grep -v "#" ~/.config/emoji | dmenu -i -l 20)
[ "$chosen" != "" ] || exit
c=$(echo "$chosen" | sed "s/ .*//")
echo "$c" | tr -d '\n' | xclip -selection clipboard
dunstify --replace=05896 "'$c' copied to clipboard." &
#+end_src
* Make GIF
:PROPERTIES:
:header-args: :tangle ~/scripts/gifenc.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
#+begin_src bash
palette="/tmp/palette.png"
filters="fps=15,scale=320:-1:flags=lanczos"
ffmpeg -v warning -i $1 -vf "$filters,palettegen" -y $palette
ffmpeg -v warning -i $1 -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y $2
#+end_src
2019-05-16 21:13:08 +02:00
* Org-Capture
:PROPERTIES:
:header-args: :tangle ~/scripts/org-capture-selection.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
#+begin_src bash
xclip -o -selection primary | xclip -o -selection clipboard -i
emacsclient -ne '(org-capture "" "Q")' && dunstify "Emacs" "Text Successfully Captured"
#+end_src
2019-05-30 16:22:37 +02:00
* Download-Audio
:PROPERTIES:
:header-args: :tangle ~/scripts/download-audio.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
#+begin_src bash
if [ $TMUX ]; then
tmux split -v -l 1 "cd ~/Downloads/ && youtube-dl --add-metadata -xic -f bestaudio/best $1" && tmux select-pane -U
else
cd ~/Downloads/;
setsid nohup youtube-dl --add-metadata -xic -f bestaudio/best $1 &> /dev/null &
fi
#+end_src
* Download-Video
:PROPERTIES:
:header-args: :tangle ~/scripts/download-video.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
#+begin_src bash
if [ $TMUX ]; then
tmux split -v -l 1 "cd ~/Downloads/ && youtube-dl --add-metadata -ic $1" && tmux select-pane -U
else
cd ~/Downloads/;
setsid nohup youtube-dl --add-metadata -ic $1 &> /dev/null &
fi
#+end_src
2019-12-15 10:51:42 +01:00
* Org-Protocol-Capture-HTML
:PROPERTIES:
:header-args: :tangle ~/scripts/org-protocol-capture-html.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
Defaults
#+begin_src bash
heading="link"
template="pu"
url="https://google.com/"
#+end_src
Functions
#+begin_src bash
function debug {
if [[ -n $debug ]]
then
function debug {
echo "DEBUG: $@" >&2
}
debug "$@"
else
function debug {
true
}
fi
}
function die {
echo "$@" >&2
exit 1
}
function urlencode {
python -c "
from __future__ import print_function
try:
from urllib import quote # Python 2
except ImportError:
from urllib.parse import quote # Python 3
import sys
print(quote(sys.stdin.read()[:-1], safe=''))"
}
#+end_src
Documentation
#+begin_src bash
function usage {
cat <<EOF
$0 [OPTIONS] [HTML]
html | $0 [OPTIONS]
Send HTML to Emacs through org-protocol, passing it through Pandoc to
convert HTML to Org-mode. HTML may be passed as an argument or
through STDIN. If only URL is given, it will be downloaded and its
contents used.
Options:
-h, --heading HEADING Heading
-t, --template TEMPLATE org-capture template key (default: pu)
-u, --url URL URL
--debug Print debug info
--help I need somebody!
EOF
}
#+end_src
Arguments
#+begin_src bash
args=$(getopt -n "$0" -o dh:rt:u: -l debug,help,heading:,template:,url: -- "$@") \
|| die "Unable to parse args. Is getopt installed?"
eval set -- "$args"
while true
do
case "$1" in
-d|--debug)
debug=true
debug "Debugging on"
;;
--help)
usage
exit
;;
-h|--heading)
shift
heading="$1"
;;
-t|--template)
shift
template="$1"
;;
-u|--url)
shift
url="$1"
;;
--)
# Remaining args
shift
rest=("$@")
break
;;
esac
shift
done
debug "ARGS: $args"
debug "Remaining args: ${rest[@]}"
#+end_src
Get HTML
#+begin_src bash
if [[ -n $@ ]]
then
debug "Text from args"
body="$@"
fi
#+end_src
URL-encode
#+begin_src bash
heading=$(urlencode <<<"$heading") || die "Unable to urlencode heading."
url=$(urlencode <<<"$url") || die "Unable to urlencode URL."
body=$(urlencode <<<"$body") || die "Unable to urlencode text."
#+end_src
Send to Emacs
#+begin_src bash
emacsclient "org-protocol://capture?template=$template&url=$url&title=$heading&body=$body"
#+end_src
* Pdf Shrink
:PROPERTIES:
:header-args: :tangle ~/scripts/pdf-shrink.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
#+begin_src bash
shrink ()
{
gs \
-q -dNOPAUSE -dBATCH -dSAFER \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.3 \
-dPDFSETTINGS=/screen \
-dEmbedAllFonts=true \
-dSubsetFonts=true \
-dAutoRotatePages=/None \
-dColorImageDownsampleType=/Bicubic \
-dColorImageResolution=$3 \
-dGrayImageDownsampleType=/Bicubic \
-dGrayImageResolution=$3 \
-dMonoImageDownsampleType=/Subsample \
-dMonoImageResolution=$3 \
-sOutputFile="$2" \
"$1"
}
check_smaller ()
{
# If $1 and $2 are regular files, we can compare file sizes to
# see if we succeeded in shrinking. If not, we copy $1 over $2:
if [ ! -f "$1" -o ! -f "$2" ]; then
return 0;
fi
ISIZE="$(echo $(wc -c "$1") | cut -f1 -d\ )"
OSIZE="$(echo $(wc -c "$2") | cut -f1 -d\ )"
if [ "$ISIZE" -lt "$OSIZE" ]; then
echo "Input smaller than output, doing straight copy" >&2
cp "$1" "$2"
fi
}
usage ()
{
echo "Reduces PDF filesize by lossy recompressing with Ghostscript."
echo "Not guaranteed to succeed, but usually works."
echo " Usage: $1 infile [outfile] [resolution_in_dpi]"
}
IFILE="$1"
# Need an input file:
if [ -z "$IFILE" ]; then
usage "$0"
exit 1
fi
# Output filename defaults to "-" (stdout) unless given:
if [ ! -z "$2" ]; then
OFILE="$2"
else
OFILE="-"
fi
# Output resolution defaults to 72 unless given:
if [ ! -z "$3" ]; then
res="$3"
else
res="90"
fi
shrink "$IFILE" "$OFILE" "$res" || exit $?
check_smaller "$IFILE" "$OFILE"
#+end_src