Created
November 9, 2022 01:24
-
-
Save m4r1vs/8b508e70981be4f6c5cfd6478d037679 to your computer and use it in GitHub Desktop.
Ask Totoro Script
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 | |
# chmod +x ./ask-totoro.sh | |
# sudo mv ask-totoro.sh /path/to/bin/ask-totoro (e.g /usr/bin or ~/.local/bin) | |
# rofi -modi "ask-totoro:ask-totoro" -show ask-totoro | |
# i3 config (optional): | |
# bindym $mod+c exec --no-startup-id rofi -modi "ask-totoro:ask-totoro" -show ask-totoro | |
if [ ! -z "$@" ]; then | |
input=$(echo "$@") | |
if [ "$input" == "Copied!" ] || [ "$input" == "Goodbye" ]; then | |
exit 1 | |
fi | |
if [ $(tr -dc ' ' <<<"$input" | wc -c) -gt 1 ]; then | |
first_word=$(echo "$input" | head -n1 | cut -d " " -f1) | |
second_word=$(echo "$input" | head -n1 | cut -d " " -f2) | |
third_word=$(echo "$input" | head -n1 | cut -d " " -f3) | |
if [ "$first_word" == "Copy" ] && [ "$second_word" == "Result:" ]; then | |
echo -n $input | sed 's/Copy Result: //' | xclip -f -i -selection "clipboard" &>/dev/null | |
notify-send "Copied Result" | |
exit 0 | |
fi | |
if [ "$first_word" == "Type" ] && [ "$second_word" == "Result:" ]; then | |
to_be_typed=$(echo -n $input | sed 's/Type Result: //') | |
coproc ( sleep 0.01 && xdotool type --clearmodifiers "$to_be_typed" > /dev/null 2>&1 ) | |
exit 0 | |
fi | |
if [ "$first_word" == "Ask" ] && [ "$second_word" == "Again:" ]; then | |
echo "Goodbye" | |
to_be_typed=$(echo -n $input | sed 's/Ask Again: //') | |
xdotool type --clearmodifiers "$to_be_typed" | |
exit 0 | |
fi | |
fi | |
# add your api key here (2000 free queries a month): | |
APPID="WOLFRAM_ALPHA_API_KEY" | |
RESPONSE=$(curl -s "https://api.wolframalpha.com/v1/result?appid=$APPID&units=metric&" --data-urlencode "i=$*") | |
if [ "$RESPONSE" == "No short answer available" ]; then | |
# replace brave with your browser of choice | |
coproc ( brave --new-window "https://wolframalpha.com/input?i=$*" > /dev/null 2>&1 ) | |
exit 0 | |
else | |
echo "Copy Result: $RESPONSE" | |
echo "Ask Again: $RESPONSE" | |
echo "Type Result: $RESPONSE" | |
echo "Goodbye" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment