Skip to content

Instantly share code, notes, and snippets.

@aeisenberg
Last active December 10, 2015 14:48
Show Gist options
  • Save aeisenberg/4450073 to your computer and use it in GitHub Desktop.
Save aeisenberg/4450073 to your computer and use it in GitHub Desktop.
Helping awsamuel out with some applescript and evernote integration.
global cleanText
-- set all variables to global [not sure whether this is needed, but it made the script run]
global noteList
global tempTags
global keepPhrase
global messageText
global newText
global theEverBody
global thisNote
global fileText
global thisText
global listOfBadChars
global t
-- ADE this is the only variable that really should be global. All others should be locally defined and used
global keepPhrase
set keepPhrase to "IDEA"
set theText to getNoteText()
log "theText"
log theText
set fileText to removeMarkup(theText)
log "fileText"
log fileText
set messageText to deleteLinesFromText(fileText, keepPhrase)
log "messageText"
log messageText
set cleanText to removeIdeas(messageText)
log "cleanText"
log cleanText
set theEverBody to assignEvernoteVariables(cleanText)
log "theEverBody"
log theEverBody
createNewEvernotes(theEverBody)
on getNoteText()
-- get body text of originating Evernote [works fine]
tell application "Evernote"
set the_notes to the selection
set thisNote to item 1 of the_notes
set t to the ENML content of thisNote
-- ADE don't need to use the clipboard
-- set the clipboard to thisText
-- get the clipboard
end tell
return t
end getNoteText
-- remove markup -- this works fine but leaves a few html entities as entities, which is a problem
-- remove markup from http://www.macosxautomation.com/applescript/sbrt/sbrt-04.html
on removeMarkup(text)
set copy_flag to true
set the clean_text to ""
repeat with this_char in text
set this_char to the contents of this_char
if this_char is "<" then
set the copy_flag to false
else if this_char is ">" then
set the copy_flag to true
else if the copy_flag is true then
set the clean_text to the clean_text & this_char as string
end if
end repeat
return clean_text
end removeMarkup
-- this deletes any text paragraphs that don't include the word IDEA
--deleteLinesFromText adapted from code by hank of hamsoftengineering.com as found on http://macscripter.net/viewtopic.php?id=37830
on deleteLinesFromText(theText, keepPhrase)
set newText to ""
try
-- here's how you can keep all lines of text from fileText that contain the keepPhrase.
-- first turn the text into a list so you can repeat over each line of text
set textList to paragraphs of theText
-- now repeat over the list and ignore lines that have the keepPhrase
repeat with i from 1 to count of textList
set thisLine to item i of textList
if thisLine contains keepPhrase then
set newText to newText & thisLine & return
end if
end repeat
if newText is "" then set newText to text 1 thru -2 of newText
on error
set newText to theText
end try
-- ADE Don't think we need these
-- set delimiter to messageText
-- set str to delimiter
return newText
end deleteLinesFromText
-- this deletes the word "IDEA" from the remaining paragraphs, so I'm left with the idea but not the IDEA label
on removeIdeas(messageText)
set ideaText to messageText
set AppleScript's text item delimiters to keepPhrase
set the item_list to every text item of ideaText
set AppleScript's text item delimiters to " "
set thisText to the item_list as string
set AppleScript's text item delimiters to ""
set cleanText to thisText
return cleanText
end removeIdeas
-- this gets the metadata from the originating Evernote note so it can be attached to the new notes I'm creating
on assignEvernoteVariables(cleanText)
set noteList to paragraphs of cleanText
tell application "Evernote"
set the_notes to the selection
set thisNote to item 1 of the_notes
set tempTitle to the title of thisNote
set tempTags to tags of thisNote
set tempLink to note link of thisNote
end tell
-- this concatenates the original note title with the original note link so they can later be added to the new note as its body
set theEverBody to tempTitle & return & tempLink
return theEverBody
end assignEvernoteVariables
-- this creates a new Evernote note with the idea paragraph as the title for the new note, the tags from the original note as the tags, and the original note title and note link as the body of the new note
on createNewEvernotes(theEverBody)
repeat with currentNote in noteList
set NoteTitle to currentNote
tell application "Evernote"
create note with text theEverBody title NoteTitle tags tempTags
end tell
end repeat
end createNewEvernotes
@aeisenberg
Copy link
Author

I'm not going to be very practical

Here are some comments:

  1. One of the benefits of applescript is that it reads very much like English. You should try to take advantage of this. So, you should be thinking of your script as a series of steps. Each step should go in its own funciton.
  2. Try to avoid globals. They are evil. Better to be passing arguments to functions and returning values from them.
  3. Try not to mix camelCase names with under_score names. I prefer camelCase, but each language and community does things differently. The important thing is to use one and stick with it.

@aeisenberg
Copy link
Author

I made some changes according to what I said above. The script is a little cleaner, but can still be made better. Unfortunately, it's not quite working yet. But, I think it's closer.

You can click on the revisions tab to see the changes I made.

Feel free to ask any questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment