Daily journal script

This is a script that I use as my own daily journal. It was written for the most part to help me learn applescript and in particular, scripting DTPO. Feel free to use the script any way you wish.

Edited 12_15_2013 : Added korm’s script for adding a new note in DayOne with a link back to the journal record in DTPO
If you have Day One and the Day One journaling CLI installed, set the appropriate flag to true before running the script.
Edited 12_17_2013: Changed the dayOneFlag to default to false

Here is a screenshot of the output:
2013-12-15 at 8.23 AM.png

(*
		Chuck Lane October 2, 2013
		If the group heirarchy 'Journal/Year/Year-Month' doesn't exist, it's created.
		 Creates file in the form of 'month_day_year_weekday' such as '10_13_2013_Sun'.
		 A header with the date, holidays/birthdays, a random quote, and the current weather and time.
		If the file already exists , then no new file will be created and a new header will be added to the existing file consisting of the current weather and time.
		I store the compiled script titled "Journal___Shift-F1" (There are 3 underscores in the name) in the DTPO /Scripts/Toolbar folder so that I can add it to Devonthink's toolbar. You can also call the script by pressing the 'Shift Fn F1' keys.
		The application 'Calendar' will be launched if it isn't already running.
		Calendar is the biggest slowdown, you can get rid of that part of the code if you don't care about holidays or birthdays being displayed.
		Change the property holidayCalendar to the one you're subscribed to.
		You may have to change the delay value after launching Calendar for your
		own computer.
		You may use this script or any part of it in any way you see fit.
		I've added korm's DayOne script. You must have the DayOne CLI installed for this to work. Set the property 'dayOneFlag' to true if you have DayOne.
	
*)
property blueColor : {0, 0, 30000}
property headerColor : {40000, 20000, 0}
property blackColor : {0, 0, 0}
property dateColor : {30000, 30000, 30000}
property holidayCalendar : "US Holidays"
property birthdayCalendar : "Birthdays"
property j_Prefix : ""
property dayOneFlag : false -- Set this flag to true if you have installed DayOne and the DayOne CLI

global theDate, targetPath, numHeadlines

set theDate to current date
set numHeadlines to 4

-- We need Calendar running so we can check holidays and birthdays
if not (get running of application "Calendar") then
	launch application "Calendar"
	delay 0.5 -- wait for Calendar
end if

on getWeather()
	--Go here to get the parameters for different locations: http://developer.yahoo.com/weather/#req
	tell application id "com.devon-technologies.thinkpro2"
		try
			set theWeather to download markup from "http://weather.yahooapis.com/forecastrss?w=12770553&u=f"
			set des to description of item 1 of (get items of feed theWeather)
			set theText to do shell script "echo " & quoted form of des & " | textutil -stdin -stdout -format html -convert txt -encoding UTF-8" --Convert the html to plain text
			set picURL to (texts (offset of "h" in des) thru (offset of "f" in des)) of des
			set theForecast to paragraph 3 of theText
			
			--Create a folder in the user's documents folder (if it doesn't already exist)
			set folderName to "zTempFolder"
			set folderPath to get (path to documents folder from user domain) as text
			set filePath to (folderPath & folderName & ":")
			tell application "Finder"
				if not (exists folder filePath) then
					make new folder at folder folderPath with properties {name:folderName}
				end if
			end tell
			
			--Download and save the weather icon to the folder we created
			set targetPath to filePath & "TempImage.jpeg"
			do shell script "curl " & picURL & " -o " & quoted form of POSIX path of targetPath
		end try
		return theForecast
	end tell
end getWeather

on getQuote()
	tell application id "com.devon-technologies.thinkpro2"
		try
			set myQuote to ""
			set getSource to download markup from "feed://feeds.feedburner.com/quotationspage/qotd"
			set getFeed to get items of feed getSource
			if items of getFeed is not {} then
				set randItem to some item of getFeed
				set myQuote to description of randItem & return & "=    " & title of randItem & "    =" & return
			end if
		end try
		return myQuote
	end tell
end getQuote

--Get the news headlines
on getNews()
	set myNews to {}
	tell application id "com.devon-technologies.thinkpro2"
		try
			set getNewsSource to download markup from "feed://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml"
			set getNewsFeed to items 1 thru numHeadlines of (get items of feed getNewsSource)
			repeat with theItems in getNewsFeed
				set end of myNews to title of theItems
				set end of myNews to link of theItems
			end repeat
		end try
		return myNews
	end tell
end getNews

on getHoliday() -- Also gets birthdays
	tell application "Calendar"
		try
			set holidayList to {}
			set mydate to theDate
			set time of mydate to 0 -- set time to midnight
			tell calendar holidayCalendar
				set allHolidays to get events whose start date is equal to mydate
				if allHolidays is not {} then
					repeat with theItem in allHolidays
						set end of holidayList to summary of theItem
					end repeat
				end if
			end tell
			tell calendar birthdayCalendar
				set birthdayList to {}
				set allBirthdays to get every event
				if allBirthdays is not {} then
					repeat with theItem in allBirthdays
						set myStart to start date of theItem
						set year of myStart to year of mydate
						if myStart is equal to mydate then
							set end of birthdayList to summary of theItem
						end if
					end repeat
				end if
			end tell
		end try
		return {holidayList, birthdayList}
	end tell
end getHoliday

-- This code snippet is compliments of korm
on makeJournalEntry(j_Content)
	set new_JournalEntry to "echo " & (quoted form of j_Content) & " | /usr/local/bin/dayone  new"
	do shell script new_JournalEntry
end makeJournalEntry

-- TIME FORMAT: Strip out the seconds but keep the AM/PM indicator
set theTime to time string of theDate
if character 5 of theTime is ":" then
	set theTime to (characters 1 through 4 of theTime) & (characters 8 through 10 of theTime) as string
else
	set theTime to (characters 1 through 5 of theTime) & (characters 9 through 11 of theTime) as string
end if

-- MONTH FORMAT
set theMonth to month of theDate as string
set numMonth to (month of theDate as integer) as string
if the (length of numMonth) < 2 then set numMonth to "0" & numMonth

-- DAY FORMAT
set theDay to day of theDate as string
set shortDay to theDay -- shortDay won't have a leading zero
if the (length of theDay) < 2 then set theDay to "0" & theDay
set suffixList to {"st", "nd", "rd"}
set theIndex to last character of theDay as integer
if (theIndex > 0) and (theIndex < 4) and the first character of theDay is not "1" then
	set daySuffix to item theIndex of suffixList
else
	set daySuffix to "th"
end if

-- WEEKDAY FORMAT
set longWeekday to weekday of theDate as string
set shortWeekday to characters 1 thru 3 of longWeekday

-- YEAR FORMAT
set theYear to year of theDate as string
set theYearMonth to theYear & "-" & numMonth

set recordName to numMonth & "_" & theDay & "_" & theYear & "_" & shortWeekday
set newHeading to getWeather() & "   " & theTime & return
set mainHeading to theMonth & space & shortDay & daySuffix & "," & space & longWeekday & return

tell application id "com.devon-technologies.thinkpro2"
	try
		activate
		set myGroup to create location "/Journal/" & "/" & theYear & "/" & theYearMonth
		set label of every child in myGroup to 0
		set root of think window 1 to myGroup
		if not (exists child recordName of myGroup) then
			set myRecord to create record with {name:recordName, rich text:"", type:rtfd, label:5, tags:theYear} in myGroup
			open tab for record myRecord in think window 1
			close first tab in think window 1
			
			show progress indicator "I'll be out to play computer shortly..."
			
			tell text of think window 1
				make paragraph at beginning with properties {alignment:center, font:"Zapfino", size:18, color:dateColor} with data mainHeading
				bold first paragraph
				unbold last character
				set properties of last character to {font:"Avenir", size:14}
				
				--Show any holidays and/or birthdays
				set allEvents to my getHoliday()
				repeat with eachList in allEvents
					if eachList is not {} then
						set theCount to 0
						repeat with theItems in eachList
							set theCount to theCount + 1
							if theCount = 1 then
								set the last character to the last character & theItems
							else
								set the last character to the last character & "     " & theItems
							end if
						end repeat
						set the last character to the last character & return
						set color of last paragraph to blueColor
					end if
				end repeat
				if item 2 of allEvents is not {} then set last character to last character & return
				set color of last character to blackColor
				
				--Show the daily quote
				set myQuote to my getQuote()
				set the last character to the last character & myQuote & return
				set alignment of last character to left
				
				--Show specified number of news headlines
				set myNews to my getNews()
				set the last character to the last character & "HEADLINES:" & return
				bold last paragraph
				unbold last character
				repeat with i from 1 to (count of items of myNews) by 2
					set the last character to the last character & item i of myNews & return
					set the URL of the last paragraph to item (i + 1) of myNews
				end repeat
			end tell
			
			--This code is compliments of korm. It adds a link to a new DayOne note.
			if dayOneFlag is true then
				set d_Default to return & return & return
				set j_Header to "## " & j_Prefix & recordName & return & return
				set d_Prompt to "Notation about " & recordName as string
				set j_Note to "Click link to see note in Devonthink"
				set j_Link to "[See document](" & (the reference URL of myRecord as string) & ")"
				set j_Content to j_Header & j_Note & return & return & j_Link
				my makeJournalEntry(j_Content)
			end if
			
		else -- Record already exists, just add new weather/time header
			set myRecord to child recordName in myGroup
			set label of myRecord to 5
			open tab for record myRecord in think window 1
			close first tab in think window 1
		end if
		
		tell text of think window 1
			set the last character to the last character & return & return
			
			--Insert the weather graphic that we saved
			set theFile to targetPath as alias
			make new attachment at end of paragraphs with properties {file name:theFile}
			
			make new paragraph at end with data (newHeading)
			set properties of last paragraph to {font:"Avenir", size:16, alignment:left, superscript:4, color:headerColor}
			italicize last paragraph
			set properties of last character to {font:"Avenir", size:16, alignment:left, superscript:0, color:blackColor}
			set last character to last character & "■ "
		end tell
		
		hide progress indicator
		
	on error errMsg number errNum
		hide progress indicator
		display alert (localized string "An error occured when adding the note. ") & errMsg
	end try
	
end tell

Just ran across this. Very nice, Chuck! I even learned something from it (still learning after all these years)…

Deleted

Yeah, it gets kind of addictive. :smiley:

One thing you might consider is changing the name of the script to allow it to be run from a hotkey. Just add three underscores and a well-chosen key. (Command will always be assumed if you just enter something like “chucksJournal___F1.scpt”. PS: Function keys work nicely in this case.) Look at the Templates folder for some more examples of the syntax.

Cheers! :^)

Deleted

Is dolly parton still alive? :question:

No, but really, thanks for sharing your work on the forum.

Deleted

Hey why did you delete your script?

I planned on using it to learn apple script just like you did and I think it could be useful to others in the future.

There was a, uh, glitch. The script is back up. Hope it helps.

I love this script … it would play well with DayOne or other journaling app – linking back to DEVONthink.

Thanks! :smiley: