Skip to content

Instantly share code, notes, and snippets.

@mandaris
Created April 16, 2015 13:50
Show Gist options
  • Save mandaris/1dc1293249f8fd944a18 to your computer and use it in GitHub Desktop.
Save mandaris/1dc1293249f8fd944a18 to your computer and use it in GitHub Desktop.
Applescript to take an email message from Mail.app and place it into DayOne using the CLI
(* This is a near copy of the script found on Veritrope.com.
I took the example template for handling mail messages and the
Export Evernote Items to DayOne and combined them for my own use.
// SUPPORT VERITROPE!
If this AppleScript was useful to you, please take a second to show your love here:
http://veritrope.com/support
// SCRIPT INFORMATION AND UPDATE PAGE
http://veritrope.com/code/export-evernote-items-to-day-one
// REQUIREMENTS
** THIS SCRIPT REQUIRES YOU TO DOWNLOAD THE DAYONE COMMAND LINE APP:
http://dayoneapp.com/tools/cli-man/
*)
(*
======================================
// Mail PROPERTIES (USE CAUTION WHEN CHANGING)
======================================
*)
property journalHeader : "ON"
property journalTitle : ""
property journalCreated : ""
property journalMessage : ""
(*
======================================
// MAIN PROGRAM
======================================
*)
using terms from application "Mail"
on perform mail action with messages these_messages for rule this_rule
tell application "Mail"
set the message_count to the count of these_messages
repeat with i from 1 to the message_count
set this_message to item i of these_messages
-- GET SUBJECT OF MESSAGE
try
set journalTitle to (subject of this_message) as Unicode text
if journalTitle is "" then set journalHeader to "OFF"
on error
set journalTitle to "NO SUBJECT"
end try
-- GET CONTENT OF MESSAGE
try
set rawMessage to (every character of content of this_message) as Unicode text
if rawMessage is in {"", "?"} then error
on error error_message
set rawMessage to "NO CONTENT"
end try
-- Convert message to plain text
try
set journalMessage to convert_Plaintext(rawMessage)
on error error_message
set journalMessage to rawMessage
end try
-- GET DATE OF MESSAGE
try
set journalCreated to (date sent of this_message) as Unicode text
on error
set journalCreated to date string of (get current date)
end try
my make_DayOne(journalTitle, journalCreated, journalMessage)
set journalHeader to "ON"
end repeat
end tell
end perform mail action with messages
end using terms from
(*
======================================
// UTILITY SUBROUTINES
======================================
*)
--CONVERT HTML TO PLAIN TEXT
on convert_Plaintext(rawMessage)
set shell_Text to "echo " & (quoted form of rawMessage) & " | textutil -stdin -convert txt -stdout"
set note_Text to do shell script shell_Text
return note_Text
end convert_Plaintext
--CONVERT DATE TO PLAIN TEXT STRING
on convert_Date(noteCreated)
set AppleScript's text item delimiters to ""
set m to ((month of noteCreated) * 1)
set d to (day of noteCreated)
set y to (year of noteCreated)
set t to (time string of noteCreated)
set date_String to (m & "/" & d & "/" & y & " " & t) as string
return date_String
end convert_Date
(*
======================================
// MAIN HANDLER SUBROUTINES
======================================
*)
--MAKE ITEM IN DAY ONE
on make_DayOne(journalTitle, journalCreated, journalMessage)
if journalHeader is "ON" then
--ADD A "HEADER" AND MAKE THE ENTRY
set note_Text to (journalTitle & return & return & journalMessage)
set new_DayOne to "echo " & (quoted form of note_Text) & " | '/usr/local/bin/dayone' -d=\"" & journalCreated & "\" new"
do shell script new_DayOne
else
--MAKE THE ENTRY WITH NO "HEADER"
set new_DayOne to "echo " & (quoted form of journalMessage) & " | /usr/local/bin/dayone -d=\"" & journalCreated & "\" new"
do shell script new_DayOne
end if
end make_DayOne
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment