Script: Remove some custom meta data

This script removes some custom meta data from selected records.

I find it quite confusing to see custom meta that I already deleted in Preferences > Data still attached to a record, the script fixes this confusion.

Setup

  • Set identifiers you want to remove in property theMetadataIdentifiers

To get the custom meta data identifiers you can either look them up in Preferences > Data or use this

Script: Copy Custom Meta Data Identifiers from plist
-- Copy Custom Meta Data Identifiers from plist
-- This script copies one identifier at a time.

property sortIdentifiers : true
property thePlistPath : POSIX path of (path to application support from user domain) & "DEVONthink 3/CustomMetaData.plist"

tell application "System Events"
	try
		set theIdentifieres to {}
		tell property list file thePlistPath
			repeat with thisItem in property list items
				set thisItem_Value to value of thisItem
				set thisIdentifier to identifier of thisItem_Value
				set end of theIdentifieres to thisIdentifier
			end repeat
		end tell
		
		if sortIdentifiers = true then set theIdentifieres to my sort_list(theIdentifieres)
		
	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

tell application id "DNtp"
	try
		activate
		set chooseFromListItems to theIdentifieres
		set theChoice to choose from list chooseFromListItems with prompt "Copy Identifier" default items (item 1 of chooseFromListItems) with title "Custom Meta Data"
		if theChoice is false then return
		set the clipboard to (item 1 of theChoice) as string
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

on sort_list(theList)
	considering numeric strings
		set theIndexList to {}
		set theSortedList to {}
		repeat (length of theList) times
			set theLowItem to ""
			repeat with a from 1 to (length of theList)
				if a is not in theIndexList then
					set theCurrentItem to item a of theList as text
					if theLowItem is "" then
						set theLowItem to theCurrentItem
						set theLowItemIndex to a
					else if theCurrentItem comes before theLowItem then
						set theLowItem to theCurrentItem
						set theLowItemIndex to a
					end if
				end if
			end repeat
			set end of theSortedList to theLowItem
			set end of theIndexList to theLowItemIndex
		end repeat
	end considering
	return theSortedList
end sort_list

Note: Due to an AppleScript limitation it’s not possible to remove meta data of type rich text.

-- Remove some custom meta data

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

property theMetadataIdentifiers : {"mdtest", "mdtest2"} -- set the identifiers of the meta data you want to remove

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Please select some records"
		show progress indicator "Removing Metadata... " steps (count theRecords) as string with cancel button
		
		repeat with thisRecord in theRecords
			step progress indicator (name of thisRecord) as string
			try
				set thisRecord_Metadata to (custom meta data of thisRecord) & {}
				set newMetadata to my removeMetadata(thisRecord_Metadata)
				set custom meta data of thisRecord to newMetadata
			end try
		end repeat
		
		hide progress indicator
	on error error_message number error_number
		hide progress indicator
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

on removeMetadata(theRecord_Metadata)
	try
		set theMetadataDictionary to current application's NSMutableDictionary's dictionaryWithDictionary:theRecord_Metadata
		
		repeat with thisMetadataIdentifier in theMetadataIdentifiers
			set thisMetadataIdentifier to thisMetadataIdentifier as string
			(theMetadataDictionary's removeObjectForKey:thisMetadataIdentifier)
		end repeat
		
		return theMetadataDictionary as record
		
	on error error_message number error_number
		activate
		display alert "Error: Handler \"removeMetadata\"" message error_message as warning
		error number -128
	end try
end removeMetadata

2 Likes

This is currently intentional. Depending on the custom metadata it’s also possible to reset it via batch processing.

1 Like