Author to clipboard (answering emails)

I’m looking for help on two scripts for DEVONthink Pro Office as I would like to speed up my email-workflow by using the clipboard in conjunction with TextExpander.

I wish I could have:

  • one script, that copies the first name of the author named in the column “author” of a chosen document (.eml) to the clipboard. (Typically the first word in that column.)
  • and one script that does the same with the family name of the author (typically the second word in the column).

Ultracool would be, if the second script would look for a hyphen-minus and respect that, so that for example, when I reply to someone with a double-family name like “Eric Böhnisch-Volkmann”, the part “Böhnisch-Volkmann” and not only “Böhnisch” would be copied to the clipboard.
We have such double family names a lot in Germany so that would be great, but I already would be happy with the simpler variant.

I’m aware, that the use of these scripts has some limitations, e.g. when the author is a company name or the person has a double first name or is named with a titel (Dr. Yuri Zhivago) and some emails do not have a dedicated author at all, but picking up the first or second word in the column would be sufficient for the major percentage of cases.

Thanks for listening!
Bernd

PS: I don’t know if that’s relevant for the script code: I want to launch the scripts externally (via LaunchBar) and not from within DEVONthink Pro Office.

Here’s a simple example which is copying the first word to the clipboard:


tell application "DEVONthink Pro"
	set theRecord to content record
	set theMetaData to meta data of theRecord
	set theAuthor to |kMDItemAuthors| of theMetaData
	set the clipboard to (word 1 of theAuthor)
end tell

Thank you! That helps a lot and with this I can workaround even the double family names. As I always know to whom I answer (seeing the “author” in DEVONthink Pro Office) I can decide, which script I call up and even integrate a salutation, e.g.


set the clipboard to "Sehr geehrter Herr " & (word 2 of theAuthor) & "-" & (word 3 of theAuthor) & ","

Fine, but one more thing, please: I found the command

tell application "System Events"
	keystroke "v" using {command down}
end tell

I added this to your code, as this would enable me to directly paste the results in my reply in Apple Mail and start typing the answer. The problem is, that pasting works in the AppleScript Editor, but it doesn’t work, when I call the script via LaunchBar while I’m in Apple Mail (or TextEdit etc.). I tried .scpt, .scptd and .app and all of them also as “run only”.
What must I change so that it pastes from the clipboard?

PS: Just a note, your code works in Three-Pane-view, but not in List-view. The AppleScript Editor says: “error “Can’t get meta data of missing value.” number -1728 from «class mtdt» of missing value”.

Does it work after inserting “tell application “Mail” to activate” at the beginning?

The script uses the currently displayed record and therefore supports document windows, search windows and 3-pane/column/split view. And advanced version supporting all views could look like this:


tell application "DEVONthink Pro"
	if exists content record then
		set theRecord to content record
	else if (count of the selection) is 1 then
		set theRecord to item 1 of (the selection as list)
	else
		error
	end if
	set theMetaData to meta data of theRecord
	set theAuthor to |kMDItemAuthors| of theMetaData
	set the clipboard to (word 1 of theAuthor)
end tell

Yes, (I saved it as an “app”) it does. :smiley:

Thank you! This works also in list view. Great!

Now my complete script including a salutation looks like this:

tell application "Mail" to activate
tell application "DEVONthink Pro"
	if exists content record then
		set theRecord to content record
	else if (count of the selection) is 1 then
		set theRecord to item 1 of (the selection as list)
	else
		error
	end if
	set theMetaData to meta data of theRecord
	set theAuthor to |kMDItemAuthors| of theMetaData
	set the clipboard to "Hello " & (word 1 of theAuthor) & ","
	
	tell application "System Events"
		keystroke "v" using {command down}
	end tell
end tell

Is there an option to simulate DtPO’s “send reply”-button at the beginning of this script, so that running the script opens a reply window in Apple Mail and inserts the text the above script generates? Then I could type the shortcut while I’m in DtPO to start the script (app) via LaunchBar.

This script is more or less identical to “Send reply”:


tell application "DEVONthink Pro"
	if exists content record then
		set theRecord to content record
	else if (count of the selection) is 1 then
		set theRecord to item 1 of (the selection as list)
	else
		error
	end if
	set theMetaData to meta data of theRecord
	set theSender to |kMDItemAuthorEmailAddresses| of theMetaData
	set theSubject to |kMDItemHeadline| of theMetaData
	if theSubject does not start with "Re:" then set theSubject to "Re: " & theSubject
	set theContent to plain text of theRecord
	
	tell application "Mail"
		activate
		tell (make new outgoing message with properties {visible:true, subject:theSubject})
			set the content to theContent
			make new to recipient at end of to recipients with properties {address:theSender}
		end tell
	end tell
end tell

Heaven … I’m in heaven! :smiley:

Thank you so much!