Automate Evernote using AppleScript

This is just a sample of how to create pages in Evernote using AppleScript.

Portions of AppleScript code were borrowed from posts on http://veritrope.com, thanks to them for that help!


global sundayDate
global weeklyItemsObject, weeklyItemsNoteLink, dailyNoteObject, dailyNoteLinksArray

-- Settings to make fonts match in URLs
property gStyleLink : "color:green"
property gstrFont : "font-family:Helvetica Neue;"
property gstrLinkFontSize : "font-size:14px;"

on setUpDateValues()
	set currentDate to (current date)
	set weekdayNum to weekday of currentDate as integer
	set sundayDate to (currentDate - ((weekdayNum - 1) * days))
	log sundayDate
end setUpDateValues

on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to theSearchString
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to theReplacementString
	set theText to theTextItems as string
	set AppleScript's text item delimiters to ""
	return theText
end findAndReplaceInText

on createWeeklyItemsNote()
	-- From http://veritrope.com/code/evernote-new-note-based-on-template/
	tell application "Evernote"
		set {year:y, month:m, day:d} to (sundayDate)

		-- Create the weekly goals sheet
		set new_note_title to "Weekly items (" & y & "-" & (m as integer) & "-" & d & ")" as string
		set new_note_date to (current date)


		-- Set up some variables
		set notebook_name to "Log"
		set template_tag to "template"
		set search_title to "WEEKLY_ITEMS"
		set tag1 to tag "weekly items"

		-- Find the note
		set template_list to find notes "notebook:\"" & notebook_name & "\" intitle:\"" & search_title & "\""

		-- Get the first found item
		if (count of template_list) is greater than 0 then
			set template_note to item 1 of template_list
			set template_content to HTML content of template_note

			-- Create the note
			set note1 to create note title new_note_title with html template_content created new_note_date notebook notebook_name
			assign tag1 to note1

			-- Save the Weekly items note instance
			set weeklyItemsObject to note1

			-- Present the window
			open note window with note1

			-- Save a link to the note
			set noteURL to note link of note1
			set noteName to title of note1
			set strHTMLLink to "<a href=\"" & noteURL & "\" style=\"" & gStyleLink & "\">" & noteName & "<a>"
			set strHTMLLink to "<span style=\"" & gstrFont & gstrLinkFontSize & "\">" & strHTMLLink & " </span>"
			set weeklyItemsNoteLink to strHTMLLink

			-- This is how we get it to the clipboard
			set lstrCMD to "echo " & quoted form of strHTMLLink & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
			do shell script lstrCMD

		else
			display dialog "Could not find template"
			error number -128
		end if

	end tell
end createWeeklyItemsNote

on createDailyLogNote(incrementBy)

	tell application "Evernote"
		-- Create the date and title for new note entry
		-- First, extract date elements from current date
		set {year:y, month:m, day:d} to (sundayDate + ((incrementBy - 1) * days))

		-- Create the title string
		set new_note_title to "Work log (" & y & "-" & (m as integer) & "-" & d & ")" as string
		-- Set the note date to the current date
		set new_note_date to (current date)


		-- Set up some variables
		set notebook_name to "Log"
		set search_title to "DAILY_LOG"
		-- set tag1 to tag "plex" --a tag to assign to your new note--

		-- Find the note we need, which is actually not a template but an actual note that we are duplicating
		set template_list to find notes "notebook:\"" & notebook_name & "\" intitle:\"" & search_title & "\""

		-- Get the first found item
		set template_note to item 1 of template_list

		-- Get the note content
		set template_content to HTML content of template_note

		-- Set the parent weekly goal link
		set template_content to findAndReplaceInText(template_content, "PARENT_WEEKLY_ITEMS_PAGE", weeklyItemsNoteLink) of me

		-- Create the note
		set note1 to create note title new_note_title with html template_content created new_note_date notebook notebook_name
		--assign tag1 to note1

		set dailyNoteObject to note1

		-- Save a link to the note
		set noteURL to note link of note1
		set noteName to title of note1
		set strHTMLLink to "<a href=\"" & noteURL & "\" style=\"" & gStyleLink & "\">" & noteName & "<a>"
		set strHTMLLink to "<span style=\"" & gstrFont & gstrLinkFontSize & "\">" & strHTMLLink & " </span>"

		set item incrementBy of dailyNoteLinksArray to strHTMLLink

		-- Not sure if this is necessary Open the note in its own editor window
		-- open note window with note1

	end tell
end createDailyLogNote

on updateWeeklyGoalDailyLink(index)
	tell application "Evernote"
		open note window with weeklyItemsObject
		set template_content to HTML content of weeklyItemsObject

		set searchText to "PLACEHOLDER" & index
		set noteLinkUrl to item index of dailyNoteLinksArray
		set template_content to findAndReplaceInText(template_content, searchText, noteLinkUrl) of me
		set HTML content of weeklyItemsObject to template_content
	end tell
end updateWeeklyGoalDailyLink

-- From http://veritrope.com/code/evernote-new-note-based-on-template/
tell application "Evernote"

	set dailyNoteLinksArray to {"", "", "", "", "", "", ""}
	setUpDateValues() of me

	createWeeklyItemsNote() of me

	set dayNumber to 1
	repeat 7 times
		createDailyLogNote(dayNumber) of me
		updateWeeklyGoalDailyLink(dayNumber) of me

		set dayNumber to (dayNumber + 1)
	end repeat


end tell

tell application "System Events" to set frontmost of process "Evernote" to true

Leave a Reply

Your email address will not be published. Required fields are marked *