How to modify a custom field value using AppleScript?

I have created a custom field “Serial Number” whose type is “Identifier”. I want to extract the serialNumber information from a record name and set it to the field “Serial Number”. After that, I want to modify the record name by removing the serialNumber.

All record names follow the format “description+serialNumber+file extension name”, and all serial numbers are a string of numbers starting with ‘#’. For example, “#0000001” of the record “how to construct an CFG#0000001.pdf” is a serialNumber.

I have searched some topics, and obtain the following code:

set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "#"

tell application id "DNtp"
	repeat with thisRecord in (selection as list)
		set recordName to (name of thisRecord)
		-- This splits the recordName on the delimiter, here set to an underscore.
		-- It says, "Ignore the underscores and report the parts they separate."
		set partsOfTheName to (text items of recordName)
		--> {"filename", "00000000001", ".pdf"}
		
		set serialNumber to (item 2 of partsOfTheName)
		display dialog "serialNumber: " & serialNumber
		set the serialNumber of thisRecord to serialNumber as string
		
	end repeat
end tell

-- It's always good to reset the text item delimiters to its original value.
set AppleScript's text item delimiters to oldDelimiters

It doesn’t work, and I have two questions:

  1. How to set serialNumber to the field “Serial Number”?
  2. How to modify the record name, i.e., remove the serialNumber from it?

Many thanks!

set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"#", "."} -- Splits on the # and the dot (assuming you're not using dots in the name except before the extension)

tell application id "DNtp"
	repeat with thisRecord in (selected records)
		set recordName to (name of thisRecord)
		set partsOfTheName to (text items of recordName)
		--> {"filename", "00000000001", ".pdf"}
		
		set serialNumber to (item 2 of partsOfTheName)
		set name of thisRecord to item 1 of partsOfTheName
		add custom meta data serialNumber for "Serial Number" to thisRecord
		
	end repeat
end tell

-- It's always good to reset the text item delimiters to its original value.
set AppleScript's text item delimiters to oldDelimiters

Note add custom meta data is discussed in DEVONthink’s dictionary.

Thank you for your prompt reply.

I have created a field “Serial Number” in advance, and if I excute the above script, I’ll get another field “serialNumber”, as shown in the below figure. How can I set the field “Serial Number” instead of creating a new field.

If the attribute exists, I am not seeing the new one created.

@cgrunenberg:
In Big Sur, it’s creating the attribute but using the concatenated name, here Serialnumber instead of Serial Number if it doesn’t already exist.

Thank you again.

I’m using Big Sur on M1 chip.
So how can I avoid creating a new field? Indeed, I don’t want the script to create a new field. I want to using the pre-created field “Serial Number” to save value.
I test the below statement, but it doesn’t work :

set the "Serial Number" of thisRecord to serialNumber

Thanks!

No problem.

I can’t reproduce the issue on my M1 Mac with an existing attribute.

Have you already applied the Serial Number to files?
If not, delete both attributes in Preferences > Data then quit and relaunch DEVONthink.
Then make the Serial Number one again and test the script once more.


I test the below statement, but it doesn’t work :

set the "Serial Number" of thisRecord to serialNumber

Correct. This will not work as it’s not how you set custom metadata via AppleScript.
As I mentioned previously: Note add custom meta data is discussed in DEVONthink’s dictionary.

Got it. Thank you. :clap:

No problem.

The AppleScript command expects a valid identifier and therefore converts the input if necessary.

So you can only create a multi-word attribute manually, not via scripting?

You can rename it via the interface but it’s recommended to use only identifiers in scripts and to set up custom metadata and all its properties via the preferences.