Find items with attachments in Applescript

I can search for md_attachments>0 - but how can I do this with Applescript? I know I can parse the RTF - but I’m looking for the most efficient way to only process items in a selection that contain attachments. The meta data field of a record doesn’t contain this.

Via the search command. It accepts any query that can be used in a toolbar search.

Thanks, as common as it sounds I hadn’t thought of that. That means I’ll have to ‘create’ the selection via search. Am I right that there’s no parameter / variable in Applescript indicating whether or not a record has any attachments (indicated by the paperclip icon in the view)?

Are all items rich texts? Then you could simply check the type (rtf vs. rtfd)

They are all (imported) e-mails - type of theRecord yields unknown

The next release (version 3.8.4) will add a new property:

tell application id "DNtp"
	repeat with theRecord in (selected records whose attachment count is greater than 0)
	end repeat
end tell
1 Like


@pete31 thanks for this; reading your post set me up to write a script which manages my paper records (so the records I keep on paper as well as in DT); at the click of a button I can now assign a (physical) folder, section, divider to a document, have that data dropped in the custom metadata; that’s rather than having to go through the folder, find an empty divider, then enter the data into custom md myself. So thank you :slight_smile:

1 Like

I’m looking forward to use this with the updated script I wrote for importing attachments: Separate imported e-mail attachments for better search V2 - my current workaround is to only run it on a selection of attachments using search md_attachments>0

Can this also be added as a criterium in smart rules?

Attachment count is already a smart group/rule & search condition.

I completely missed this - thanks for pointing this out!