AppleScript question about #' tags and nested tags

I am trying to come up with a script to create a nested tag from #parent tag/child tag. But I am still appleccript stupid, so forgive me for this next question.

set theResult to extract keywords from record [record] ¬
 barcodes [boolean] ¬
 existing tags [boolean] ¬
 hash tags [boolean] ¬
  1. This was taken from DT3 AS dictionary. How should this read into an actual script where I want to extract hash tags?

  2. When tagging via AppleScript, how can I use one tag as parent and the other one as child?

I had not seen the addendum to Chris’ reply in another thread, so scratch the first part of this question.

set theTags to extract keywords from record theRecord with existing tags and hash tags

This command returns only found & preprocessed tags, therefore it’s not suitable to create nested tags. Subtags can be e.g. created like this:

tell application id "DNtp"
	set theDB to current database
	set theTag to create location "/Tags/ParentTag" in theDB
	set theSubtag to create location "/Tags/ParentTag/Subtag" in theDB
end tell
1 Like

You’re awesome, Chris @cgrunenberg. DT3 is getting dangerously close of becoming a mad mean wikifying machine :laughing::heart_eyes::star_struck:

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.

Just append __Cmd-Shift-L to the filename.

Aha, for some reason I was placing shift before cmd… Thanks again :slight_smile:

EDITED: It didn’t work.

The order shouldn’t matter but this shortcut is actually already used (see Format menu). BTW: Here’s a revised & simplified version of your script as smart rule scripts shouldn’t process the selection. In addition, this script supports deeper nesting like #just/a/test

on performSmartRule(theRecords)
	tell application id "DNtp"
		-- Go through each record
		repeat with theRecord in theRecords
			-- 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 don't 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 od to text item delimiters of AppleScript
			set text item delimiters of AppleScript to return
			set grepResultsList to text items of grepResults
			set text item delimiters of AppleScript to od
			
			repeat with fTag in grepResultsList
				if fTag is not "" then
					set theTag to create location "/Tags/" & fTag in (database of theRecord)
					replicate record theRecord to theTag -- Replicating is tagging (and vice versa)
				end if
			end repeat
		end repeat
	end tell
end performSmartRule
1 Like

Looks much better now. Thanks again :upside_down_face: