Remove annotation (link) of a record via Applescript?

I’m trying to remove an annotation for a record. The record shows the “flower” icon singnalling that an annotation exists while it actually doesn’t (it might have removed in a different way). I know the work around is to add a default annotation and remove it directly afterwards using the “Remove” menu in the sidebar. Is there also a way to fully remove the annotation via Applescript?

This should work, I think.

-- Remove annotation record

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Please select some records"
		
		repeat with thisRecord in theRecords
			set thisRecord_Annotation to annotation of thisRecord
			try
				move record thisRecord_Annotation to trash group of database of thisRecord_Annotation
			end try
		end repeat
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

However (as you’ve already found) it currently also doesn’t work to manually remove the annotation record by moving it to trash (and emptying it) …

… so I guess it’s a bug.

Thanks @pete31! A more definite solution also works:

tell application id "DNtp"
	repeat with theRecord in (selection as list)
		set annotation of theRecord to missing value
	end repeat
end tell

I’m also trying to write a script to check if an annotation record really exists, but I’m struggling to get it right, as get annotation of theRecord doesn’t return anything if it doesn’t exists (“no value was returned”) so it’s harder to check for it in an if statement. I’m trying the workaround below, but no success so far. Any tips welcome:

tell application id "DNtp"
	repeat with theRecord in (selection as list)
		set theAnnotationRecord to (annotation of theRecord as list ≠ {}) as boolean
		if theAnnotationRecord then
			set annotation of theRecord to missing value
		end if
	end repeat
end tell

Use a try instead of an if block.

(It’s not unusal that properties in DEVONthink return nothing if there’s no value. In these cases there’s often no other way than using try. Always wondered why that’s the case, maybe @cgrunenberg could explain the reasoning behind it? :slight_smile: )

This works:

tell application id "DNtp"
	repeat with theRecord in (selection as list)
		if not (exists annotation of theRecord) then
			set annotation of theRecord to missing value
		end if
	end repeat
end tell
1 Like

The icon just means that an item has an annotation reference. But the reference might be outdated or the referenced item could be in a closed database.

But shouldn’t deleting an annotation record (or moving it to trash and empyting the trash) trigger an update on the annotated record’s icon? Currently it’s a bit strange from a user’s perspective.

How exactly was the annotation deleted? The easiest method currently is to use the Annotations pop-up of the inspector.

In my case I removed it by deleting the file from the Annotations folder. I especially was looking for a way to remove it programmatically rarther than using the menu option.

This would be something “done behind DEVONthink’s back”, i.e., DEVONthink wouldn’t have knowledge of the change since that folder isn’t indexed for annotations. Perhaps the folder could be dynamically loaded when the dropdown opens but @cgrunenberg would have to assess that.

Sure, but @mdbraber is looking for a way to do it programmatically.

Tried different ways

  • manually move to trash (and emptying it)
  • via AppleScript move to trash (and emptying it)
  • via AppleScript delete

None of them triggered an update of the annotated record’s icon.

So it seems currently the only method that updates the icon is the “normal” way via pop-up of the inspector.

This works to remove the annotation file as well as clearing the property icon in the item list…

tell application id "DNtp"
	set sel to (item 1 of (selected records))
	set annotationFile to annotation of sel
	if annotationFile ≠ missing value then
		set annotation of sel to missing value
		move record annotationFile to trash group of (database of sel)
	end if
end tell

The only artifact is the annotation will still show in the Reminders & Annotations inspector until the file is reselected. This happens whether you move the file to the Trash or use the delete command.

1 Like

Ha, I did try both lines but not both together. Thanks!

Haha! :stuck_out_tongue:

Been there, done that myself. :wink:

The next release will fix this.

This is actually sufficient:

set sel to selected record 1

2 Likes

Noted. Thanks :slight_smile: