How to retrieve fields from Apple Mail emails archived in DT3 (latest beta)

I’m a beginner with AppleScript but definitely see the potential for automating some of my work with DT3. The basics e.g. retrieving and processing a DT record etc… I understand. For instance I can retrieve the name and Reference URL of a record.

Right now I’m wondering how to extract the fields of a selected email my archive. As an example based on the metadata name I’ve tried (unsuccessfully) the following to retrieve the information of the email sender:

	set recordFrom to get custom meta data for "From" from thisRecord

Thanks for your help and tips.

Did you actually create a custom definition of From in Preferences > Data?

I selected it here in the Metadata Menu. I thought this would be sufficient ?

That is not a custom meta data definition. Those are listed in… wait for it… CUSTOM METADATA :stuck_out_tongue:

Also, displaying field in the interface doesn’t affect AppleScript. You could disable all the columns and still successfully get the desired info.

Remember: Automation on a computer is not about replicating mechanical movements and rarely need to interact with the interface.

So you need to be addressing the properties of a record.

Understood and point taken - however if I try something like

	set recordFrom to (From of thisRecord)

or similar it doesn’t work either. So what steps to I take ? It’s a normal imported email and I don’t see the name of the property listed.

Have you looked at the AppleScript dictionary for DEVONthink?
And the Automation chapter in the built-in Help?
Check out the Basic AppleScript Terminology section of that chapter.

And here’s the most basic snippet to gain some understanding about an item. properties of… some item works in many AppleScript-aware applications and instances.

tell application id "DNtp"
	set sel to selection
	properties of (item 1 of sel)
end tell

When you view the Result in the bottom pane of Script Editor (and while it’s cool, Script Debugger is not required to do even very advanced AppleScript). Here’s an example with Safari…

And the Replies section is very useful when debugging.

I’ve done my homework to the best of my abilities:

  • the documentation to understand how to iterate through records
  • the AppleScript dictionary to look for the properties. I can find many properties there e.g. creation date and others.

I’ll now use the snippet you provided to explore how to retrieve properties.

Here an example of extracting info my way so far:

tell application id "DNtp"
	repeat with thisRecord in (selection as list)
		set recordName to (name of thisRecord)
		set recordFrom to (reference URL of thisRecord)
		set recordMod to (modification date of thisRecord)
	end repeat
end tell

Is the magic answer:

kMDItemAuthorEmailAddresses

?

When you view the properties of the file, you should notice you have already requested some of the properties: name, reference URL, and modification date. So you’ve actually gotten farther than you might expect and gotten a good start.

However, if you search the AppleScript dictionary, you won’t find a from property of a record.

Looking at a record’s properties you will see meta data is a property. In fact, it’s a list of properties.

So here is a teaching edition example for you to examine…

tell application id "DNtp"
	repeat with thisRecord in (selection as list)

          set recordProperties to (properties of thisRecord)
	
		set recordMetadata to meta data of recordProperties
		--> {kMDItemRecipients:"Jim", kMDItemSubject:"Hello, Jim. It's Elvis", kMDitemauthors:"Elvis Presly", kMDITemauthoremailaddresses:"eap@thegreatbeyond.com", kMDItemRecipientEmailAddresses:"jim@somewhere.com"}
		
		set senderName to kMDitemauthors of recordMetadata
		--> "Elvis Presley"
		
		set sendersEmail to kMDITemauthoremailaddresses of recordMetadata
		--> "eap@thegreatbeyond.com"
		
		-- Using "get custom meta data" requires a custom definition of "From" in Preferences > Data.
		-- This is also a value you would enter yourself or one that would be populated via smart rules or other scripts. It is NOT an inherent attribute of an email.
		get custom meta data for "from" from thisRecord
		--> "Bobby McGee"

	end repeat
end tell

But one note on the code above, I used an inefficient method since there’s a lesson involved:
Calling the properties of a file then querying from the properties is almost always going to be an inefficient method. I wrote it that way since it is valid and shows you are querying the properties of the record.

That being said, use properties when you’re getting started, perhaps wondering what attributes and values you might find. But in practice you would query the properties directly, as in…

set recordMetadata to meta data of thisRecord
similar to what you were doing previously.

If you’re looking for the email address, then yep! :smiley:

Great - thanks for the crash course. I think I’ve understood enough to proceed.

You’re welcome. :slight_smile: