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!
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