Last active
January 23, 2023 13:42
-
-
Save JayBrown/2f463ad06773ee16f7edbc49df89e1d2 to your computer and use it in GitHub Desktop.
macOS shell script for Automator QuickAction: send Twitter $ search terms to Google search
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
# Twitter to Google search | |
# 0.1 | |
export LANG=en_US.UTF-8 | |
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:/opt/local/bin:/opt/local/sbin:"$HOME"/.local/bin:"$HOME"/.local/sbin | |
_beep () { | |
osascript -e 'beep' -e 'delay 0.5' &>/dev/null | |
} | |
if ! [[ $* ]] ; then | |
textinput=$(pbpaste) | |
else | |
textinput="$*" | |
fi | |
searchstrings=$(echo "$textinput" | tr ' ' '\n' | grep "^\\$") | |
if ! [[ $searchstrings ]] ; then | |
echo "ERROR: no \$ search strings detected!" >&2 | |
_beep | |
exit | |
else | |
if [[ $(echo "$searchstrings" | wc -l) -eq 1 ]] ; then | |
echo -e "Detected \$ search string:\n$searchstrings\nSending to Google search..." | |
open "https://www.google.com/search?q=$searchstrings&ie=utf-8&oe=utf-8" 2>/dev/null | |
exit | |
fi | |
searchstrings=$(echo "$searchstrings" | sort | awk '!a[$0]++') | |
echo -e "Detected \$ search strings:\n$searchstrings" | |
searchstrings=$(echo -e "Search for all…\n$searchstrings") | |
fi | |
echo "Asking user for selection..." | |
searchchoices=$(osascript 2>/dev/null << EOS | |
tell application "System Events" | |
activate | |
set theList to {} | |
set theItems to paragraphs of "$searchstrings" | |
repeat with anItem in theItems | |
set theList to theList & {(anItem) as string} | |
end repeat | |
set AppleScript's text item delimiters to return & linefeed | |
set theResult to choose from list theList with prompt "Select your search(es)." with title "Twitter to Google Search" OK button name "Select" cancel button name "Cancel" with multiple selections allowed | |
return the result as string | |
set AppleScript's text item delimiters to "" | |
end tell | |
theResult | |
EOS | |
) | |
if ! [[ $searchchoices ]] || [[ $searchchoices == "false" ]] ; then | |
echo "User canceled" | |
_beep | |
exit | |
fi | |
if echo "$searchchoices" | grep "Search for all" &>/dev/null ; then | |
searchchoices=$(echo "$searchstrings" | grep -v "Search for all") | |
echo -e "User selected to search for all strings:\n$searchchoices" | |
else | |
echo -e "User selected to search for:\n$searchchoices" | |
fi | |
while read -r searchchoice | |
do | |
echo "Sending to Google search: $searchchoice" | |
open "https://www.google.com/search?q=$searchchoice&ie=utf-8&oe=utf-8" 2>/dev/null | |
sleep .1 | |
done < <(echo "$searchchoices") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment