How to unselect an item in a custom metadata "set" field?

I have a custom metadata set field which contains 5 items. I accidentally selected one of these items in the info pane. I can’t see how to unselect it for that document?

To be honest, I can’t figure it out either. So here’s a script that does it:

# mark only the records for which the Set custom metadata should be removed
# CHANGES MADE BY SCRIPTS CANNOT BE UNDONE

tell application id "DNtp"
	set theRecords to selection
	repeat with theRecord in theRecords
	# in the following line, use the Identifier (of the custom metadata, see Preferences) instead of test
		add custom meta data "" for "test" to theRecord
	end repeat
end tell

Run the script from Script Editor. If you are unsure what I mean by the Identifier, please write back before attempting to run the script: Changes made by scripts cannot be undone!

@cgrunenberg Criss am I missing something or is there no possibility to remove a selection from the UI?

@cgrunenberg Criss am I missing something or is there no possibility to remove a selection from the UI?

While I’m clearly not @cgrunenberg :stuck_out_tongue: no, currently there is no way to set a selection in single-line text or a set to an empty state.

How is that possible? So what does one do when one selects it by accident? Can this feature be added please?

How is what possible?

I would assume it is simply an oversight :slight_smile:

One writes a quick script :wink: Or somebody does it for one. You’re welcome.

That, of course, was why I was prompting Criss - in typical English understatement, that is :slight_smile:

1 Like

Many thanks @Blanc . I’m a bit of a newbie with DT. That sounds good. Can you give any clues on how to run the script?

Sure.

In DEVONthink 3: go to Preferences > Data, and then highlight the set which you later want to change. In the top righthand half of the window you will see Identifier: - make a note of the identifier. Close the preferences. By selecting it, highlight the record you want changed.

Open Script Editor (e.g. by doing command-space and typing script): copy and past the script from this thread into script editor. As instructed in the script, change the word test in the line add custom meta data "" for "test" to theRecord to the identifier (see first part of these instructions), enclosed in ".

Make sure the correct record is selected in DEVONthink. Run the script by selecting the appropriate button in Script Editor or selecting Script > Run in Script Editor. There will be no feedback; the Set associated with the record will have been changed. Verify that is the case. Close Script Editor. Sit back. Grin. You’ve just embarked on a journey to learning scripting DEVONthink.

The script is quick and dirty - that is, it was written for one-off use to rectify a specific problem. As such it contains no error trapping. If this were something you were going to be doing repeatedly, it would be worth adding a few lines.

Thanks so much @Blanc , that seems to work as intended! Is there any way to run that script from within DT, rather than having to open the separate script editor app?

Also, would it be possible to modify the script to empty 2 separate custom metadata fields, rather than just the 1?

Sure :slight_smile:

this line add custom meta data "" for "test" to theRecord is the one which actually overwrites the custom metadata; so simply adding another such line will make the script handle two custom metadata fields. Of course each line needs to point to the appropriate identifier. Rather than do that, however, we can make the script go round and round in circles until it has handled a whole list of identifiers which you supply; that routine is included in the script below.

If you are going to use the script on any kind of regular basis, it should be expanded to do three more things; first, to give you notice if you forget to actually select any records; second to only replace the custom metadata if there actually is currently any metadata in the appropriate field; and third to trap any errors.

The first is done by adding if theRecords = {} then error "Please select some records."
The second is done by adding an if custom metadata in question isn't empty routine.
The third is done by adding a try routine with an on error section.

The end result looks like this (and is taken, in part, from this post by @bluefrog (no point in reinventing the wheel); the error handling is copied from @pete31.

# Mark only the records for which the defined custom metadata should be removed
# CHANGES MADE BY SCRIPTS CANNOT BE UNDONE

# In the following line of code (starting with: property), between the {} add the identifiers of metadata to be removed. Each identifier needs to be enclosed in " and identifiers should be separated by a comma, e.g. {"test", "another", "etc"}.
# The appropriate identifier can be found in DEVONthink > Preferences > Data, by highlighting the metadata in question; the identifier will then be shown in the top righthand half of the Preferences window.

property attributesToScrub : {}

tell application id "DNtp"
	try
		set theRecords to selection
		if theRecords = {} then error "Please select some records."
		if attributesToScrub = {} then error "Please add identifiers to the Property \"attributesToScrub\" in the Script"
		
		repeat with theRecord in theRecords
			repeat with thisAttribute in attributesToScrub
				set mdQuery to (get custom meta data for thisAttribute from theRecord)
				if mdQuery ≠ missing value then
					add custom meta data "" for thisAttribute to theRecord
				end if
			end repeat
		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

And for the last trick: Scripts can be included in the toolbar in DEVONthink; this is explained on pp 197, 198 of the current DEVONthink 3.8 Documentation.