Last active
November 10, 2021 05:27
-
-
Save winiciuscota/5790f7b6134518555d7d6b1426298abe to your computer and use it in GitHub Desktop.
Bash useful scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
exit_help () { echo -e "$doc"; exit 1; } | |
send-notification() { | |
DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "Script:" "$1" | |
} | |
result-message() { | |
message="$@" | |
if [[ -t 1 ]]; then | |
echo $message | |
else | |
send-notification "$message" | |
fi | |
} | |
send2clipboard() { | |
message="$@" | |
echo "$message" | xclip -sel clip | |
} | |
result-json() { | |
formatted_json="$@" | |
if [[ -t 1 ]]; then | |
echo $formatted_json | |
else | |
file_name="/tmp/json_text.json" | |
ps aux | grep "kate $file_name" | xargs kill -9 | |
echo $formatted_json | jq . > $file_name && kate $file_name | |
fi | |
} | |
gsudo() { | |
parameters="$@" | |
if [[ -t 1 ]]; then | |
sudo $parameters | |
else | |
pkexec $parameters | |
fi | |
} | |
format_deltatime() { | |
local secs=${1%.*-0} | |
secs=${secs%.*} | |
local formatted_runtime=`printf '%dh:%dm:%ds\n' $(($secs/3600)) $(($secs%3600/60)) $(($secs%60))` | |
echo $formatted_runtime | |
} | |
acquire-lock() { | |
if [ $# -eq 0 ] | |
then | |
echo "Must provide lock name" | |
exit | |
fi | |
lock_name=$1 | |
lock_file_name=$HOME/.$lock_name.lck | |
while test -f $lock_file_name; do | |
echo "Waiting for lock $lock_name to be released" | |
sleep 1 | |
done | |
touch $lock_file_name | |
echo "Lock $lock_name successfully acquired" | |
} | |
release-lock() { | |
if [ $# -eq 0 ] | |
then | |
echo "Must provide lock name" | |
exit | |
fi | |
lock_name=$1 | |
lock_file_name=$HOME/.$lock_name.lck | |
if test -f $lock_file_name; then | |
rm $lock_file_name | |
echo "Lock $lock_name successfully released" | |
else | |
echo "Lock file $lock_file_name doesn't exists" | |
fi | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
source common | |
file_name="$@" | |
if [ ! -f "$file_name" ]; then | |
result-message "File $file_name not found!" | |
fi | |
file_url="file://$file_name" | |
echo $file_url | xclip -i -selection clipboard -t text/uri-list | |
result-message "Press Ctrl+V to copy $file_name" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Dependencies: xclip dragon-drag-and-drop | |
source common | |
doc="$0 <filename> | |
Drags a given file to where the mouse is using dragon. Click to drop it (anywere). | |
If no filename is provided will attempt to get image from clipboard | |
(i.e. ideal to be triggered by a hotkey). | |
" | |
get_image_from_clipboard () { | |
echo "Trying to get image from clipboard" | |
image_file=/tmp/clipboard.png | |
rm $image_file | |
result=1 | |
attempts=0 | |
# Long pulling for images on clipboard, flameshot can take a while to move the image to clipboard | |
while [ $result -ne 0 ] && [ $attempts -lt 100 ]; do | |
sleep .3 | |
xclip -selection clipboard -t image/png -o > "$image_file" && identify -format '%f' "$image_file" | |
result=$? | |
let attempts++ | |
done | |
file=$image_file | |
test "$result" == 1 && echo "Unable to retrieve image from clipboard" | |
return $result | |
} | |
drag_file () { | |
eval "$(xdotool getmouselocation --shell)" | |
killall dragon-drag-and-drop 2>/dev/null | |
dragon-drag-and-drop --and-exit "$file" & | |
while true; do | |
xid="$(xdotool search --onlyvisible --class dragon-drag-and-drop | head -n 2)" | |
test -z "$xid" || break | |
sleep 0.05 | |
done | |
xdotool mousemove --sync -w "$xid" 1 1 mousedown 1 mousemove $X $Y | |
result-message "Click to drop $file..." | |
} | |
main () { | |
test "$1" == "-h" && exit_help | |
file="$1" | |
test "$file" == "" && { get_image_from_clipboard EXIT || return 1; } | |
drag_file | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The drag script was adapted from @AXGKl `s solution at mwh/dragon#24