Import an Evernote note's attachments to DEVONthink

See this request:

Here is a script that will import the attachments of a selection of Evernote notes into DEVONthink. Use:

  1. Make a selection of notes in Evernote that have attachments (embedded images are attachments, BTW)
  2. Run the script
  3. DEVONthink will prompt you for a destination – you may optionally create a new destination at this time
  4. The script will import the attachments (using the name they are stored with in Evernote), assign that note’s tags to the attachment, and add a link-back to the original Evernote note to the new document in DEVONthink

Use at your own risk. If your data gets lost, mangled, or disappears – you were warned.

set tempPath to path to temporary items from user domain

set theAttachments to {}
set selectedItems to {}

tell application id "DNtp"
	set theGroup to display group selector
end tell

tell application id "EVRN"
	set selectedItems to selection
	repeat with selectedItem in selectedItems
		set theTitle to the selectedItem's title
		set theAttachments to (attachments of selectedItem)
		set theTags to the selectedItem's tags
		set theLink to the selectedItem's note link
		repeat with theAttachment in theAttachments
			set theAttachmentName to theAttachment's filename
			set theFilename to (tempPath & theAttachmentName) as string
			write theAttachment to theFilename
			tell application id "DNtp"
				set theImport to import theFilename name theAttachmentName to theGroup
				repeat with thisTag in theTags
					set the tags of theImport to the (tags of theImport) & the name of thisTag
				end repeat
				set the URL of theImport to theLink
			end tell
		end repeat
	end repeat
end tell

Problem for someone else to fix: some attachments are not imported at the same scale as they are stored in Evernote. Probably has something to do with the way Evernote refreshes content for its display. Update: looks like it would be useful for notes that are clipped from web content to have the script download the image at the original URL for that image and not the thumbnail that Evernote stores in its notes.