Distinguishing tags from (ordinary) groups in scripts

I’m trying to figure out the correct way to distinguish between tags and groups in a script. Given the following AppleScript code,

tell application id "DNtp"
	repeat with rec in (selected records)
		set rtype to (type of rec) as string
		log "(type of rec) is " & rtype
	end repeat
end tell

the following are the results when I run the script after selecting certain kinds of entities in a database:

  • if I select a group, the value of type of rec is group
  • if I select a tag within the Tags hierarchy in a database, the value is also group.

Searching for discussions in this forum, I found a previous posting that provides a clue: there is another property, tag type, that is different for a tag than for a group. And indeed, if I test that using this,

tell application id "DNtp"
	repeat with rec in (selected records)
		set rtype to (type of rec) as string
		log "(type of rec) is " & rtype
		set ttype to tag type of rec
		log "(tag type of rec) is " & ttype
	end repeat
end tell

then the “tag type” value:

  • is “no tag” for a group
  • is “ordinary tag” for a tag.

So my question is: is testing both type and tag type together the appropriate way to distinguish between a group and a tag? Or is there another, more direct way?

(There is a nuance if I select the top level of the tags hierarchy – i.e., the group called “Tags” at the top of a database – then the results for the second script are “group” and “no tag”, which kind of makes sense, but is also slightly surprising at first. Luckily for my purposes this ambiguity doesn’t matter.)

1 Like
tell application id "DNtp"
	set sel to (selected record 1)
	(*
	Tag: {group, ordinary tag}
	Group Tag: {group, group tag}
	Group: {group, no tag}
	Document: {x, no tag}
	*)
	
	return tag type of sel is in {group tag, ordinary tag}
	-- If true, then it's a tag of some type
	-- If false, it's a group or document
end tell

What are you trying to do?

PS: @cgrunenberg. Not sure if you know the reason for the difference shown below…

tell application id "DNtp"
	set sel to (selected record 1)
     --This resolves as false when a group is selected. Not sure why.
	log{type, tag type} of sel = {group, no tag}
	
     --This works when a group is selected…
	set {type:t, tag type:tt} to sel
	return {t, tt} is {group, no tag}
end tell
1 Like