Convert tags to custom text field w smart rule

I’ve imported a lot of data from Paperless to Devonthink - Paperless fields are imported as tags.

No I want to create a smart rule converting the tag “amount=51,12 €” (51,12 being the variable) to a custom text field showing “51,12 €” and then delete the tag.

But: In the smart rule dialog I cannot scan tags, I can only scan name and text.

Is there a way to convert a tag to text, so it can be scanned for strings - without havin to create a script?

Thanks :slight_smile:

I doubt that. What you did doesn’t really translate well to tags (as you’ve found out, I guess). Since the “tag” probably came from the document itself, you could try DT’s amount related functions/scripts. Alternatively, you’d have to write your own script:

  • get all records in the current database with a tag like amount= – that will probably be very slow
  • remove “amount=” from the tag
  • copy the rest to your custom metadata field

Though I don’t understand why you’d want a text field to contain a number – why not a number field?

Thank you!

I did have a number field. When trying around w a smart rule I could’t not assign “Insert Placeholder” to the number field. After changing it to text field “Insert Placeholder” was available. Anyway, I couldn’t extract a string from the tag to insert to the custom field.

I’m not good w scripts, so I was hoping for a smart rule solution.

But thank you for your advice!

Supposing that you have selected the relevant records manually, this JavaScript code might help

(() => {
  const app = Application("DEVONthink 3");
 /* Loop over all selected records */
  app.selectedRecords().forEach(r => {
   /* get the list of tags */
     const tags = r.tags();
     /* filter out the tags containing "amount=" */
     tags.filter(t => t.includes("amount=")).forEach(t => {
      /* remove 'amount=' from the tag */
      const amount = t.replace("amount=","");
      /* set the custom metadata field "price" to the string following "=" */
      r.addCustomMetaData(price, {for: r, to: "price"});
    })
  })
})()

I can’t test that since I don’t have appropriate data (nor time right now). But in principle it should work like that. Note that I use your convention of a string metadata field. If you want a number, the string must be converted before calling addCustomMetaData.

Thank you so much for the code. Unfortunately I didn’t get it running.

Well, if you provided more detail as to what you did and what you experienced, one might help you. With that post – not possible

1 Like

Your’re right. Sorry!

I copied the code to MacOS Script Editor.

I changed the custom metadata field in DT to currency. The field has the identifier „price“ and is named “Betrag”, therefore I corrected “amount” in lines 11 and 12 to „price“. (Later I tried it with „Betrag“, too).

(() => {
  const app = Application("DEVONthink 3");
 /* Loop over all selected records */
  app.selectedRecords().forEach(r => {
   /* get the list of tags */
     const tags = r.tags();
     /* filter out the tags containing "amount=" */
     tags.filter(t => t.includes("amount=").forEach(t => {
      /* remove 'amount=' from the tag */
      const amount = t.replace("amount=","");
      /* set the custom metadata field "price" to the string following "=" */
      r.addCustomMetaData(price, {for: r, to: "price"});
    })
  })
})()

When trying to save it said the following:

Screenshot 2024-03-16 at 13.34.15

Still the script was saved and I tried to run it in DT (having selected the records manually), but it didn’t do anything. No removing of „amount=“ and no changes to the metadata field.

I would say there is no need for a smart rule. Please read this blog post on how to assess your automation needs…

This AppleScript does what you’re asking for…

property tagDelimiter : "amount="
set od to AppleScript's text item delimiters

tell application id "DNtp"
	if (selected records) is {} then return
	repeat with theRecord in (selected records)
		set recordTags to tags of theRecord
		repeat with theTag in recordTags
			if theTag begins with tagDelimiter then
				set AppleScript's text item delimiters to {tagDelimiter, "€"}
				set md to (text items of theTag)
				set AppleScript's text item delimiters to od
				add custom meta data (md as string) for "Price" to theRecord
				exit repeat
			end if
		end repeat
	end repeat
end tell

Note: I am using a custom attribute, Price, with a data type of Decimal Number as Currency. This would also work with a text-based data type.

There was a parenthesis missing in the code. I updated the script accordingly.

This works wonderful, thank you so much!

The tag „amount=2.000 €“ was translated to the metadata field as „2.00 €“. Number format in system preferences was correct.

So I added the following line to the script:

				set AppleScript's text item delimiters to {tagDelimiter, "."}

It works great now. All tags are converted.

Yes, there’s no more syntax error. Oddly, the script still doesn’t do anything. Today I read that Sonoma 14.4 (which I already have updated to) could render Java unusable. I wonder if that’s the reason.

You’re very welcome :slight_smile:

Why would unusable Javs have anything to do with JavaScript code not doing what I want it to do? The two languages have absolutely nothing to do with each other.