Posted By

dsanson on 09/10/09


Tagged

applescript evernote


Versions (?)

txt2evernote


 / Published in: Bash
 

A simple shell script wrapped around an applescript for piping text to Evernote. Try txt2evernote -h for options.

  1. #!/bin/bash
  2.  
  3. osaopts=
  4.  
  5. usage()
  6. {
  7. cat << EOF
  8. Usage: `basename $0` [options] file
  9. cat file | `basename $0` [options]
  10.  
  11. OPTIONS:
  12. -h Show this message
  13. -t <title> Specify title of note
  14. -d <date> Specify creation date of note
  15. -t <tag(s)> Specify tags (separate with commas)
  16. -n <notebook> Specify notebook
  17. EOF
  18. }
  19.  
  20. defined()
  21. {
  22. [ "${!1-X}" == "${!1-Y}" ]
  23. }
  24.  
  25. # parse options
  26.  
  27. while getopts "ht:d:k:n:" option
  28. do
  29. case $option in
  30. h)
  31. usage
  32. exit 1
  33. ;;
  34. t)
  35. title=$OPTARG
  36. shift $((OPTIND-1)); OPTIND=1
  37. ;;
  38. d)
  39. date=$OPTARG
  40. shift $((OPTIND-1)); OPTIND=1
  41. ;;
  42. k)
  43. tags=$OPTARG
  44. shift $((OPTIND-1)); OPTIND=1
  45. ;;
  46. n) notebook=$OPTARG
  47. shift $((OPTIND-1)); OPTIND=1
  48. ;;
  49. ?)
  50. usage
  51. exit
  52. ;;
  53. esac
  54. done
  55.  
  56. # pass options to osaopts
  57.  
  58. if defined "title"; then
  59. osaopts="$osaopts title \"$title\""
  60. fi
  61. if defined "date"; then
  62. osaopts="$osaopts created date \"$date\""
  63. fi
  64. if defined "notebook"; then
  65. osaopts="$osaopts notebook \"$notebook\""
  66. fi
  67. if defined "tags"; then
  68. tags=`echo "$tags" | perl -pe "s/,/\",\"/g"`
  69. osaopts="$osaopts tags {\"$tags\"}"
  70. fi
  71.  
  72. # If no arguments, process STDIN
  73.  
  74. if [ $# -eq 0 ]; then
  75. orig=`mktemp -t txt2evernote`
  76. cat > $orig
  77. elif [ -f "$@" ]; then # otherwise, arguments are file names
  78. orig="$@"
  79. else
  80. echo "$@ not a readable file" >&1
  81. fi
  82.  
  83. # Pass the goodies to osascript
  84.  
  85. osascript <<EOF
  86. -- read file content
  87.  
  88. set fileref_ to open for access "$orig" as POSIX file
  89. set text_ to read fileref_ as «class utf8»
  90. close access fileref_
  91.  
  92. -- send to Evernote
  93.  
  94. tell application "Evernote"
  95. create note with text text_ $osaopts
  96. end tell
  97. EOF

Report this snippet  

You need to login to post a comment.