AppleScript: convert hashtags to markdown metadata

Summary

This script basically works in a markdown record effectively to move up to three hashtags from the end of the record to the (invisible) metadata at the start of the record. It then deletes the corresponding hashtag(s) at the end of the record.

Why?

When Day One journal entries are exported as markdown tags are not exported and so do not appear in DT. I can manually add the hashtags at the end of any relevant Day One journal entry and those tags will then appear in DT (I have checked DT > Preferences > Import > Convert hashtags to Tags). I dislike having the hashtags display in addition to DT tags. However, if I delete the hashtags the DT tags also disappear. The solution is to nest the hashtags in the metadata at the start of the markdown—with the result DT shows the tags but not the hashtags.

It’s neat—and I’m happy! :grinning:

Yes, I know it’s yet another niche script but I post it only in case any elements of it might at some time assist others.

The AppleScript

(* This script effectively moves up to three Day One hashtags, typed
at the end of a Day One diary entry (each on a separate line), to
a metadata entry at the start of the DEVONThink diary entry record
and deletes the corresponding hashtag(s) from the end of the record.

It is designed to overcome the fact that export of
Day One entries as markdown fails to export hashtags. It
also ensures that the hashtags do not display in unsightly
fashion in the Day One record.

It assumes each diary record is markdown.  *)

-- Set the database we want to use
property pDatabase : "[path to and name of database you want to use]"

tell application id "DNtp"
	try
		set theDatabase to open database pDatabase
		
		-- Test for selection of single markdown record
		set theSelection to the selection
		if (count of theSelection) is 0 then
			error "Please select a single markdown diary record"
		end if
		
		set theRecord to the content record
		-- we are going to check for the existence of a hashtag
		set tagItem to "#"
		set existingMetadata to ""
		
		if type of theRecord is markdown then
			set theText to plain text of theRecord
			set theResult to (get every paragraph of theText)
			-- check for up to 3 hashtags at the end of the record
			repeat with i from -3 to -1
				if (item i of theResult) contains tagItem then
					set taggedItem to (item i of theResult)
					-- construct the metadata entry
					set theMetadata to "tag: " & taggedItem
					-- delete the hashtag from the text and reassemble the text
					set amendedText to my reviseText(theResult, taggedItem)
					-- check if we need to add to existing metadata
					if existingMetadata is "" then
						set existingMetadata to theMetadata
					else
						set existingMetadata to existingMetadata & return & theMetadata
					end if
					-- assemble the metadata and text
					set theText to existingMetadata & return & return & amendedText
					set plain text of theRecord to theText
				else
					error "There is no hashtag at the end of this record"
				end if
			end repeat
		end if
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		
	end try
end tell

on reviseText(textList, hashTag)
	-- textList is already a list of paragraphs (theResult) so we can repeat over each paragraph
	-- we repeat over the list and replace with "missing value" the line that has the passed hashtag
	-- we check for up to 3 hashtags
	
	repeat with i from -3 to -1
		if (item i of textList contains hashTag) then set item i of textList to missing value
	end repeat
	
	-- Coerce the paragraphs which are left to a single text using return delimiters.
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to return
	set newText to textList's text as text
	set AppleScript's text item delimiters to astid
	return newText
end reviseText

Stephen

5 Likes

Nicely done! And you never know who might either need this script or learn something from it :slight_smile:

1 Like

Just wondering why your script doesn’t directly create DT Tags
Is there a benefit to storing the tags in the markdown metadata?

There is a benefit for me because I don’t see the hashtags when in markdown preview mode. In other words, the metadata is invisible in preview mode but the tags are still present (in the metadata) so DT still shows them.

To be clear, it’s purely a visual benefit for someone who likes to keep things neat and tidy!

Stephen

Below is a sample document
DT shows tags without the tags being present (in the metadata)
Screen Shot 2021-08-25 at 07.55.32

Yes, I understand that but you’ll see (the holistic view—in my first post) this is part of import from Day One. I did explain the difficulty in my first post but, in essence:

  • Day One tags are not exported when exporting to markdown
  • If I manually tag in Day One using hashtags I can then use this script to automate hiding of the hashtags in DT but still display the tags.
  • If I simply remove the hashtags in DT the tags disappear from DT.

No doubt there’s a process that will convert the Day One hashtags to DT tags without having the hashtags present (at all) but, for me, this was a reasonably satisfying solution to the problem.

Stephen

I was wondering about that statement
I’m thinking that after import, there is no connection between DT tags and the hashtags

In my experience (and unless I’m doing something bizarre—which, of course, is quite possible :grinning:) that has not proved to be the case: delete hashtags, lose tags—tragedy!

Stephen