Cannot change custom meta data with AppleScript

I can add it to the record with the “add custom meta data” function, but when I try to change the value of the same custom field with same command it doesn’t work: returns “true”, no errors, but value doesn’t change.

Is there another method for changing existing values?

I just tried this and it works as expected. Could you please post the complete script?

Try this script twice:

tell application id "DNtp"
	set theDoc to item 1 of (get selection)
	set theDate to current date
	add custom meta data theDate for "modified" to theDoc
end tell

First time it is ok, after that “modified” field value does not change…

While this script works just fine:

tell application id "DNtp"
	set theDoc to item 1 of (get selection)
	set theFields to custom meta data of theDoc
	set mdmodified of theFields to current date
	set custom meta data of theDoc to theFields
end tell

Have you tried your first example by putting “md” in front of the field name?

add custom meta data theDate for "mdmodified" to theDoc

Yes, it will create a new field “mdmodified” and you will see the new key in object model named “mdmdmodified”. We should use the key, which is provided in the Preference pane if we want to use “add custom meta data” function.

Interesting… that has not been my experience. I have a script that uses the “add custom meta data” function to update 18 separate meta data fields. I am using the identifiers given in the the Data section of Preferences with “md” added before the identifier. This works for me, and does not add new fields to the Data section of Preferences.

Granted, I’m by no means an expert with AppleScript and DEVONthink, so maybe I’ve just fudged my way into something. Perhaps it isn’t the correct way to accomplish the task. But as I mentioned, it does work for me. Maybe someone else can shed some light on the correct way.

I hope @cgrunenberg will come and speak his word of wisdom )

tell application id "DNtp"
	repeat with thisRecord in (selection as list)
		
		get custom meta data for "newnumber" from thisRecord
		--> 12345
		add custom meta data "98765" for "newnumber" to thisRecord
		--> true
		get custom meta data for "newnumber" from thisRecord
		--> 98765
		
		-- You use the md-prefixed form when addressing the meta data in this way.
		-- The previous method is more user friendly for people to use.
		set recordMetadata to (custom meta data of thisRecord)
		set mdnewnumber of recordMetadata to "77777"
		--> true
		mdnewnumber of recordMetadata
		--> 77777
		
	end repeat
end tell

@cgrunenberg would have to comment on the Date metadata. it sets once but doesn’t accept subsequent changes when the Date data type is used.

1 Like

Yes, as I mentioned earlier, the script with “md”-prefix works fine, even with “date”.
Problem is just with “add custom meta data” and “date” type…

My example is for the benefit of any passersby who may be interested in the subject.

1 Like

As a temporary workaround:
This code solves all the problems with any kind of data, with any state of custom meta data (missing value; needed field is non-existent; field exists, but we need to change the value)

tell application id "DNtp"
	set theDoc to item 1 of (get selection)
	set theDate to current date
	
	set theFields to custom meta data of theDoc
	try
		theFields
	on error number -2753
		set theFields to {}
	end try
	set theFields to {mdmodified:theDate} & theFields
	set custom meta data of theDoc to theFields
end tell

And the Handler:

on SetCustomMD(theLabel, theValue, theRecord)
	tell application id "DNtp" to set theFields to custom meta data of theRecord
	try
		theFields
	on error number -2753
		set theFields to {}
	end try
	set theField to {}
	set theField to set user property ("md" & theLabel) in theField to theValue
	set theFields to theField & theFields
	tell application id "DNtp" to set custom meta data of theRecord to theFields
end SetCustomMD

What it does:

  • This handler sets given custom meta data. You will need to provide the same parameters as to the «add custom meta data» function: Label (AppleScript Identifier, which you may find in DT Preferences - Data), the Value for this Label and the Record where to change meta data. It works with any data types and does the following:
    • Adds data if given «label-value pair» doesn’t exist in meta data;
    • Adds data if the custom meta data is absent at all in a given Record;
    • Overwrites data if the Record already has the value for given label in custom meta data.

This handler needs scripting addition «List & Records Tools» from «Late Night Software». You may find it here.

1 Like

The next release will fix this issue.

1 Like