Trying to determine if a tag has a parent tag

Hi,

I’d appreciate some help with a script I’ve been working on for some time:

What I’m trying to achieve:
I want the handler sortRecordUsingMetadata to sort records into the parent tags DocType, Source and Regarding based on the input. Input arguments are:

  • tagNameToMove of data type string is the name of the tag to be moved, if needed.
  • parentTag of data type string is the name of the parent tag to receive tagNameToMove as a child
  • theRecord of data type record is the record where the tag with name tagNameToMove should be or have been applied.

In order to do so, I want to supply the record to be sorted, i.e. theRecord, which in this case is a document. I want to look at the tags of theRecord and determine which ones are ordinary tags in order not to move parent tags (group tags).

I then want to determine if the tag tagNameToMove is already a child of parentTag.

What my problems are:
I cannot seem to get the parent of aTag as string, with the error: “name of parent of item 1 of {item 2 of every parent of content id 107314 of database id 10, item 3 of every parent of content id 107314 of database id 10, item 4 of every parent of content id 107314 of database id 10}”

What (likely many things) am I missing here?

tell application id "DNtp"
	
	if selection is {} then
		-- if no selection is made, process all records of the database
		set recordList to every record of current database
		-- If a selection is made, process selected records
	else if selection is not {} then
		set recordList to selection as list
	end if
	
	set theRecord to item 1 of recordList
	sortRecordUsingMetadata("villkor", "DocType", theRecord) of me
	
end tell

-- Handler used by sortRecord(theRecord) in order to clean up code and facilitating readability and maintenance within this handler
on sortRecordUsingMetadata(tagNameToMove, parentTag, theRecord)
	(*
		input	tagNameToMove of data type string is the name of the tag to be moved.
				parentTag of data type string is the name of the parent tag to receive tagNameToMove as a child
				theRecord of data type record is the record where the tag with name tagNameToMove should be applied.
		output	none. Handler sorts.
	*)
	tell application id "DNtp"
		-- find the ordinary tags of theRecord (not the folders where theRecord is residing and not a group tag - i.e. not a parent tag)
		-- theTagList is a list of records, which are tags
		set theTagList to {}
		repeat with aTag in parents of theRecord
			if tag type of aTag is ordinary tag then
				set the end of theTagList to aTag
			end if
		end repeat
	end tell
	get theTagList
	
	repeat with aTag in theTagList
		-- Try to find tagNameToMove in theTagList, data type string in the name of aTag
		if name of aTag as string = tagNameToMove then
			set tempTag to item 1 of aTag
			
			-- If name of parent tag of aTag is parentTag, data type string, then the tag does not need to be moved and the handler is done
			if name of parent of aTag as string = parentTag then
				display dialog "The record is already in the right place"
				return
			end if
		else
			-- Move the tag using handler moveTagInDatabase(tagNameToMove, parentTag, theRecord)
			return
		end if
		
		-- Other conditions and commands TBD
		
	end repeat
end sortRecordUsingMetadata

You’re trying to specify a parent outside the tell block for DEVONthink. That can’t work as it’s a property of DEVONthink.

The end of the script would be…

		end repeat
	end tell
end sortRecordUsingMetadata

Also, you can’t specify parent of… since a record can have multiple parents (e.g., in the case of replicants).
You need to specify parent n of…, typically parent 1 unless otherwise required for the situation.

Thank you for the prompt reply, Bluefrog, you’re right, mistake on my behalf. That did it!

You’re welcome. Happy scripting :slight_smile:

Hi,

I’ve found out that I need to remove the tag from the record in case it has an incorrect parent. I know I can set the tags of a record using a list of strings, like so:

tell theRecord to set tags to ("tag1","tag2")

But since I potentially can have the same name for tags with different parents, this seemed uncertain. I instead wanted to dissociate the tag with an incorrect parent from theRecord. I’ve therefore instead tried to tell theRecord to delete its parent aTag using:

tell theRecord to delete parent aTag

But I need to ask for help again, please. What am I doing wrong this time?

Below, the runnable script:

use scripting additions



tell application id "DNtp"
	
	if selection is {} then
		-- if no selection is made, process all records of the database
		set recordList to every record of current database
		-- If a selection is made, process selected records
	else if selection is not {} then
		set recordList to selection as list
	end if
	
	set theRecord to item 1 of recordList
	sortRecordUsingMetadata("villkor", "DocType", theRecord) of me
	
end tell

-- Handler used by sortRecord(theRecord) in order to clean up code and facilitating readability and maintenance within this handler
on sortRecordUsingMetadata(tagNameToMove, parentTag, theRecord)
	(*
		input	tagNameToMove of data type string is the name of the tag to be moved.
				parentTag of data type string is the name of the parent tag to receive tagNameToMove as a child
				theRecord of data type record is the record where the tag with name tagNameToMove should be applied. (Not working and non-working code removed 2020-03-21)
		output	none. Handler sorts.
	*)
	tell application id "DNtp"
		-- find the ordinary tags of theRecord (not the folders where theRecord is residing and not a group tag - i.e. not a parent tag)
		-- theTagList is a list of records, which are tags
		set theTagList to {}
		
		repeat with aTag in parents of theRecord
			if tag type of aTag is ordinary tag then
				set the end of theTagList to aTag
			end if
		end repeat
		
		get theTagList
		
		repeat with aTag in theTagList
			-- Try to find tagNameToMove in theTagList, data type string in the name of aTag
			if name of aTag as string = tagNameToMove then
				
				-- If name of parent tag of aTag is parentTag, data type string, then the tag does not need to be moved and the handler is done
				if name of parent 1 of aTag as string = parentTag then
					-- The tag tagNameToMove has the correct parent. It does not have to be moved.
					display dialog "The record is already in the right place"
					return
				else
					-- The parent of the tag is incorrect - remove the tag from the record
					tell theRecord to delete parent aTag
					return
				end if
			else
				-- 
			end if
		end repeat
	end tell
end sortRecordUsingMetadata

I originally posted this in another thread by mistake.

For removing a record from a tag, see this post?

I think this line of code is the key? Please test it “carefully”. “from” is very important coz it only remove theRecord under aTag.

Instead of

tell theRecord to delete parent aTag 

use

move record theRecord from aTag to the trash group of database of theRecord

You may also want to add a log message to the log, in case you detach the wrong record:

e.g.

log message (name of theRecord) & " is removed from " & (name of aTag)
2 Likes

Perhaps you can consider to replace this block

		set theTagList to {}
		
		repeat with aTag in parents of theRecord
			if tag type of aTag is ordinary tag then
				set the end of theTagList to aTag
			end if
		end repeat

with this?

set theTagList to parents of theRecord whose tag type is ordinary tag
1 Like

Thank you ngan, this works very good!

Thanks again, ngan, also works very well. I now remember reading that post. I fail to understand why my approach was unsuccessful, but regardless, this problem is now solved!

Thanks again!

/B

1 Like