Skip to content

Instantly share code, notes, and snippets.

@bachya
Created September 5, 2014 05:20
Show Gist options
  • Save bachya/166dd967f4ca33e9f946 to your computer and use it in GitHub Desktop.
Save bachya/166dd967f4ca33e9f946 to your computer and use it in GitHub Desktop.
Smarter saving of files to Evernote (regardless of whether Evernote is open)
(*
### SCRIPT NAME
Save Files to Evernote (via Hazel)
### DESCRIPTION
Allows for smarter document creation on OS X: if Evernote is running, use
Applescript to add the document; otherwise, use the user's Evernote
email address.
### INSTALLATION
1. Download the .scpt file somewhere on your filesystem.
2. Create a Hazel rule that uses it.
3. You should be good to go. :)
### LICENSE
(The MIT License)
Copyright © 2014 Aaron Bach [email protected]
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: August 2, 2014
version: 1.1.0
*)
(*
hazelProcessFile
The primary function that occurs when Hazel matches a file
*)
on hazelProcessFile(theFile)
-- The Evernote address to use
set evernoteEmailAddress to "[email protected]"
-- DO NOT CHANGE BELOW THIS LINE UNLESS YOU KNOW WHAT YOU'RE DOING.
-- Determine whether Evernote is running.
tell application "System Events"
set evernoteActive to count (every process whose name is "Evernote")
end tell
set theTags to getTags(theFile)
if evernoteActive > 0 then
-- Evernote is running, so insert a task into it directly.
tell application "Evernote"
if theTags is equal to {"null"} then
create note from file theFile notebook {"@ Inbox"}
else
create note from file theFile notebook {"@ Inbox"} tags theTags
end if
end tell
else
-- Evernote isn't running, so use the email method.
if theTags as text is equal to "null" then
set tagString to ""
else
set tagString to "#" & implode(" #", theTags)
end if
set posixTheFile to POSIX path of theFile
do shell script "/usr/local/bin/mutt -s \"" & basename(posixTheFile) & " " & tagString & "\" -a \"" & posixTheFile & "\" -- " & evernoteEmailAddress & " < /dev/null"
end if
end hazelProcessFile
(*
basename(thePath)
Mimics the bash command: returns the filename referenced by thePath
*)
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
(*
getTags(theFile)
Returns a list of the Mavericks (OS X 10.9) tags on theFile
*)
on getTags(theFile)
set tagItems to {}
set userTags to (do shell script "mdls -name kMDItemUserTags " & quoted form of POSIX path of theFile) as text
-- SET UP THE DELIMITER VALUES TO PULL OUT TAG VALUES…
set theDelim to "("
set finalDelim to ")"
-- SAVE THE OLD TEXT ITEM DELIMITERS
set oldDelim to AppleScript's text item delimiters
-- PROCESS THE KEY INTO A LIST OF TAGS
set AppleScript's text item delimiters to theDelim
set userTags to (userTags as text)
set preProc to (every word of userTags) as string
set procItems to words 3 thru end of preProc
set finalLine to last item of procItems
set AppleScript's text item delimiters to finalDelim
set finalProc to first text item of finalLine
set last item of procItems to finalProc
set AppleScript's text item delimiters to oldDelim
-- REMOVE THE COMMAS…
repeat with procItem in procItems
set the_String to procItem
if (the last character of (the_String) is ",") then
set newItem to (text 1 thru ((length of the_String) - 1)) of the_String as string
copy newItem to the end of tagItems
else
set newItem to (text 1 thru (length of the_String)) of the_String as string
copy newItem to the end of tagItems
end if
end repeat
if tagItems is null then
return ""
else
return tagItems
end if
end getTags
(*
implode(delimiter, pieces)
Converts a list into a delimited string
*)
on implode(delimiter, pieces)
local delimiter, pieces, ASTID
set ASTID to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to delimiter
set pieces to "" & pieces
set AppleScript's text item delimiters to ASTID
return pieces --> text
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't implode: " & eMsg number eNum
end try
end implode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment