Skip to content

Instantly share code, notes, and snippets.

@cassava
Created November 1, 2020 12:04
Show Gist options
  • Save cassava/d658f591c4b6ba25bf7e974834becb63 to your computer and use it in GitHub Desktop.
Save cassava/d658f591c4b6ba25bf7e974834becb63 to your computer and use it in GitHub Desktop.
Bash script to download URLs from the clipboard.
#!/bin/bash
# Create a pipe for async communication
pipe=$(mktemp -u)
trap "rm -f $pipe" EXIT
if [[ ! -p $pipe ]]; then
mkfifo $pipe
fi
download_loop() {
while true; do
cat $pipe | while read line; do
download_file $line
done
done
echo "Exiting download loop."
}
download_file() {
local url="$1"
shift
local name="$@"
printf "Downloading: % -64s " "$name"
wget -q "$url" -O "$name"
printf "done.\n"
}
main() {
download_loop &
while clipnotify; do
link=$(xsel -p 2>/dev/null)
text=$(zenity --entry)
if [[ "$text" == "" ]]; then
continue
fi
echo "$link" "$text" >> $pipe &
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment