Get the name of a tag record using applescript

Hi,

I’ve searched the forum with no luck. I’m trying to get the name out of a record which is a tag (I think) using AppleScript (roughly cut from an applescript, with inspiration from
Make a tag a subtag to another tag via Applescript

The script goes something like this:
– parentTagName and theTagName are strings.
– theRecord is an ordinary record containing a PDF

set theRecordToFindName to “/Tags/” & parentTagName & “/” & theTagName

set recordToFind to get record at (theRecordToFindName) --"/Tags/" & parentTagName & “/” & theTagName)

– display dialog parentTagName

if recordToFind = missing value then

– display dialog "The Tag, " & theTagName & “, could not be found. It doesn’t exist or you mistyped it.”

else

set the end of theListOfChildTags to recordToFind

set theName to name of ( get record recordToFind)

I get the error: " error “DEVONthink 3 drabbades av ett fel: Kan inte hämta record (parent id 298 of database id 8).” number -1728 from record ( parent id 298 of database id 8)" - translating to something like:
DEVONthink 3 got an error: Cannot get record (…)

There is no get record command, as shown in the dictionary…

I’m trying to get the name out of a record which is a tag (I think)

It’c unclear what you’re actually trying to accomplish here. Can you clarify and / or provide screencaps?

Hi!

Sure, yes. I’d want to organise my records based on three group tags, “DocType” (could be ad, receipt, bill, etc), “Source” (essentially whom created the document) and “Regarding”. Only the group tag “Regarding” can contain several tags for each record:

I try to follow a nomenclature when adding documents which is YYYY-MM-DD - DocType, Source, Regarding. This is important to me. The tags are also added correspondingly. Since this to some order is automated using Hazel, some work also needs to be done manually. This is what I want to avoid using the AI of DTP3. I hope to get an easier way automating stuff to OmniFocus, have an easier time finding and culling my documents.

What I want to accomplish is for DTP3 to look at the document contents and suggest a file name, rename the file and add the tags. This would be based on the found records using the compare function.

I’d like for DTP3 to suggest not any tags, but the relevant child tags. I.e. when the new record is matched against documents, I’d want the suggestion for the DocType to come from the child tags of the group tag “DocType”, not the group tags “Regarding” or “Source”. Ideally, for a new record containing a receipt, I hope for one of the suggestions of the “DocType”-tag to be receipt.

For this, I’ve built a handler which find group tags (using capital characters, other tags don’t), findTheTagNames. The handler returns a list of the name of the group tags.

Further, I’ve built a handler which returns a list of tags which are children of the given group tag name.

Hope this makes sense :slight_smile:
Regards,
Björn

– 2019-06-27: Updated, trying to create handler for finding child tags. During testing, I cannot get the name out of the tag (at least what I think is the tag). Posted question on how to do this in the DTP forum.

– 2019-06-24 by Björn Brian

– For finding tag names of a record, based on how the tags of the record is set up. This is useful if the tags are already children of parent tags. E.g. if the record contains the tag “kvitto”, and the tag kvitto is a child of the tag “DocType”, the tag kvitto will be the result of the handler. Handler has to be created, though. This can be used in conjunction with the DTPO test 4 script that is used to compare a given record and suggest tags based on other records in the database. In the case of matching, instead of suggesting any kind of tags of the matched records, using this sketch, tags for e.g. DocType could be suggested instead.

tell application id "DNtp"
	
	set theDatabase to current database
	
	if selection is {} then
		
		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
	
	-- theCycle is defined to show the number of comparisons used to find the nested tags
	
	set theCycle to 0
	
	-- Call function to get list of parent tags and their names. If the searchString is an empty string, it is assumed that the parent tag names consist of at least on upper case character.
	
	set listOfParentTagNames to findTheTagNames(theDatabase, "") of me
	
	-- Create empty lists for children
	
	set listOfChildTagsDocType to {}
	
	set listOfChildTagsSource to {}
	
	set listOfChildTagsRegarding to {}
	
	repeat with theRecord in recordList
		
		repeat with parentTagName in listOfParentTagNames
			
			set theList to findChildTags(theRecord, parentTagName) of me
			
			repeat with childTag in theList
				
				set the end of theList to name of childTag as string
				
			end repeat
			
			display dialog "For the record" & (name of theRecord as string) & parentTagName & "Has the following children" & theList as string
			
		end repeat
		
	end repeat
	
	-- set recordList to {}
	
end tell

-- ( THESE ARE THE USED HANDLERS )

--Handler to find instances of tags in given database with given characters. Returns list a list of listOfParentsTagsNames. It is a list of strings.

on findTheTagNames(theDatabase, searchString)
	
	tell application "DEVONthink 3"
		
		-- Initiate theDatase and searchString
		
		if theDatabase is null then
			
			set theDatabase to current database
			
		else
			
			set theDatabase to theDatabase
			
		end if
		
		if searchString is "" then
			
			-- Define the upper case characters in Swedish, I use upper case for tag names of tags that are parents
			
			set theComparisonCharacters to characters of "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ"
			
		else
			
			set theComparisonCharacters to searchString
			
		end if
		
		-- Create list of all tags
		
		set allTagsOfDatabase to tag group of current database
		
		-- Create empty list for Parents Tag names
		
		set listOfParentsTagsNames to {}
		
		-- Get all parent tags which are defined by containing upper case character
		
		repeat with theTag in allTagsOfDatabase
			
			set theTagName to name of theTag
			
			considering case
				
				repeat with theCharacter in theComparisonCharacters
					
					if theTagName contains theCharacter then
						
						set the end of listOfParentsTagsNames to theTagName
						
						-- If one upper case character was found, disengage repeat loop (otherwise will get severeal instances of the same tag in listOfParentsTagsNames if the tag contains severeal upper case characters which is are not the same character).
						
						exit repeat
						
					end if
					
				end repeat
				
			end considering
			
		end repeat
		
	end tell
	
	return listOfParentsTagsNames
	
end findTheTagNames

on findChildTags(theRecord, parentTagName)
	
	set theListOfChildTags to {}
	
	tell application id "DNtp"
		
		set tagList to tags of theRecord
		
		-- Cycle thru all items of tagList.
		
		repeat with theTag in tagList
			
			-- Get the name of the tag in variable theTagName
			
			set theTagName to theTag as string
			
			-- If parentTagName is not theTagName, go ahead and proceed to find out if the tag is a child to the parent defined in listOfParentTagNames, else get next tag in the list.
			
			if parentTagName is not theTagName then
				
				-- theRecordToFindName is set to each tag with the parent found in parentTagName to find out if it already exists
				
				set theRecordToFindName to "/Tags/" & parentTagName & "/" & theTagName
				
				set recordToFind to get record at (theRecordToFindName) --"/Tags/" & parentTagName & "/" & theTagName)
				
				-- display dialog parentTagName
				
				if recordToFind = missing value then
					
					-- display dialog "The Tag, " & theTagName & ", could not be found. It doesn't exist or you mistyped it."
					
				else
					
					set the end of theListOfChildTags to recordToFind
					
					set theName to name of (get record recordToFind)
					
				end if
				
			end if
			
		end repeat
		
		-- display dialog theCycle
		
		-- set theTag to get record at (theRecordToFindTemp)
		
		return theListOfChildTags
		
	end tell
	
end findChildTags

(*

repeat with theRecord in recordList

-- tagList is a list of tags of the record

set tagList to tags of theRecord

-- Cycle thru all items of tagList.

repeat with theTag in tagList

-- Get the name of the tag in variable theTagName

set theTagName to theTag as string

-- If listOfParentTagNames does not contain theTagName, go ahead and proceed to find out if the tag is a child to the parent defined in listOfParentTagNames, else get next tag in the list.

if listOfParentTagNames does not contain theTagName then

repeat with parentTagName in listOfParentTagNames

-- theRecordToFindName is set to each tag with the parent found in listOfParentTagNames to find out if it already exists

set theRecordToFindName to "/Tags/" & parentTagName & "/" & theTagName

set recordToFind to get record at (theRecordToFindName) --"/Tags/" & parentTagName & "/" & theTagName)

-- display dialog parentTagName

if recordToFind = missing value then

-- display dialog "The Tag, " & theTagName & ", could not be found. It doesn't exist or you mistyped it."

else if parentTagName as string is equal to "DocType" then

set the end of listOfChildTagsDocType to theTagName

else if parentTagName as string is equal to "Source" then

set the end of listOfChildTagsSource to theTagName

else if parentTagName as string is equal to "Regarding" then

set the end of listOfChildTagsRegarding to theTagName

else

display dialog "No tag name added"

end if

set theCycle to theCycle + 1

end repeat

end if

end repeat

end repeat

-- display dialog theCycle

-- set theTag to get record at (theRecordToFindTemp)

display dialog (listOfChildTagsDocType as string) & ":" & (listOfChildTagsSource as string) & ":" & (listOfChildTagsRegarding as string)

*)

Hi @bjorn, perhaps placing your script in a code block will make it easier for others to read it. :wink:
All you have to do is place ``` (triple back ticks) before and after the script.

2 Likes

Thanks for the hint! Looks like someone already helped me out - thank you for that. I’ll remember for coming posts.

1 Like