You’re awesome, Chris @cgrunenberg. DT3 is getting dangerously close of becoming a mad mean wikifying machine
Easy to create new md files from selection
Auto nested tags via smart rule
on performSmartRule(theRecords)
tell application id "DNtp"
-- Go through each record
set selectedItems to selection
repeat with theRecord in selectedItems
-- Get the text of the record (assuming text/markdown file)
set _text to plain text of theRecord
-- Use shell script to extract text after character "#", ending with a space/new-line etc
-- UPDATED search line v2, take care of hashtags in web links, we dont want their # in the link as tags..
set grepResults to do shell script "grep -E -o " & quote & "(\\s|^)#\\S*" & quote & " <<< " & quoted form of _text & ¬
"| sed -E " & quote & "s/#//g" & quote
-- Clear the current records tag list
set tags of theRecord to {}
-- transform the output to a list
set grepResultsList to paragraphs of grepResults
-- Add found tags one by one
repeat with fTag in grepResultsList
set fTagString to fTag as string
-- If the tag is an empty string (likely a # without text afterwards, like markdown header, skip it)
if fTagString is not "" then
if fTagString does not contain "/" then
-- Add this tag to the records tag list
set tags of theRecord to tags of theRecord & fTagString
end if
end if
if fTagString contains "/" then
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set theTags to text items of fTagString
set AppleScript's text item delimiters to od
set theTag1 to the item 1 of theTags
set theTag2 to the item 2 of theTags
set theDB to current database
set theTag1 to create location "/Tags/" & theTag1 & "/" & theTag2 in theDB
set tags of theRecord to tags of theRecord & theTag2
end if
end repeat
end repeat
end tell
end performSmartRule
This script from the other thread and added the ability to created nested tags with #parent/child
. Thanks for sharing the script, @Magicnemo.
In case anyone else is reading this: some improvements is still needed for allowing spaces in tags and nesting with more levels (currently only parent/child work).
Obsolete tags automatically deleted
I am also using the filter obsolete tags smart rule.
Last, but not least: keyboard navigation to other md files
tell application "System Events" to set frontApp to name of first process whose frontmost is true
tell application "System Events"
tell application process frontApp
set _selection to value of attribute "AXFocusedUIElement"
tell _selection to perform action "AXShowMenu"
end tell
end tell
This was from a Alfred workflow by Vítor Galvão.
@cgrunenberg, if I want the shortcut for this to be ⇧⌘L, how must ⇧ appear in the file name for this to work? Sure I can use MacOS to set it up, but I am curious about using this method you suggested in the other thread.