/LaunchBar/QuickQuestion.lbaction/Contents/Scripts/default.sh

https://github.com/ttscoff/QuickQuestion · Shell · 274 lines · 245 code · 12 blank · 17 comment · 23 complexity · 91ecac4c94d94d03d43300613496b91f MD5 · raw file

  1. #!/bin/bash
  2. #
  3. # LaunchBar Action Script
  4. #
  5. #!/bin/bash
  6. # 1.1.0
  7. # Only opens url/copies code from one note if there are multiple answers
  8. #
  9. __qq_esc () {
  10. echo "$*"|sed 's/"/\\\"/g'
  11. }
  12. __qq_remove_stopwords () {
  13. local input=$1
  14. declare -a STOPWORDS=( what which is can how do my where when why that the was who this I )
  15. for word in ${STOPWORDS[@]}; do
  16. input=$(echo "$input"|sed -E "s/(^| )$word([\.\,\? ]|$)/\1/g")
  17. done
  18. echo -n "$input"
  19. }
  20. __qq_query_include_all () {
  21. local input=$(__qq_remove_stopwords "$*")
  22. set $input
  23. declare -a query_array=( "$@" )
  24. local query=' AND ('
  25. for i in ${query_array[@]}; do
  26. query="${query}`__qq_esc $i` AND "
  27. done
  28. echo -n "$query"|sed 's/ AND $/)/'
  29. }
  30. __qq_query_exclude_all () {
  31. local input="$1"
  32. local OLDIFS=$IFS
  33. IFS=":"
  34. set $input
  35. declare -a query_array=( "$@" )
  36. local query=' NOT ('
  37. for i in ${query_array[@]}; do
  38. query="${query}filename:\"`__qq_esc $i`\" OR "
  39. done
  40. echo -n "$query"|sed 's/ OR $/)/'
  41. IFS=$OLDIFS
  42. }
  43. __qq () {
  44. ### CONFIG
  45. # notes folder, for note creation and limiting searches
  46. local NOTESDIR="$HOME/Dropbox/nvALT2.2"
  47. # extension used for your notes
  48. local NOTESEXT="md"
  49. # the prefix you use to separate "Question" notes
  50. local NOTESPRE="??"
  51. # editor command to use for modifying answers
  52. local QQEDITOR="subl"
  53. NOTESDIR="${NOTESDIR%/}/"
  54. # Exlude file names containing these phrases, separated by colons
  55. local EXCLUDENAMES="what was I doing:check for jQuery"
  56. #### END CONFIG
  57. local INPUT QUERY HAS_OPENED_URL HAS_COPIED_TEXT NOTESPREESC QUESTION ANSWER appname url
  58. local EXCLUDEQUERY=$(__qq_query_exclude_all "$EXCLUDENAMES")
  59. HAS_COPIED_TEXT=false
  60. HAS_OPENED_URL=false
  61. if [[ "$1" == "-h" ]]
  62. then
  63. appname=`basename $0`
  64. echo "$appname: build a knowledgebase with plain text files"
  65. echo "Find an answer: $appname \"terms to search for\""
  66. echo "Add a question\answer: $appname -a \"Question in natural language\" \"Succinct answer\""
  67. echo "Add a question\answer interactively: $appname -a"
  68. echo "Edit a question\answer: $appname -e \"terms to search for\" # first question found is edited"
  69. elif [[ "$1" == "-a" ]]; then
  70. if [ $# == 3 ]; then
  71. QUESTION=$2
  72. ANSWER=$3
  73. elif [ $# == 1 ]; then
  74. echo -n "Question: "
  75. read QUESTION
  76. echo -n "Answer: "
  77. read ANSWER
  78. else
  79. echo "Invalid number of arguments for -a(dd). Requires question and answer (or no arguments to input them at runtime)."
  80. echo "example: ${0##*/} -a \"What is the meaning of life?\" \"42\""
  81. exit 1
  82. fi
  83. echo -n "$ANSWER" > "${NOTESDIR}$NOTESPRE $QUESTION.$NOTESEXT" && echo "Question added and answered." || echo "Something went wrong"
  84. elif [[ "$1" == "-e" ]]; then
  85. shift
  86. QUERY="'filename:.$NOTESEXT AND filename:\"$NOTESPRE\"$(__qq_query_include_all "${*%\?}")${EXCLUDEQUERY}'"
  87. echo $QUERY
  88. ANSWER=`mdfind -onlyin "$NOTESDIR" $QUERY|head -n 1`
  89. if [[ "$ANSWER" == "" ]]; then
  90. echo "No results found for search."
  91. exit 2
  92. else
  93. $QQEDITOR "$ANSWER"
  94. fi
  95. else
  96. # local INPUT=$@
  97. # QUERY="'kind:text AND filename:.$NOTESEXT AND filename:$NOTESPRE$(__qq_query_include_all "${*%\?}")${EXCLUDEQUERY}'"
  98. QUERY="mdfind -onlyin '$NOTESDIR' -interpret '(kind:text OR kind:markdown) AND filename:.$NOTESEXT AND filename:$NOTESPRE$(__qq_query_include_all "${*%\?}")${EXCLUDEQUERY}'"
  99. RESULTS=$(eval $QUERY)
  100. echo -e "$RESULTS" | while read LINE; do
  101. if [[ "$LINE" =~ ^$ ]]; then
  102. echo "Sorry, I don't know the answer to that question."
  103. exit 1;
  104. fi
  105. QUESTION=${LINE##*/}
  106. echo -n "Q: "
  107. NOTESPREESC=`echo "$NOTESPRE"|sed -E 's/([\?\!\$\`\"]) ?/\\\\\1/g'`
  108. echo ${QUESTION%%.$NOTESEXT}|sed -E "s/$NOTESPREESC ?//g"|sed -E 's/([^\?])$/\1?/'
  109. echo -n "A: "
  110. cat "$LINE"|sed -E 's/@\([^\)]+\) ?//g'|sed -E 's/@copy\(([^\)]+)\)/\1/'|sed -E 's/@open\(([^\)+]*)\)/Related URL: \1/'|sed -E 's/@[^\( ]+ ?//g'|sed -E 's/^[ ]*|[ ]*$//g'
  111. if [[ `cat "$LINE"|grep -E '@copy\('` && $HAS_COPIED_TEXT == false ]]; then
  112. cat "$LINE"|grep '@copy('|sed -E 's/.*@copy\(([^\)]+)\).*/\1/'|tr -d '\n'|pbcopy
  113. echo "Example in clipboard"
  114. HAS_COPIED_TEXT=true
  115. fi
  116. if [[ `cat "$LINE"|grep -E '@open\('` && $HAS_OPENED_URL == false ]]; then
  117. url=$(cat "$LINE"|grep '@open('|sed -E 's/.*@open\(([^\)]+)\).*/\1 /'|tr -d '\n')
  118. open -g $url
  119. echo "Opened URL"
  120. HAS_OPENED_URL=true
  121. fi
  122. echo
  123. done
  124. fi
  125. exit 0
  126. }
  127. __qq $@
  128. #!/bin/bash
  129. # 1.1.1
  130. # Only opens url/copies code from one note if there are multiple answers
  131. #
  132. __qq_esc () {
  133. echo "$*"|sed 's/"/\\\"/g'
  134. }
  135. __qq_remove_stopwords () {
  136. local input=$1
  137. declare -a STOPWORDS=( what which is can how do my where when why that the was who this I )
  138. for word in ${STOPWORDS[@]}; do
  139. input=$(echo "$input"|sed -E "s/(^| )$word([\.\,\? ]|$)/\1/g")
  140. done
  141. echo -n "$input"
  142. }
  143. __qq_query_include_all () {
  144. if [[ "$*" != "" ]]; then
  145. local input=$(__qq_remove_stopwords "$*")
  146. declare -a query_array=( "$@" )
  147. local query=' AND ('
  148. for i in ${query_array[@]}; do
  149. query="${query}`__qq_esc $i` AND "
  150. done
  151. echo -n "$query"|sed 's/ AND $/)/'
  152. fi
  153. }
  154. __qq_query_exclude_all () {
  155. local input="$1"
  156. local OLDIFS=$IFS
  157. IFS=":"
  158. set $input
  159. declare -a query_array=( "$@" )
  160. local query=' NOT ('
  161. for i in ${query_array[@]}; do
  162. query="${query}filename:\"`__qq_esc $i`\" OR "
  163. done
  164. echo -n "$query"|sed 's/ OR $/)/'
  165. IFS=$OLDIFS
  166. }
  167. __qq () {
  168. ### CONFIG
  169. # notes folder, for note creation and limiting searches
  170. local NOTESDIR="$HOME/Dropbox/nvALT2.2"
  171. # extension used for your notes
  172. local NOTESEXT="md"
  173. # the prefix you use to separate "Question" notes
  174. local NOTESPRE="??"
  175. # editor command to use for modifying answers
  176. local QQEDITOR="subl"
  177. NOTESDIR="${NOTESDIR%/}/"
  178. # Exlude file names containing these phrases, separated by colons
  179. local EXCLUDENAMES="what was I doing"
  180. #### END CONFIG
  181. local INPUT QQQUERY HAS_OPENED_URL HAS_COPIED_TEXT NOTESPREESC QUESTION ANSWER appname url
  182. local EXCLUDEQQQUERY=$(__qq_query_exclude_all "$EXCLUDENAMES")
  183. HAS_COPIED_TEXT=false
  184. HAS_OPENED_URL=false
  185. if [[ "$1" == "-h" ]]
  186. then
  187. appname=`basename $0`
  188. echo "$appname: build a knowledgebase with plain text files"
  189. echo "Find an answer: $appname \"terms to search for\""
  190. echo "Add a question\answer: $appname -a \"Question in natural language\" \"Succinct answer\""
  191. echo "Add a question\answer interactively: $appname -a"
  192. echo "Edit a question\answer: $appname -e \"terms to search for\" # first question found is edited"
  193. elif [[ "$1" == "-a" || $# == 0 ]]; then
  194. if [ $# == 3 ]; then
  195. QUESTION=$2
  196. ANSWER=$3
  197. elif [ $# -le 1 ]; then
  198. echo -n "Question: "
  199. read QUESTION
  200. echo -n "Answer: "
  201. read ANSWER
  202. else
  203. echo "Invalid number of arguments for -a(dd). Requires question and answer (or no arguments to input them at runtime)."
  204. echo "example: ${0##*/} -a \"What is the meaning of life?\" \"42\""
  205. exit 1
  206. fi
  207. echo -n "$ANSWER" > "${NOTESDIR}$NOTESPRE $QUESTION.$NOTESEXT" && echo "Question added and answered." || echo "Something went wrong"
  208. elif [[ "$1" == "-e" ]]; then
  209. shift
  210. QQQUERY="'filename:.$NOTESEXT AND filename:\"$NOTESPRE\"$(__qq_query_include_all "${*%\?}")${EXCLUDEQQQUERY}'"
  211. ANSWER=`mdfind -onlyin "$NOTESDIR" $QQQUERY|head -n 1`
  212. if [[ "$ANSWER" == "" ]]; then
  213. echo "No results found for search."
  214. exit 2
  215. else
  216. $QQEDITOR "$ANSWER"
  217. fi
  218. else
  219. # local INPUT=$@
  220. # QQQUERY="'kind:text AND filename:.$NOTESEXT AND filename:$NOTESPRE$(__qq_query_include_all "${*%\?}")${EXCLUDEQQQUERY}'"
  221. QQQUERY="mdfind -onlyin '$NOTESDIR' -interpret '(kind:text OR kind:markdown) AND filename:.$NOTESEXT AND filename:$NOTESPRE $(__qq_query_include_all "${*%\?}")${EXCLUDEQQQUERY}'"
  222. RESULTS=$(eval $QQQUERY)
  223. echo -e "$RESULTS" | while read LINE; do
  224. if [[ "$LINE" =~ ^$ ]]; then
  225. echo "Sorry, I don't know the answer to that question."
  226. exit 1;
  227. fi
  228. QUESTION=${LINE##*/}
  229. echo -n "Q: "
  230. NOTESPREESC=`echo "$NOTESPRE"|sed -E 's/([\?\!\$\`\"]) ?/\\\\\1/g'`
  231. echo ${QUESTION%%.$NOTESEXT}|sed -E "s/$NOTESPREESC ?//g"|sed -E 's/([^\?])$/\1?/'
  232. echo -n "A: "
  233. cat "$LINE"|sed -E 's/@\([^\)]+\) ?//g'|sed -E 's/@copy\(([^\)]+)\)/\1/'|sed -E 's/@open\(([^\)+]*)\)/Related URL: \1/'|sed -E 's/@[^\( ]+ ?//g'|sed -E 's/^[ ]*|[ ]*$//g'
  234. if [[ `cat "$LINE"|grep -E '@copy\('` && $HAS_COPIED_TEXT == false ]]; then
  235. cat "$LINE"|grep '@copy('|sed -E 's/.*@copy\(([^\)]+)\).*/\1/'|tr -d '\n'|pbcopy
  236. echo "Example in clipboard"
  237. HAS_COPIED_TEXT=true
  238. fi
  239. if [[ `cat "$LINE"|grep -E '@open\('` && $HAS_OPENED_URL == false ]]; then
  240. url=$(cat "$LINE"|grep '@open('|sed -E 's/.*@open\(([^\)]+)\).*/\1 /'|tr -d '\n')
  241. open -g $url
  242. echo "Opened URL"
  243. HAS_OPENED_URL=true
  244. fi
  245. echo
  246. done
  247. fi
  248. exit 0
  249. }
  250. __qq $@