How to assign nested tags? [AppleScript]


I’ve two tags with the same name but in different groups:


How do I assign specific tags from AppleScript? The tags property is just flat. I thougt of something like this:

tell (first child of it whose name begins with pdfName)
    set tags to tags of it & "/Software/Test"
end tell

Are the tags located in the Tags group? Then something like this should work:

set theTag to get record at "/Tags/Hardware/Test"
set tags of theRecord to tags of theRecord & theTag

Thanks for the fast reply. It does not work. I get this error:

Can’t make «class DTpr» id 1232208 of «class DTkb» id 2 of application “DEVONthink 3” into type vector.

My (test) code looks like this:

tell application id "DNtp"
	tell database "MYDB"
		set theRecord to (first child of record "Mags")
		--return theRecord
		set theTag to get record at "/Tags/Software/Test"
            --return theTag
		set tags of theRecord to tags of theRecord & theTag
	end tell
end tell

The error happens on the line “set tags …”

Note this kind of redundancy of tags - and our recommendation to not do this - is discussed in the documentation - Help > Documentation > Getting Started > Tagging > Nested Tags.

The joys of AppleScript :slight_smile: But this should work:

tell application id "DNtp"
	repeat with theRecord in (selection as list)
		-- Works even if the nested tag doesn't exist yet
		set theTag to create location "/Tags/ParentTag/SubTag" in (database of theRecord)
		replicate record theRecord to theTag
	end repeat
end tell
1 Like

Thanks for the note. I didn’t know. The hierarchy is not by myself and I can’t change it.

But I found a solution for the error message:

set tags of theRecord to tags of theRecord & {theTag}

I’ve to enclose theTag with braces.

And another approach just for fun…

tell application id "DNtp"
	set sel to (item 1 of (selection as list))
	set tags of sel to tags of sel & {child "02" of child "123" of tags group of current database}
end tell


(And yes, Tags must be presented as a list, per the dictionary.)