Help with a way (script?) to copy DT tags to first line of md file?

Good morning all :slight_smile:
first of all thanks for your super helpful comments! this inspired me to dive into Applescript (and to be honest coding in generalā€¦) for the first time in my life and it feels like i had done some baby steps advancements :slight_smile:

this is the current state of my script (spoilers: the code is probably horrific since i donā€™t really know what im doing: )) :slight_smile:

tell application id "DNtp"
	repeat with theRecord in (selection as list)
		if type of theRecord is markdown then
			set theText to plain text of theRecord
			if theText starts with "tag:" then
				set theText to do shell script "sed -e '1,1d' <<<" & quoted form of theText
			else
				set theTags to ""
				
				
				
				set theTagList to tags of theRecord
				repeat with theTag in theTagList
					if theTags is not "" then set theTags to theTags & ", "
					set theTags to theTags & "#" & theTag
				end repeat
				
				set theRating to rating of theRecord
				if theRating is not 0 then
					if theTags is not "" then set theTags to theTags & ", "
					set theTags to theTags & "#" & (theRating as string) & "star"
				end if
				
				set theLabel to label of theRecord
				if theLabel is not 0 then
					if theTags is not "" then set theTags to theTags & ", "
					set theTags to theTags & "#" & (theLabel as string)
				end if
				
				
				if theTags is not "" then
					set theText to "tag: " & theTags & return & return & theText
					set plain text of theRecord to theText
				end if
			end if
		end if
	end repeat
end tell

so the key areas that still donā€™t work

  1. i still cant get the script to overrun/delete the first line if it starts with tags:.
    i tried using this if else statement but it dosent seem to do anything
if theText starts with "tag:" then
				set theText to do shell script "sed -e '1,1d' <<<" & quoted form of theText
			else
				set theTags to ""
				
  1. for the cables i only get a 0 - 7 index. i read in the Devonthink library and i cant find how to translate the number to an actual label (TODO, DONE etc)

thx so much!

Z

I think the delete is working ok
The problem is ā€œset plain text of theRecord to theTextā€ is not executed
Itā€™s buried in the else statements

Remove the else, end if

if theText starts with "tag:" then set theText to do shell script "sed -e '1,1d' <<<" & quoted form of theText
set theTags to ""
...

thx again!

so this is what i ended up with

tell application id "DNtp"
	repeat with theRecord in (selection as list)
		if type of theRecord is markdown then
			set theText to plain text of theRecord
			if theText starts with "tag:" then
				set theText to do shell script "sed -e '1,1d' <<<" & quoted form of theText
				set theTags to ""

this for some reason runs the first time and creates the tags line but deletes everything else apart from the tags line the 2nd run :laughing: . any clue how i screwed this up?

@DTLow

haha found another script of yours on the forum to delete lines based on strings so just used this to get rid of all ā€œtags:ā€ linesā€¦epic :slight_smile:

so i guess now im left with the label issue. any now knows how to add them as tags (but not numeric but actual text?)

Thx

Z

Hi all again!

just an update in case anyone wants to use this, i spend hours (but had lots of fun!) based on the incredible generous help go the community here (thx @DTLow and @chrillek and @cgrunenberg)!

this is the final version i cam up with which seems to do what i want

tell application id "DNtp"
	repeat with theRecord in (selection as list)
		if type of theRecord is markdown then
			set theText to plain text of theRecord
			if theText does not start with "tag:" then
				set theTags to ""
				
				set theTagList to tags of theRecord
				repeat with theTag in theTagList
					if theTags is not "" then set theTags to theTags & ", "
					set theTags to theTags & " " & theTag
				end repeat
				
				set theLabel to label of theRecord
				set theRating to rating of theRecord
				
				
				if theTags is not "" then
					set theText to "---" & return & "tags: [" & theTags & "]" & return & "priority: " & theLabel & return & "rating: " & theRating & return & "---" & return & return & theText
					set plain text of theRecord to theText
					
				end if
			end if
		end if
	end repeat
end tell

still have an issue of getting rid of pre existing tag lines but will open a separate thread for that

thx guys!!

Z

Thatā€™s part of the beauty of tackling automation on your own! :slight_smile:

Just interested in the purpose behind this (copying tags)
Iā€™m comfortable the backups already include tag info,
and impressed that DT includes tags/folders when exporting notes to OS files

I use a script to add the tags to the note title as keywords to provide context

Hi @DTLow

sure, i want to have access to the files in obsidian and want the tag, rating and labels to appear there in proper format

thx for all your help!!

Z