Last active
March 23, 2022 11:57
-
-
Save yo-goto/7acfa712006488466d73ff42b9d952cc to your computer and use it in GitHub Desktop.
searching for keywords on Google from fish shell (fish shellからGoogle検索するスクリプト)
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
# shellからgoogle検索できる関数(macOS用) | |
# v0.2.10 | |
# コマンドの引数をホワイトスペースで連結してからURLエンコーディングして選択したブラウザで開く | |
function ggl -d "Search for keywords on Google" | |
# arguments (& options) handling | |
argparse \ | |
-x 'v,c,s,f' \ | |
-x 't,h' \ | |
-x 'z,q,g' \ | |
't/test' 'h/help' \ | |
'e/english' 'i/image' 'p/perfect' 'n/nonperson' \ | |
'z/zenn' 'q/qiita' 'g/github' \ | |
'v/vivaldi' 'c/chrome' 's/safari' 'f/firefox' \ | |
-- $argv | |
or return | |
# set variables ## () : command substitution | |
set -l pcolor bryellow # text coloring | |
set -l keyword (string join " " $argv) | |
set -l encoding (string escape --style=url $keyword) | |
set -l baseURL "https://www.google.com/search?q=" | |
# site option | |
set -q _flag_zenn; and set -l baseURL "https://zenn.dev/search?q=" | |
set -q _flag_qiita; and set -l baseURL "https://qiita.com/search?q=" | |
set -q _flag_github; and set -l baseURL "https://github.com/search?q=" | |
# help option | |
if set -q _flag_help | |
set_color $pcolor | |
echo 'Utilities:'(set_color normal) | |
echo ' -t or --test URL generation test' | |
echo ' -h or --help Print this help message' | |
set_color $pcolor | |
echo 'Browser Option:'(set_color normal) | |
echo ' -v or --vivaldi Use Vivaldi' | |
echo ' -c or --chrome Use Google Chrome' | |
echo ' -s or --safari Use Safari' | |
echo ' -f or --firefox Use Firefox' | |
set_color $pcolor | |
echo 'Search Options:'(set_color normal) | |
echo ' -e or --english English search' | |
echo ' -i or --image Image serch' | |
echo ' -p or --perfect Exact match' | |
echo ' -n or --nonperson Non-Personalized search' | |
set_color $pcolor | |
echo 'Site Option:'(set_color normal) | |
echo ' -z or --zenn Search with Zenn' | |
echo ' -q or --qiita Search with Qiita' | |
echo ' -g or --github Search with Github' | |
return | |
end | |
# open a browser | |
if test -n "$encoding" | |
## google search options | |
set -q _flag_english; and set _flag_english "lr=lang_en" | |
set -q _flag_image; and set _flag_image "tbm=isch" | |
set -q _flag_nonperson; and set _flag_nonperson "pws=0" | |
### exact match with double quotes "" | |
set -q _flag_perfect; and set _flag_perfect "%22" | |
and set encoding (string join "" $_flag_perfect $encoding $_flag_perfect) | |
set -l searchURL (string join "&" (string join "" $baseURL $encoding) $_flag_english $_flag_image $_flag_nonperson) | |
### testing for URL generation | |
if set -q _flag_test | |
echo (set_color $pcolor)"Keyword :"(set_color normal) "$keyword" | |
echo (set_color $pcolor)"URL encoding:"(set_color normal) "$encoding" | |
echo (set_color $pcolor)"Search URL :"(set_color normal) "$searchURL" | |
return | |
end | |
### browser selction | |
if set -q _flag_vivaldi | |
open -a Vivaldi "$searchURL" | |
else if set -q _flag_chrome | |
open -a "Google Chrome" "$searchURL" | |
else if set -q _flag_safari | |
open -a Safari "$searchURL" | |
else if set -q _flag_firefox | |
open -a Firefox "$searchURL" | |
else | |
#### デフォルトブラウザで開く | |
open "$searchURL" | |
end | |
echo "\"$argv\"" "について検索完了しました" | |
else | |
echo "検索したい言葉を引数として実行してください" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用方法
ユーティリティオプション
-h
: ヘルプ-t
: URL生成テストブラウザオプション(ブラウザは一つのみ選択可能)
-s
: Safari-c
: Google Chrome-v
: Vivaldi-f
: FirefoxGoogle検索オプション(
-t
オプションと組み合わせ可能)-e
: 英語検索-i
: 画像検索-p
: 完全一致検索-n
: 個人最適化検索を無効化サイト検索
-z
: Zenn-q
: Qiita-g
: Github参考と説明
https://zenn.dev/estra/articles/google-search-from-fish-shell