literate-dotfiles/binaries.org

1374 lines
35 KiB
Org Mode
Raw Permalink Normal View History

2019-05-16 21:13:08 +02:00
#+TITLE: Binaries
2021-01-01 20:12:34 +01:00
#+SETUPFILE: ./setup/org-setup-file.org
2019-05-16 21:13:08 +02:00
2020-04-05 15:39:08 +02:00
#+PROPERTY: header-args:bash :comments both :mkdirp yes
#+PROPERTY: header-args:bash+ :shebang "#!/usr/bin/env bash"
#+PROPERTY: header-args:bash+ :tangle-mode (identity #o555)
2019-05-16 21:13:08 +02:00
* =dmenumount= - Mount USB and Android
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: dmenumount
:END:
Script taken from Luke Smith.
#+begin_src bash :tangle ~/.local/bin/dmenumount
getmount() { \
[ -z "$chosen" ] && exit 1
# shellcheck disable=SC2086
mp="$(find $1 2>/dev/null | dmenu -i -p "Type in mount point.")" || exit 1
[ "$mp" = "" ] && exit 1
if [ ! -d "$mp" ]; then
mkdiryn=$(printf "No\\nYes" | dmenu -i -p "$mp does not exist. Create it?") || exit 1
[ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || sudo -A mkdir -p "$mp")
fi
}
mountusb() { \
chosen="$(echo "$usbdrives" | dmenu -i -p "Mount which drive?")" || exit 1
chosen="$(echo "$chosen" | awk '{print $1}')"
sudo -A mount "$chosen" 2>/dev/null && notify-send "💻 USB mounting" "$chosen mounted." && exit 0
alreadymounted=$(lsblk -nrpo "name,type,mountpoint" | awk '$3!~/\/boot|\/home$|SWAP/&&length($3)>1{printf "-not ( -path *%s -prune ) ",$3}')
getmount "/mnt /media /mount /home -maxdepth 5 -type d $alreadymounted"
partitiontype="$(lsblk -no "fstype" "$chosen")"
case "$partitiontype" in
"vfat") sudo -A mount -t vfat "$chosen" "$mp" -o rw,umask=0000;;
,*) sudo -A mount "$chosen" "$mp"; user="$(whoami)"; ug="$(groups | awk '{print $1}')"; sudo -A chown "$user":"$ug" "$mp";;
esac
notify-send "💻 USB mounting" "$chosen mounted to $mp."
}
mountandroid() { \
chosen="$(echo "$anddrives" | dmenu -i -p "Which Android device?")" || exit 1
chosen="$(echo "$chosen" | cut -d : -f 1)"
getmount "$HOME -maxdepth 3 -type d"
simple-mtpfs --device "$chosen" "$mp"
echo "OK" | dmenu -i -p "Tap Allow on your phone if it asks for permission and then press enter" || exit 1
simple-mtpfs --device "$chosen" "$mp"
notify-send "🤖 Android Mounting" "Android device mounted to $mp."
}
asktype() { \
choice="$(printf "USB\\nAndroid" | dmenu -i -p "Mount a USB drive or Android device?")" || exit 1
case $choice in
USB) mountusb ;;
Android) mountandroid ;;
esac
}
anddrives=$(simple-mtpfs -l 2>/dev/null)
usbdrives="$(lsblk -rpo "name,type,size,mountpoint" | awk '$4==""{printf "%s (%s)\n",$1,$3}')"
if [ -z "$usbdrives" ]; then
[ -z "$anddrives" ] && echo "No USB drive or Android device detected" && exit
echo "Android device(s) detected."
mountandroid
else
if [ -z "$anddrives" ]; then
echo "USB drive(s) detected."
mountusb
else
echo "Mountable USB drive(s) and Android device(s) detected."
asktype
fi
fi
#+end_src
* =dmenuumount= - Unmount USB and Android devices
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: dmenuumount
:END:
Script taken from Luke Smith.
#+begin_src bash :tangle ~/.local/bin/dmenuumount
unmountusb() {
[ -z "$drives" ] && exit
chosen="$(echo "$drives" | dmenu -i -p "Unmount which drive?")" || exit 1
chosen="$(echo "$chosen" | awk '{print $1}')"
[ -z "$chosen" ] && exit
sudo -A umount "$chosen" && notify-send "💻 USB unmounting" "$chosen unmounted."
}
unmountandroid() { \
chosen="$(awk '/simple-mtpfs/ {print $2}' /etc/mtab | dmenu -i -p "Unmount which device?")" || exit 1
[ -z "$chosen" ] && exit
sudo -A umount -l "$chosen" && notify-send "🤖 Android unmounting" "$chosen unmounted."
}
asktype() { \
choice="$(printf "USB\\nAndroid" | dmenu -i -p "Unmount a USB drive or Android device?")" || exit 1
case "$choice" in
USB) unmountusb ;;
Android) unmountandroid ;;
esac
}
drives=$(lsblk -nrpo "name,type,size,mountpoint" | awk '$4!~/\/boot|\/home$|SWAP/&&length($4)>1{printf "%s (%s)\n",$4,$3}')
if ! grep simple-mtpfs /etc/mtab; then
[ -z "$drives" ] && echo "No drives to unmount." && exit
echo "Unmountable USB drive detected."
unmountusb
else
if [ -z "$drives" ]
then
echo "Unmountable Android device detected."
unmountandroid
else
echo "Unmountable USB drive(s) and Android device(s) detected."
asktype
fi
fi
#+end_src
* =dmenukill= - Kill program using Dmenu
:PROPERTIES:
:CUSTOM_ID: dmenukill
:END:
#+begin_src bash :tangle ~/.local/bin/dmenukill
ps_line=$(ps -u $USER k -size -o pid=,%mem=,%cpu=,comm= | dmenu -i -l 15)
if [ ! -z "$ps_line" ]; then
pid=$(echo $ps_line | awk '{print $1}')
name=$(echo $ps_line | awk '{print $4}')
kill -15 $pid && \
2022-02-06 21:45:41 +01:00
notify-send "Kill" "$name (PID $pid)" &
fi
2019-05-16 21:13:08 +02:00
#+end_src
2021-01-01 20:12:34 +01:00
* =nordvpn-toggle= - Connect to VPN using NordVPN
:PROPERTIES:
:CUSTOM_ID: nordvpn-toggle
:END:
To use this this, =nordvpn= must be installed: =yay -S nordvpn-bin=.
#+begin_src bash :tangle ~/.local/bin/nordvpn-toggle
tmpfile="/tmp/vpnstatus";
2021-01-01 20:12:34 +01:00
if [[ $(nordvpn status) == *"Connected"* ]]; then
nordvpn disconnect && \
2022-02-06 21:45:41 +01:00
notify-send "VPN" "Disconnected" && \
echo "off" > $tmpfile;
else
# Select Country to connect to
country=`cat ~/.local/data/nordvpn_countries.txt | rofi -i -dmenu | sed 's/\s/_/g'`;
2022-02-06 21:45:41 +01:00
notify-send "VPN" "Connecting to $country...";
nordvpn connect $country && \
2022-02-06 21:45:41 +01:00
notify-send "VPN" "Connected to $country" && \
echo "on" > $tmpfile;
fi
2019-05-16 21:13:08 +02:00
#+end_src
2020-03-28 11:10:14 +01:00
* =i3exit= - Manage lock, suspend, reboot, ...
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: i3exit
:END:
2019-05-16 21:13:08 +02:00
#+begin_src bash :tangle ~/.local/bin/i3exit
option=$(echo -e "Lock\nExit\nLogout\nSuspend\nHibernate\nReboot\nShutdown" | rofi -i -dmenu)
case "$option" in
"Lock")
~/.local/bin/lockscreen
;;
"Exit")
pkill -15 -t tty"$XDG_VTNR" Xorg
;;
"Logout")
loginctl terminate-session `loginctl session-status | head -n 1 | awk '{print $1}'`
;;
"Suspend")
systemctl suspend && ~/.local/bin/lockscreen
;;
"Hibernate")
systemctl hibernate && ~/.local/bin/lockscreen
;;
"Reboot")
confirmation=$(echo -e "Yes\nNo" | rofi -i -p "Are you sure you want to Reboot?" -dmenu)
case "$confirmation" in
"Yes")
systemctl reboot
;;
esac
;;
"Shutdown")
confirmation=$(echo -e "Yes\nNo" | rofi -i -p "Are you sure you want to Shutdown?" -dmenu)
case "$confirmation" in
"Yes")
systemctl poweroff
;;
esac
;;
,*)
echo "== ! i3exit: missing or invalid argument ! =="
exit 2
esac
exit 0
2019-05-16 21:13:08 +02:00
#+end_src
2019-06-22 17:27:21 +02:00
2020-05-26 08:33:31 +02:00
* =askpass-rofi= - GUI prompt for passwords
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: askpass-rofi
:END:
2019-05-16 21:13:08 +02:00
Take password prompt from STDIN, print password to STDOUT.
The sed piece just removes the colon from the provided prompt: =rofi -p= already gives us a colon
2021-01-01 20:12:34 +01:00
#+BEGIN_SRC bash :tangle ~/.local/bin/askpass-rofi
rofi -dmenu \
-password \
-no-fixed-num-lines \
-p "$(printf "$1" | sed s/://)"
2019-05-16 21:13:08 +02:00
#+END_SRC
2019-06-22 17:27:21 +02:00
2022-02-06 21:46:59 +01:00
* =get-pass= - Get Stored Password
:PROPERTIES:
:CUSTOM_ID: get-pass
:END:
#+BEGIN_SRC bash :tangle ~/.local/bin/get-pass
pass $1 | sed -n 1p
#+END_SRC
2020-03-28 11:10:14 +01:00
* =screenshot= - Take Screenshot
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: screenshot
:END:
2019-06-22 17:27:21 +02:00
#+begin_src bash :tangle ~/.local/bin/screenshot
# Ask for screenshot type
status=$(echo -e "All\nGUI\nSelection\nCropped\nCopy\nPretty\nShadow\nWindow" | rofi -i -dmenu -p "Type")
if [ -z "$status" ]; then
exit;
fi
# Ask for filename if not copying the image
if [[ $status != "Copy" ]]; then
name=$(echo -e "screenshot-$(date +"%m-%d-%y_%H-%M-%S")" | rofi -i -dmenu -p "Filename")
if [ -z "$name" ]; then
exit;
fi
filename=~/Pictures/$name.png
fi
case "$status" in
"All")
maim -u $filename ;;
"GUI")
flameshot gui -r > $filename && \
pkill flameshot;;
"Selection")
maim -u -s $filename ;;
"Cropped")
maim -u -s $filename && convert -trim $filename $filename ;;
"Copy")
maim -u -s | xclip -selection clipboard -t image/png ;;
"Shadow")
maim -u -s | convert - \( +clone -background black -shadow 80x3+5+5 \) +swap -background none -layers merge +repage $filename ;;
"Pretty")
maim -u -s $filename && \
convert $filename \( +clone -alpha extract -draw 'fill black polygon 0,0 0,5 5,0 fill white circle 5,5 5,0' \( +clone -flip \) -compose Multiply -composite \( +clone -flop \) -compose Multiply -composite \) -alpha off -compose CopyOpacity -composite $filename && \
convert $filename \( +clone -background black -shadow 40x5+0+0 \) +swap -background none -layers merge +repage $filename ;;
"Window")
maim -u -i $(xdotool selectwindow) $filename ;;
esac
2019-06-22 17:27:21 +02:00
#+end_src
2019-07-12 13:26:49 +02:00
2020-03-28 11:10:14 +01:00
* =network-toggle= - Toggle Network
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: network-toggle
:END:
Minimal network manager to just toggle the Wifi or Ethernet connection.
2019-07-12 13:26:49 +02:00
#+begin_src bash :tangle ~/.local/bin/network-toggle
result=$(nmcli device | sed '1d' | dmenu -l 20);
2019-07-12 13:26:49 +02:00
interface=$(echo $result | awk -F ' ' '{print $1}');
status=$(echo $result | awk -F ' ' '{print $3}');
2019-07-12 13:26:49 +02:00
if [ $status == 'disconnected' ]; then
nmcli device connect $interface
else
nmcli device disconnect $interface
fi
2019-07-12 13:26:49 +02:00
#+end_src
2021-01-01 20:12:34 +01:00
* =make-gif= - Convert an =MP4= video to =GIF=
:PROPERTIES:
:CUSTOM_ID: make-gif
:END:
First argument is the =mp4= file and the second argument is the output =gif= file.
2020-03-01 21:25:11 +01:00
#+begin_src bash :tangle ~/.local/bin/make-gif
palette="/tmp/palette.png"
filters="fps=15,scale=320:-1:flags=lanczos"
2020-03-01 21:25:11 +01:00
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
2020-03-01 21:25:11 +01:00
#+end_src
2020-05-26 08:33:31 +02:00
2020-03-28 11:10:14 +01:00
* =yt-audio= - Download-Audio from youtube
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: yt-audio
:END:
2020-03-01 21:25:11 +01:00
#+begin_src bash :tangle ~/.local/bin/yt-audio
2020-03-01 21:25:11 +01:00
if [ $TMUX ]; then
tmux split -v -l 5 "cd ~/Downloads/ && youtube-dl --add-metadata -xic -f bestaudio/best $1" && tmux select-pane -U
2020-03-01 21:25:11 +01:00
else
cd ~/Downloads/;
setsid nohup youtube-dl --add-metadata -xic -f bestaudio/best $1 &> /dev/null &
2020-03-01 21:25:11 +01:00
fi
#+end_src
2020-03-28 11:10:14 +01:00
* =yt-video= - Download-Video from youtube
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: yt-video
:END:
2020-03-01 21:25:11 +01:00
#+begin_src bash :tangle ~/.local/bin/yt-video
2020-03-01 21:25:11 +01:00
if [ $TMUX ]; then
tmux split -v -l 5 "cd ~/Downloads/ && youtube-dl --add-metadata -ic $1" && tmux select-pane -U
2020-03-01 21:25:11 +01:00
else
cd ~/Downloads/;
setsid nohup youtube-dl --add-metadata -ic $1 &> /dev/null &
2020-03-01 21:25:11 +01:00
fi
#+end_src
* =setbg= - Set Background
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: setbg
:END:
First argument is either:
- the background file
- a directory, in such case it will pick a random picture file from that directory
#+begin_src bash :tangle ~/.local/bin/setbg
bgloc="${XDG_CACHE_HOME:-$HOME/.cache/}/bg"
# If the argument is a file
[ -f "$1" ] && ln -sf "$(readlink -f "$1")" "$bgloc"
# If the argument is a directory
[ -d "$1" ] && ln -sf "$(find "$(readlink -f "$1")" -iregex '.*.\(jpg\|jpeg\|png\|gif\)' -type f | shuf -n 1)" "$bgloc"
# Set the wallpaper
xwallpaper --zoom "$bgloc"
#+end_src
* =insert-unicode= - Insert Unicode Icon
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: insert-unicode
:END:
The list of emojis is available [[file:data.org::#emojis][here]].
#+begin_src bash :tangle ~/.local/bin/insert-unicode
# Must have xclip installed to even show menu.
xclip -h 2>/dev/null || exit 1
chosen=$(cut -d ';' -f1 ~/.local/data/emojis | rofi -dmenu -i -l 20 | sed "s/ .*//")
[ "$chosen" != "" ] || exit
# If you run this command with an argument, it will automatically insert the character.
if [ -n "$1" ]; then
xdotool key Shift+Insert
else
echo "$chosen" | tr -d '\n' | xsel -ib
2022-02-06 21:45:41 +01:00
notify-send "'$chosen' copied to clipboard." &
fi
#+end_src
* =insert-nerd-fonts= - Insert Nerd Font Icon
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: insert-nerd-fonts
:END:
The list of emojis is available [[file:data.org::#nerd-fonts][here]].
#+begin_src bash :tangle ~/.local/bin/insert-nerd-fonts
# Must have xsel installed to even show menu.
xsel -h 2>/dev/null || exit 1
chosen=$(cat ~/.local/data/nerd-fonts | rofi -dmenu -i -l 20 | sed "s/ .*//")
[ "$chosen" != "" ] || exit
# If you run this command with an argument, it will automatically insert the character.
if [ -n "$1" ]; then
xdotool key Shift+Insert
else
echo "$chosen" | tr -d '\n' | xsel -ib
2022-02-06 21:45:41 +01:00
notify-send "'$chosen' copied to clipboard." &
fi
#+end_src
2021-10-25 14:22:47 +02:00
* =linkhandler= - Open any URL with Default application
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: linkhandler
:END:
Inspired from =linkhandler= script ([[https://github.com/LukeSmithxyz/voidrice/][github]]).
2021-10-25 14:22:47 +02:00
This is used to open any *url* with the wanted program.
#+begin_src bash :tangle ~/.local/bin/linkhandler
[ -z "$1" ] && { "$BROWSER"; exit; }
case "$1" in
2021-10-25 14:22:47 +02:00
,*mkv|*webm|*mp4|*youtube.com/watch*|*youtube.com/playlist*|*youtu.be*|*gif)
setsid mpv -quiet "$1" >/dev/null 2>&1 & ;;
,*png|*jpg|*jpe|*jpeg)
curl -sL "$1" > "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" && sxiv -a "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" >/dev/null 2>&1 & ;;
,*pdf|*cbz|*cbr)
curl -sL "$1" > "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" && zathura "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" >/dev/null 2>&1 & ;;
,*svg)
curl -sL "$1" > "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" && inkview "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" >/dev/null 2>&1 & ;;
,*mp3|*flac|*opus|*mp3?source*)
setsid curl -LO "$1" >/dev/null 2>&1 & ;;
,*)
2021-10-25 14:22:47 +02:00
[ -f "$1" ] && setsid -f "$TERMINAL" -e "$EDITOR" "$1" >/dev/null 2>&1 || setsid -f "$BROWSER" "$1" >/dev/null 2>&1
esac
#+end_src
2021-01-01 20:12:34 +01:00
2021-10-25 14:22:47 +02:00
* =open= - Open any file using =rifle=
:PROPERTIES:
:CUSTOM_ID: filehandler
:END:
Simple alternative to =xdg-open=, uses the =rifle= program included with =ranger=.
The default applications are listed [[file:ranger.org::#rifle][here]].
#+begin_src bash :tangle ~/.local/bin/open
rifle -p $(rifle -l "$1" | rofi -dmenu -i | sed -n -e 's/\([0-9]*\):.*/\1/p') "$1"
#+end_src
2021-01-01 20:12:34 +01:00
* =lockscreen= - Lock Screen
:PROPERTIES:
2021-01-01 20:12:34 +01:00
:CUSTOM_ID: lockscreen
:header-args: :tangle ~/.local/bin/lockscreen
:END:
2021-01-01 20:12:34 +01:00
A nice lockscreen that uses =i3lock=.
It takes a screenshot, pixelize it and overlay an image in the lockscreens folder.
#+begin_src bash
# First, turn off dunst
killall -SIGUSR1 dunst && echo "off" > /tmp/dunststatus;
# Turn off the music if it is playing.
MPC_STATE=$(mpc | sed -n '2p' | cut -d "[" -f2 | cut -d "]" -f1)
if [[ $MPC_STATE == "playing" ]]; then
mpc pause
fi
# Take the screenshot and process it nicely
temp_file="/tmp/screen.png"
rm -f $temp_file
maim $temp_file && \
# Pixelize the Screenshot
convert $temp_file -scale 10% -scale 1000% $temp_file && \
# Overlay a random image in the lockscreens folder
composite -gravity center $(find ~/.local/data/lockscreens/ -type f | shuf -n 1) $temp_file $temp_file;
# Finally, lock the screen using =i3lock=
i3lock --ignore-empty-password --nofork --image=$temp_file && \
# When unlocking, restart dunst
killall -SIGUSR2 dunst && echo "on" > /tmp/dunststatus
#+end_src
2021-01-01 20:12:34 +01:00
* =mopidy-restart= - Restart Mopidy
:PROPERTIES:
:CUSTOM_ID: mopidy-restart
:END:
2021-01-01 20:12:34 +01:00
Sometimes =mopidy= need to be restarted...
#+begin_src bash :tangle ~/.local/bin/mopidy-restart
pids=( $(pgrep -f mopidy) )
for pid in "${pids[@]}"; do
if [[ $pid != $$ ]]; then
kill "$pid"
fi
done
echo "Killed mopidy."
echo "Restarting mopidy..."
mopidy --config ~/.config/mopidy/mopidy.conf >/dev/null 2>&1 &
echo "Done"
#+end_src
2020-05-26 08:33:31 +02:00
* =upload= - Upload Script
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: upload
:END:
Upload a file to https://0x0.st/ and copy the generated url.
2020-05-26 08:33:31 +02:00
#+begin_src bash :tangle ~/.local/bin/upload
if [ $TMUX ]; then
tmux split -v -l 1 "curl --progress-bar -F\"file=@$1\" https://0x0.st | xsel -ib;" && tmux select-pane -U
else
curl --progress-bar -F"file=@$1" https://0x0.st | xsel -ib && \
2022-02-06 21:45:41 +01:00
notify-send 'Upload' 'Successful' || \
notify-send --urgency=critical 'Upload' 'Failed'
fi
2020-05-26 08:33:31 +02:00
#+end_src
* =weather= - Display Weather in terminal
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: weather
:END:
Get the weather from http://wttr.in/.
2020-05-26 08:33:31 +02:00
#+begin_src bash :tangle ~/.local/bin/weather
if [ -n "$*" ]; then
address="wttr.in/"
address+=$*
else
address="wttr.in/"
fi
2020-05-26 08:33:31 +02:00
if type sxiv > /dev/null 2>&1; then
address+=".png"
2020-05-26 08:33:31 +02:00
wget -qO- "$address" > /tmp/weather.png && \
sxiv -b /tmp/weather.png
elif type feh > /dev/null 2>&1; then
address+=".png"
2020-05-26 08:33:31 +02:00
wget -qO- "$address" | feh -
else
curl "$address"
fi
2020-05-26 08:33:31 +02:00
#+end_src
* =pdf2bib= - Extract bibtex entry from PDF file
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: pdf2bib
:END:
2020-05-26 08:33:31 +02:00
#+begin_src bash :tangle ~/.local/bin/pdf2bib
pdf2doi () {
pdfinfo "$1" | grep -io "doi:.*" | grep -Poi "10.\d+/[^\s]+" || \
pdftotext "$1" 2>/dev/null - | grep -io "doi:.*" -m 1 | grep -Poi "10.\d+/[^\s]+" || \
pdftotext "$1" 2>/dev/null - | grep -Poi "10.\d+/[^\s]+"
}
doi2bib () {
# curl -LHs "Accept: application/x-bibtex" http://dx.doi.org/$1 -w "\\n"
curl -s "http://api.crossref.org/works/$1/transform/application/x-bibtex" -w "\\n"
}
# If a file is specified, try to extract DOI from the file
if [ -f "$1" ]; then
doi=$(pdf2doi "$1")
fi
2020-05-26 08:33:31 +02:00
if hash rofi 2>/dev/null; then
doi=$(echo "$doi" | rofi -i -dmenu -p "DOI")
else
doi=$(echo "$doi" | dmenu -i -p "DOI")
fi
2020-05-26 08:33:31 +02:00
if [ -n "$1" ]; then
doi2bib $doi | xsel -ib && \
2022-02-06 21:45:41 +01:00
notify-send 'BibTeX' 'Copied to Clipboard' || \
notify-send --urgency=critical 'BibTeX' 'Failed'
fi
2020-05-26 08:33:31 +02:00
#+end_src
* =pdf2png= - Convert a PDF to PNG
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: pdf2png
:END:
2020-05-26 08:33:31 +02:00
#+begin_src bash :tangle ~/.local/bin/pdf2png
# Check if the input argumetn is a PDF file
if [[ -f "$1" && "$1" == *.pdf ]]; then
pdftoppm -png "$1" > "$(echo "$1" | cut -f 1 -d '.' | sed 's/$/.png/')"
fi
2020-05-26 08:33:31 +02:00
#+end_src
2021-10-25 14:22:47 +02:00
* TODO =convert-file= - Convert any file to another filetype
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: convert-file
2021-10-25 14:22:47 +02:00
:header-args: :tangle ~/.local/bin/convert-file
2021-01-01 20:12:34 +01:00
:END:
2021-10-25 14:22:47 +02:00
** Get basic information about the file
#+begin_src bash
# Get filename
filename_with_extension=$(basename -- "$1")
# Extract extension of the file
in_ext="${filename_with_extension##*.}"
# filename without extension
filename_without_extension=${filename_with_extension%.*}
2021-10-25 14:22:47 +02:00
#+end_src
** SVG files
List of useful programs:
- =inkscape=
*** SVG to PNG
#+begin_src bash
svg2png_function() { \
if command -v inkscape &> /dev/null; then
inkscape --export-type="$out_ext" --export-dpi=200 --export-area-drawing "$filename_with_extension"
fi
}
#+end_src
2021-10-25 14:22:47 +02:00
*** SVG to PDF
#+begin_src bash
svg2pdf_function() { \
if command -v inkscape &> /dev/null; then
inkscape --export-type="$out_ext" "$filename_with_extension"
fi
}
#+end_src
*** Main function
#+begin_src bash
# Convert SVG Files
svg2() { \
out_ext=$(echo -e "pdf\npng" | rofi -i -dmenu -p "Convert SVG to")
if [ -z "$out_ext" ]; then
exit;
fi
case "$out_ext" in
"png")
2021-10-25 14:22:47 +02:00
svg2png_function
;;
"pdf")
2021-10-25 14:22:47 +02:00
svg2pdf_function
;;
esac
}
2021-10-25 14:22:47 +02:00
#+end_src
** PDF files
List of useful programs:
- =pdftocairo=
- =pdftoppm=
- =convert=
- =inkscape=
- =gs=
- =pdfcrop=
- =pdftk=
*** PDF to PNG
#+begin_src bash
pdf2png_function() { \
if command -v pdftocairo &> /dev/null; then
pdftocairo -png -singlefile -cropbox "$filename_with_extension" "$filename_without_extension"
elif command -v pdftoppm &> /dev/null; then
pdftoppm -png "$filename_with_extension" > "$filename_without_extension.png"
elif command -v convert &> /dev/null; then
convert -density 100 -trim -antialias "$filename_with_extension" -quality 100 "$filename_without_extension.png"
elif command -v inkscape &> /dev/null; then
inkscape --export-type="$out_ext" --export-dpi=200 --export-area-drawing "$filename_with_extension"
fi
}
#+end_src
*** PDF to SVG
#+begin_src bash
pdf2svg_function() { \
if command -v pdftocairo &> /dev/null; then
pdftocairo -svg "$filename_with_extension" "$filename_without_extension.svg"
elif command -v pdf2svg &> /dev/null; then
pdf2svg "$filename_with_extension" "$filename_without_extension.svg"
elif command -v inkscape &> /dev/null; then
inkscape --export-type="$out_ext" "$filename_with_extension"
fi
}
#+end_src
*** PDF Reduce Size
#+begin_src bash
pdfreduce_function() { \
if command -v gs &> /dev/null; then
# Maybe ask for new filename?
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$filename_without_extension.red.pdf" "$filename_with_extension"
fi
}
#+end_src
*** PDF Trim
#+begin_src bash
pdftrim_function() { \
if command -v pdfcrop &> /dev/null; then
pdfcrop "$filename_with_extension" "$filename_with_extension"
fi
}
#+end_src
*** PDF Extract Pages
#+begin_src bash
pdfextract_function() { \
if command -v pdftk &> /dev/null; then
pages=$(echo -e "get first\ndelete first\nextract i-j" | rofi -i -dmenu -p "Extract pages")
if [ -z "$pages" ]; then
exit;
fi
case "$pages" in
"get first")
if command -v pdftk &> /dev/null; then
pdftk "$filename_with_extension" cat 1-1 output "$filename_without_extension.first.pdf"
fi
;;
"delete first")
if command -v pdftk &> /dev/null; then
pdftk "$filename_with_extension" cat 2-end output "$filename_with_extension"
fi
;;
"extract i-j")
# TODO
page_i=$(rofi -dmenu -p "From")
page_j=$(rofi -dmenu -p "To")
if command -v pdftk &> /dev/null; then
pdftk "$filename_with_extension" cat "$page_i-$page_j" output "$filename_with_extension.slice.pdf"
fi
;;
esac
fi
}
#+end_src
2021-10-25 14:22:47 +02:00
*** Delete First Page
#+begin_src bash
pdf_delete_first_page_function() { \
if command -v stapler &> /dev/null; then
stapler del "$filename_with_extension" 1 /tmp/pdftk_out.pdf && mv /tmp/pdftk_out.pdf "$filename_with_extension"
elif command -v pdftk &> /dev/null; then
pdftk "$filename_with_extension" cat 2-end output /tmp/pdftk_out.pdf && mv /tmp/pdftk_out.pdf "$filename_with_extension"
fi
}
#+end_src
*** Remove Annotations
#+begin_src bash
pdf_remove_annotations_function() { \
if command -v pdftk &> /dev/null; then
pdftk "$filename_with_extension" output /tmp/uncompressed.pdf uncompress
LANG=C sed -n '/^\/Annots/!p' /tmp/uncompressed.pdf > /tmp/stripped.pdf
pdftk /tmp/stripped.pdf output "$filename_with_extension" compress
fi
}
#+end_src
*** Main function
#+begin_src bash
# Convert PDF Files
pdf2() { \
2021-10-25 14:22:47 +02:00
out_ext=$(echo -e "svg\npng\nreduce size\ntrim\nextract pages\ndelete first page\nremove annotations" | rofi -i -dmenu -p "Convert PDF to")
if [ -z "$out_ext" ]; then
exit;
fi
case "$out_ext" in
2021-10-25 14:22:47 +02:00
"svg")
pdf2svg_function
;;
"png")
2021-10-25 14:22:47 +02:00
pdf2png_function
;;
"reduce size")
pdfreduce_function
;;
2021-10-25 14:22:47 +02:00
"trim")
pdftrim_function
2021-04-25 19:10:57 +02:00
;;
2021-10-25 14:22:47 +02:00
"extract pages")
pdfextract_function
;;
"delete first page")
pdf_delete_first_page_function
;;
"remove annotations")
pdf_remove_annotations_function
;;
esac
}
#+end_src
** DVI files
List of useful programs:
- =dvisvgm=
- =dvipng=
*** DVI to PNG
#+begin_src bash
dvi2png_function() { \
if command -v dvipng &> /dev/null; then
dvipng "$filename_with_extension"
fi
}
#+end_src
*** DVI to SVG
#+begin_src bash
dvi2svg_function() { \
if command -v dvisvgm &> /dev/null; then
dvisvgm "$filename_with_extension" -o "$filename_without_extension.svg"
fi
}
#+end_src
*** Main function
#+begin_src bash
# Convert DVI Files
dvi2() { \
out_ext=$(echo -e "svg\npng" | rofi -i -dmenu -p "Convert DVI to")
if [ -z "$out_ext" ]; then
exit;
fi
case "$out_ext" in
"svg")
2021-10-25 14:22:47 +02:00
dvi2svg_function
;;
"png")
dvi2png_function
;;
esac
}
2021-10-25 14:22:47 +02:00
#+end_src
2021-10-25 14:22:47 +02:00
** DOCX/PTTX files
*** DOC to PDF
#+begin_src bash
doc2pdf_function() { \
if command -v lowriter &> /dev/null; then
lowriter --convert-to pdf "$filename_with_extension"
fi
}
#+end_src
*** Main function
#+begin_src bash
# Convert DOCX/PPTX Files
docx2() { \
out_ext=$(echo -e "pdf" | rofi -i -dmenu -p "Convert DOCX/PPTX to")
if [ -z "$out_ext" ]; then
exit;
fi
case "$out_ext" in
"pdf")
2021-10-25 14:22:47 +02:00
doc2pdf_function
;;
esac
}
2021-10-25 14:22:47 +02:00
#+end_src
2021-10-25 14:22:47 +02:00
** GIF files
*** GIF to PNG
#+begin_src bash
gif2png_function() { \
if command -v convert &> /dev/null; then
convert -coalesce "$filename_with_extension" "$filename_without_extension.png"
fi
}
#+end_src
*** Main function
#+begin_src bash
2021-04-25 19:10:57 +02:00
# Convert GIF Files
gif2() { \
out_ext=$(echo -e "png" | rofi -i -dmenu -p "Convert GIF to")
if [ -z "$out_ext" ]; then
exit;
fi
case "$out_ext" in
"png")
2021-10-25 14:22:47 +02:00
gif2png_function
2021-04-25 19:10:57 +02:00
;;
esac
}
2021-10-25 14:22:47 +02:00
#+end_src
** PNG files
*** PNG to PDF
#+begin_src bash
png2pdf_function() { \
if command -v convert &> /dev/null; then
convert "$filename_with_extension" "$filename_without_extension.pdf"
fi
}
#+end_src
*** PNG to JPG
#+begin_src bash
png2jpg_function() { \
if command -v convert &> /dev/null; then
convert "$filename_with_extension" "$filename_without_extension.jpg"
fi
}
#+end_src
*** PNG Trim
#+begin_src bash
pngtrim_function() { \
if command -v convert &> /dev/null; then
convert -trim "$filename_with_extension" "$filename_with_extension"
fi
}
#+end_src
*** PNG Resize
#+begin_src bash
pngresize_function() { \
if command -v convert &> /dev/null; then
size_type=$(echo -e "width\nheight" | rofi -i -dmenu -p "Maximum:")
size_px=$(rofi -dmenu -p "Number of px:")
if [ "$size_type" = "width" ]; then
convert -resize "$size_px"x "$filename_with_extension" "$filename_with_extension"
elif [ "$size_type" = "height" ]; then
convert -resize x"$size_px" "$filename_with_extension" "$filename_with_extension"
fi
fi
}
#+end_src
2021-04-25 19:10:57 +02:00
2021-10-25 14:22:47 +02:00
*** Main function
#+begin_src bash
# Convert PNG Files
png2() { \
2021-10-25 14:22:47 +02:00
out_ext=$(echo -e "pdf\njpg\ntrim\nresize" | rofi -i -dmenu -p "Convert PNG to")
if [ -z "$out_ext" ]; then
exit;
fi
case "$out_ext" in
"pdf")
2021-10-25 14:22:47 +02:00
png2pdf_function
;;
"jpg")
png2jpg_function
;;
2021-04-25 19:10:57 +02:00
"trim")
2021-10-25 14:22:47 +02:00
pngtrim_function
;;
"resize")
pngresize_function
2021-04-25 19:10:57 +02:00
;;
esac
}
2021-10-25 14:22:47 +02:00
#+end_src
** JPG files
*** JPG to PDF
#+begin_src bash
jpg2pdf_function() { \
if command -v convert &> /dev/null; then
convert "$filename_with_extension" "$filename_without_extension.pdf"
fi
}
#+end_src
2021-10-25 14:22:47 +02:00
*** JPG Resize
#+begin_src bash
jpgresize_function() { \
if command -v convert &> /dev/null; then
size_type=$(echo -e "width\nheight" | rofi -i -dmenu -p "Maximum:")
size_px=$(rofi -dmenu -p "Number of px:")
if [ "$size_type" = "width" ]; then
convert -resize "$size_px"x "$filename_with_extension" "$filename_with_extension"
elif [ "$size_type" = "height" ]; then
convert -resize x"$size_px" "$filename_with_extension" "$filename_with_extension"
fi
fi
}
#+end_src
*** Main function
#+begin_src bash
# Convert JPG Files
jpg2() { \
out_ext=$(echo -e "pdf\nresize" | rofi -i -dmenu -p "Convert JPG to")
if [ -z "$out_ext" ]; then
exit;
fi
case "$out_ext" in
"pdf")
jpg2pdf_function
;;
"resize")
jpgresize_function
;;
esac
}
#+end_src
** MP4 files
*** MP4 to GIF
#+begin_src bash
mp42gif_function() { \
if command -v ffmpeg &> /dev/null; then
palette="/tmp/palette.png"
width=$(echo -e "auto" | rofi -i -dmenu -p "GIF width")
if [ "$width" = "auto" ]; then
filters="fps=15"
else
filters="fps=15,scale=$width:-1:flags=lanczos"
fi
# Generate optimal pallette of colors
ffmpeg -v warning -i "$filename_with_extension" -vf "$filters,palettegen" -y $palette
# Convert
ffmpeg -v warning -i "$filename_with_extension" -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$filename_without_extension.gif"
fi
}
#+end_src
*** Main function
#+begin_src bash
# Convert MP4 Files
mp42() { \
out_ext=$(echo -e "gif" | rofi -i -dmenu -p "Convert MP4 to")
if [ -z "$out_ext" ]; then
exit;
fi
case "$out_ext" in
"gif")
2021-10-25 14:22:47 +02:00
mp42gif_function
;;
esac
}
2021-10-25 14:22:47 +02:00
#+end_src
2021-10-25 14:22:47 +02:00
** Case statement
#+begin_src bash
case "$in_ext" in
"svg")
svg2
;;
2021-04-25 19:10:57 +02:00
"gif")
gif2
;;
2021-10-25 14:22:47 +02:00
"dvi")
dvi2
;;
"pdf")
pdf2
;;
"mp4")
mp42
;;
"png")
png2
;;
2021-10-25 14:22:47 +02:00
"jpg")
jpg2
;;
"jpeg")
jpg2
;;
"docx")
docx2
;;
"pttx")
docx2
;;
esac
2021-01-01 20:12:34 +01:00
#+end_src
2022-05-09 10:06:19 +02:00
* =preview-file= - Preview any file
:PROPERTIES:
:CUSTOM_ID: preview-file
:header-args: :tangle ~/.local/bin/preview-file
:END:
** Get basic information about the file
#+begin_src bash
# Get filename
filename_with_extension=$(basename -- "$1")
# Extract extension of the file
in_ext="${filename_with_extension##*.}"
# filename without extension
filename_without_extension=${filename_with_extension%.*}
#+end_src
** SVG files
#+begin_src bash
# Convert SVG Files
svg2() { \
if command -v inkscape &> /dev/null; then
inkscape --export-type="$out_ext" "$filename_with_extension" --export-filename="/tmp/$filename_without_extension.pdf" && zathura "/tmp/$filename_without_extension.pdf"
fi
}
#+end_src
** DOCX files
#+begin_src bash
# Convert DOCX/PPTX Files
docx2() { \
if command -v lowriter &> /dev/null; then
lowriter --convert-to pdf "$filename_with_extension" --outdir /tmp/ && zathura "/tmp/$filename_without_extension.pdf"
fi
}
#+end_src
** Case statement
#+begin_src bash
case "$in_ext" in
"svg")
svg2
;;
"gif")
gif2
;;
"dvi")
dvi2
;;
"pdf")
pdf2
;;
"mp4")
mp42
;;
"png")
png2
;;
"jpg")
jpg2
;;
"jpeg")
jpg2
;;
"docx")
docx2
;;
"pttx")
docx2
;;
esac
#+end_src
2020-11-03 11:39:35 +01:00
* =pdf-shrink= - Pdf Shrink
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: pdf-shrink
:END:
Simply reduces the size of a given pdf file.
2020-05-26 08:33:31 +02:00
#+begin_src bash :tangle ~/.local/bin/pdf-shrink
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
2020-05-26 08:33:31 +02:00
# Output filename defaults to "-" (stdout) unless given:
if [ ! -z "$2" ]; then
OFILE="$2"
else
OFILE="-"
fi
2020-05-26 08:33:31 +02:00
# Output resolution defaults to 90 unless given:
if [ ! -z "$3" ]; then
res="$3"
else
res="90"
fi
2020-05-26 08:33:31 +02:00
shrink "$IFILE" "$OFILE" "$res" || exit $?
2020-05-26 08:33:31 +02:00
check_smaller "$IFILE" "$OFILE"
2020-05-26 08:33:31 +02:00
#+end_src
* =pdf-delete-annotations= - Delete Annotations from PDFs
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: pdf-delete-annotations
:END:
Taken from this [[https://gist.github.com/stefanschmidt/5248592][gist]].
2020-05-26 08:33:31 +02:00
#+begin_src bash :tangle ~/.local/bin/pdf-delete-annotations
# Check if the input argumetn is a PDF file
if [[ -f "$1" && "$1" == *.pdf ]]; then
pdftk $1 output /tmp/uncompressed.pdf uncompress
LANG=C sed -n '/^\/Annots/!p' /tmp/uncompressed.pdf > /tmp/stripped.pdf
pdftk /tmp/stripped.pdf output $1 compress
fi
2020-05-26 08:33:31 +02:00
#+end_src
* =pdf-delete-first-page= - Delete first page of PDF
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: pdf-delete-first-page
:END:
2020-05-26 08:33:31 +02:00
The requirement is to have =pdftk= or =stapler= installed.
#+begin_src bash :tangle ~/.local/bin/pdf-delete-first-page
# Check if the input argumetn is a PDF file
if [[ -f $1 && $1 == *.pdf ]]; then
if type stapler > /dev/null 2>&1; then
stapler del "$1" 1 /tmp/pdftk_out.pdf && mv /tmp/pdftk_out.pdf "$1"
elif type pdftk > /dev/null 2>&1; then
pdftk "$1" cat 2-end output /tmp/pdftk_out.pdf && mv /tmp/pdftk_out.pdf "$1"
else
echo "Neither pdftk nor stapler are installed"
fi
fi
2020-05-26 08:33:31 +02:00
#+end_src
2020-11-03 11:39:35 +01:00
* =rofi-calc= - Simple Calculation using Rofi
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: rofi-calc
:END:
Run some simple calculations with =rofi=.
2020-11-03 11:39:35 +01:00
#+begin_src bash :tangle ~/.local/bin/rofi-calc
rofi -show calc -mode calc -no-show-match -no-sort
2020-11-03 11:39:35 +01:00
#+end_src
* =pass-gen= - Generate Random Alphanumeric Password
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: pass-gen
:END:
2020-11-03 11:39:35 +01:00
#+begin_src bash :tangle ~/.local/bin/pass-gen
# Ask for the wanted number of caracters
num=$(rofi -dmenu -p "Number of caracters")
2021-01-01 20:12:34 +01:00
# Random generation of alphanumeric caracters
pass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
2020-11-03 11:39:35 +01:00
re='^[0-9]+$'
if [[ $num =~ $re ]] ; then
pass=${pass:0:$num}
fi
2020-11-03 11:39:35 +01:00
# Send the password to the clipboard
printf "$pass" | xclip -sel clip && \
2022-02-06 21:46:59 +01:00
notify-send 'Password' 'Generated'
2020-11-03 11:39:35 +01:00
#+end_src
2021-01-01 20:12:34 +01:00
* =sxhkd-help= - List of keybindings using Rofi
:PROPERTIES:
:CUSTOM_ID: sxhkd-help
:END:
#+begin_src bash :tangle ~/.local/bin/sxhkd-help
awk '/^[a-z]/ && last {print "<small>",$0,"\t",last,"</small>"} {last=""} /^#/{last=$0}' ~/.config/sxhkd/sxhkdrc{,.i3} |
column -t -s $'\t' |
rofi -dmenu -i -markup-rows -no-show-icons -width 1000 -lines 15 -yoffset 40
2021-01-01 20:12:34 +01:00
#+end_src
2022-05-09 10:06:19 +02:00
* =qrdecode= - Decode QRcode by taking screenshot
- =zbar-tools=
- =main=
- =xclip=
- =notify-send=
#+begin_src bash :tangle ~/.local/bin/qrdecode
image_file="/tmp/ocr.png"
# Take screenshot by selecting the area
maim -s "$image_file"
# Get the exit code of the previous command.
# So in this case, it is the screenshot command. If it did not exit with an
# exit code 0, then it means the user canceled the process of taking a
# screenshot by doing something like pressing the escape key
status=$?
# If the user pressed the escape key or did something to terminate the proccess
# taking a screenshot, then just exit
[ $status -ne 0 ] && exit 1
# Use zbarimg to decode the text from the QR code
decoded_text=$(zbarimg "$image_file" -q --raw)
if [ -z "$decoded_text" ]; then
notify-send "qrshot" "no text was detected"
rm $image_file && exit 1
fi
# Copy text to clipboard
printf %b "$decoded_text" | xclip -selection clip
# Let us know that something was decoded
notify-send "qrshot" "$decoded_text"
# Cleaning up the trash that was left behind
rm $image_file
#+end_src