Done!
Here are the scripts I used.
The first is an utility script that copies meta data identifiers from DEVONthink’s plist.
Script: Copy Custom Meta Data Identifiers from plist
-- Copy Custom Meta Data Identifiers from plist
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
The following scripts need the scripting addition List & Record Tools. Put it in /Users/Username/Library/ScriptingAdditions/
(create it if necessary).
The second writes meta data identifiers of selected records to a text file on desktop.
Script: Write Custom Meta Data Identifiers to file
-- Write Custom Meta Data Identifiers to file
use scripting additions
property theFilePath : (POSIX path of (path to desktop) & "Metadata Identifiers.txt") as string
tell application id "DNtp"
try
set theRecords to selection of viewer window 1 as list
if theRecords = {} then error "Select some records"
set writeToFile to my write_To_File(("" & linefeed) as string, theFilePath, true)
show progress indicator "Writing Metadata Identifiers... " steps (count theRecords) as string with cancel button
repeat with thisRecord in theRecords
step progress indicator (name of thisRecord) as string
set thisMetadata to custom meta data of thisRecord
try
set theRecordPropertyNames to my getPropertyNames(thisMetadata)
repeat with thisName in theRecordPropertyNames
set writeToFile to my write_To_File((thisName & linefeed) as string, theFilePath, true)
end repeat
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 getPropertyNames(theMetadata)
set theRecordPropertyNames to get user property names theMetadata
end getPropertyNames
on write_To_File(this_data, target_file, append_data)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access POSIX file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data as «class utf8» to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access POSIX file target_file
end try
return false
end try
end write_To_File
Identifiers are not deduplicated as this can be done in a text editor, e.g. BBEdit Text > Process Duplicate Lines
.
Finally the main script that deletes given Custom Meta Data from selected records.
There’s a dialog that should prevent accidents, its default button is set to Cancel
. Change this or uncomment the whole dialog if you like.
-- Delete Custom Meta Data
use scripting additions
property deleteMetadataNames : {"mdtest1", "mdtest2"} -- set your Meta Data Identifiers here
tell application id "DNtp"
try
set theRecords to selection of viewer window 1 as list
if theRecords = {} then error "Select some records"
set theButtons to {"Cancel", "Delete"}
set theButton to button returned of (display alert "Delete this Metadata from selected records?" buttons {item 1, item 2} of theButtons default button 1 message my tid(deleteMetadataNames, linefeed) as critical)
if theButton = item 1 of theButtons then return
show progress indicator "Deleting given Metadata... " steps (count theRecords) as string with cancel button
repeat with thisRecord in theRecords
step progress indicator (name of thisRecord) as string
set thisMetadata to custom meta data of thisRecord
try
set newMetadata to thisMetadata
set thePropertyNames to my getPropertyNames(thisMetadata)
repeat with thisPropertyName in thePropertyNames
if (thisPropertyName as string) is in deleteMetadataNames then set newMetadata to my deleteUserProperty(thisPropertyName as string, newMetadata)
end repeat
if newMetadata ≠ thisMetadata then 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
end try
end tell
on tid(theList, theDelimiter)
set d to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theList to theList as text
set AppleScript's text item delimiters to d
return theList
end tid
on getPropertyNames(theMetadata)
set thePropertyNames to get user property names theMetadata
end getPropertyNames
on deleteUserProperty(thePropertyName, theMetadata)
set theMetadata_new to delete user property thePropertyName in theMetadata
end deleteUserProperty