Script for Day One

I’ve come to despise the Day One diary app, but I still have a lot of old data in it.

It supports linking but no Apple Events. Unfortunately I’m bad at UI scripting. Help?

Screenshot 2020-02-23 at 12.35.25

Any way we can hook to entries in the Day One app?

From a dev here:

Day one does not have an AppleScript dictionary.

Though you can copy the entry url from context menu ->share->copy entry url, it is not easy and unreliable to do so using UI scripting.
Ulysses (which also does not have an API) does give us a shortcut to copy URLs. I am not sure Day One has such thing. They are quite similar, looks to me.

It would be good to request an API of the Day One dev: https://hookproductivity.com/help/integration/other-app-developers/
( AppleScript / JavaScript, command line, whatever … we’re not fussy :blush: ).

We/one could conceivably provide a Keyboard Maestro Plug In Action to bridge Day One and Hook.

Reply from Day One, they sent me this link

https://help.dayoneapp.com/en/articles/470001-day-one-url-scheme

1 Like

I ended up doing something like that. It’s not packaged as a Plug-In, just a few KM macros and some script elements.

My solution is here: https://github.com/kraigparkinson/HookPlugins/tree/master/Day%20One

I’ve implemented Get Name, Get Address, and New Item.

The ‘Get Name’ AppleScript invokes the Get Name macro, which acts as a UI driver to get to the JSON export of the entry, opens it in TextEdit (because there’s no copy to clipboard option), reads the contents, finds the first line of the text as the title, closes the TextEdit window (but doesn’t quit it), and returns the name. This uses the JSON Helper library.

The ‘Get Address’ AppleScript invokes the Get Address macro, which acts as a UI driver to get to the Copy Entry URL menu item and returns it.

Both of the above rely on a third macro, Trigger Context Menu, that at the moment only works on the main window. (With a little work, I could get it to work on dedicated entry windows, but that’s not a priority for the moment.) It’s the one that gets the context menu to pop up in the first place to do the rest of the UI driving.

I tried to do both of the above with just AppleScript, but the context menu wouldn’t respond to any actions I threw at it. If someone figures that out, I’d appreciate it a pull request or a comment.

Finally, the New Item script will go ahead and create an entry by calling a shell script to invoke the Day One 2.0 CLI and then it will parse the response via RegEx to get the UUID using the Text script library. Then it converts it into a URL. The Day One 2.0 CLI needs to be installed per the instructions in the link, and the Text script needs to be installed in ~/Library/Script Libraries or /Library/Script Libraries as usual.

No warranty on this, and if it breaks something for you, please don’t hold me responsible. But I’ve been using it for a couple of days now, and other than it being slow and cringeworthy, it does the trick for me.

1 Like

Does that mean that I have to install or subscribe to subscribe keyboard maestro

Welcome to the Hook Productivity Forum , @Rigorjunky , and thanks for sharing!

Based on @Rigorjunky’s investigation, it does not look like Day One is a good candidate for UI scripting. Hence for now, yes.

Really best for the Day One dev to provide some automation that users / Hook can latch onto for linking.

Having said that, something other insights might come of it.

1 Like

Is there no way you can contact them directly?

Yeah, the Keyboard Maestro macros require Keyboard Maestro to execute them. There might be another way, but it’s not a priority for me to investigate, because I already have (and love!) Keyboard Maestro. :smile: (I highly recommend Keyboard Maestro; it really does help elevate the user experience on macOS.)

Also, I’ve contacted Day One a few times in the past couple of years via their support channels re: better automation support via AppleScript and JavaScript (besides Hook), and I get the same responses, which is effectively a combination of “Read the Manual” (RTM) and “use the URLs”. :wink:

1 Like

Thanks for the update and the help. I’ll definitely check it out

FYI, I shared my AppleScript code that’s not responding as expected with the Day One support team. After some discussion to clarify the behavior, they’ve logged the issue to fix (no ETA) and have sent back code that should work once that’s implemented.

I’ll update this thread when I can confirm the fix. :slight_smile:

1 Like

Cool. Please do update and thanks for this message

Day One 5.0 was released today, and I can confirm that the requested fix to their UI was completed for the main screen. It still doesn’t work for me for pop-out entries, but at least it works for the main screen. :slight_smile:

I’ve updated my script to remove the need for the Keyboard Maestro macro.

Also of note, on my computer it took a moment to involve the initial Entry Menu, then upwards of 10 seconds (gasp!) to invoke the Share menu item, then the Copy Entry URL came quickly over that. So best to have patience if you’re going to use this.

If anyone can suggest some speed optimizations or point me toward AppleScript to make a progress bar or something that I could insert during this delay, I’d be happy to add it. I’ve tried a variation of the script whereby I refer to the items by index instead of name, and I don’t get any benefit from that.

And with no more ado, the following is my current script:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

on openItem()
	
end openItem

on getName()
	tell application "System Events"
		tell process "Day One"
			set entryText to value of text area 1 of scroll area 1 of group 1 of splitter group 1 of window 1
			get first paragraph of entryText
		end tell
	end tell
end getName

on getAddress()
	set the clipboard to ""
	delay 0.05
	
	tell application "System Events"
		tell process "Day One"
			set entryPane to group 1 of splitter group 1 of window 1
			set theEntryMenuButton to (first button where its accessibility description = "Entry Menu") of entryPane
			click theEntryMenuButton
			
			delay 0.05
			set entryMenu to menu 1 of theEntryMenuButton
			set shareMenu to menu item "Share" of entryMenu
			click shareMenu
			
			delay 0.05
			click menu item "Copy Entry URL" of menu 1 of shareMenu
		end tell
		
		repeat 50 times -- poll clipboard for ~2.5 seconds
			try
				if (the clipboard) is not equal to "" then
					exit repeat
				end if
			end try
			delay 0.05
		end repeat
		
		set theAddr to (the clipboard) as text
		
	end tell
	
	return theAddr
end getAddress

-- Relies on two items: 
-- 		1. Text script library, from https://github.com/mattneub/applescript-stdlib, installed in ~/Script Libraries
--		2. Day One 2.0 CLI, from https://help.dayoneapp.com/en/articles/435871-command-line-interface-cli
on newItem()
	set entryText to "$title"
	set theResult to do shell script "/usr/local/bin/dayone2 new \"" & entryText & "\""
	set theRegex to "(?:(?:.*)Created new entry with uuid: )([\\d|\\w]{32})"
	tell script "Text" to set theResult to (search text theResult for theRegex using pattern matching)
	set theUUID to foundText of first item of foundGroups of first item of theResult
	set theEntryAddress to "dayone2://view?entryId=" & theUUID
	get theEntryAddress
end newItem
1 Like

Where do I copy and paste the above?

The Day One application in the Hook Preferences is grayed out. I deleted it and added it multiple times, but it still stays Greyed