(*
### SCRIPT NAME

Send nvALT Checkboxes to Omnifocus

### DESCRIPTION

Automatically scans nvALT files (via Hazel) and, upon finding "checkboxes,"
sends them to OmniFocus as tasks.

### LICENSE
he
(The MIT License)

Copyright © 2014 Aaron Bach bachya1208@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

author: Aaron Bach
date: July 27, 2014
version: 1.0.0
*)
on hazelProcessFile(theFile)
  -- The maildrop address to use.
  set mailDropAddress to "YOUR MAIL DROP ADDRESS"
	
  -- DO NOT CHANGE BELOW THIS LINE UNLESS YOU KNOW WHAT YOU'RE DOING.
	
  set p to POSIX path of theFile
  set l to paragraphs of (do shell script "grep '\\[ x \\]' " & quoted form of p)
	
  -- Determine whether OmniFocus is running.
  tell application "System Events"
    set omnifocusActive to count (every process whose name is "OmniFocus")
  end tell
	
  repeat with v in l
    set taskName to (do shell script "n=\"" & v & "\"; echo ${n:6}")
    set taskNote to "Created from nvALT: nvalt://find/" & my encode_text(my remove_extension(my basename(p)), true, false)
		
    if omnifocusActive > 0 then
      -- OmniFocus is running, so insert a task into it directly.
      tell application "OmniFocus" to tell document 1
        make new inbox task with properties {name:taskName, note:taskNote}
      end tell
    else
      -- OmniFocus isn't running, so use the Mail Drop method.
      do shell script "echo \"" & taskNote & "\" | mail -s \"" & taskName & "\" " & mailDropAddress
    end if
  end repeat
end hazelProcessFile

on basename(thePath) -- Requires POSIX path
  if thePath ends with "/" then
    set nameIndex to -2
  else
    set nameIndex to -1
  end if
  set ASTID to AppleScript's text item delimiters
  set AppleScript's text item delimiters to "/"
  set thePath to text item nameIndex of thePath
  set AppleScript's text item delimiters to ASTID
  return thePath
end basename

on encode_char(this_char)
  set the ASCII_num to (the ASCII number this_char)
  set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
  set x to item ((ASCII_num div 16) + 1) of the hex_list
  set y to item ((ASCII_num mod 16) + 1) of the hex_list
  return ("%" & x & y) as string
end encode_char

on encode_text(this_text, encode_URL_A, encode_URL_B)
  set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789"
  set the URL_A_chars to "$+!'/?;&@=#%><{}[]\"~`^\\|*"
  set the URL_B_chars to ".-_:"
  set the acceptable_characters to the standard_characters
  if encode_URL_A is false then set the acceptable_characters to the acceptable_characters & the URL_A_chars
    if encode_URL_B is false then set the acceptable_characters to the acceptable_characters & the URL_B_chars
      set the encoded_text to ""
      repeat with this_char in this_text
        if this_char is in the acceptable_characters then
          set the encoded_text to (the encoded_text & this_char)
        else
          set the encoded_text to (the encoded_text & encode_char(this_char)) as string
        end if
      end repeat
    end if
  return the encoded_text
end encode_text

on remove_extension(this_name)
  if this_name contains "." then
    set this_name to ¬
      (the reverse of every character of this_name) as string
    set x to the offset of "." in this_name
    set this_name to (text (x + 1) thru -1 of this_name)
    set this_name to (the reverse of every character of this_name) as string
  end if
  return this_name
end remove_extension

on replaceString(theText, oldString, newString)
  local ASTID, theText, oldString, newString, lst
  set ASTID to AppleScript's text item delimiters
  try
    considering case
      set AppleScript's text item delimiters to oldString
      set lst to every text item of theText
      set AppleScript's text item delimiters to newString
      set theText to lst as string
    end considering
    set AppleScript's text item delimiters to ASTID
    return theText
  on error eMsg number eNum
    set AppleScript's text item delimiters to ASTID
    error "Can't replaceString: " & eMsg number eNum
  end try
end replaceString