Script for linking from Address Book

I just have created a script for a customer who wanted to link from DEVONthink Pro to address book records. Here it is:

-- Create link to address book record
-- Created by Eric Böhnisch-Volkmann on Mon Aug 28, 2006.
-- Copyright (c) 2006. All rights reserved.

property pLocation : "/"

using terms from application "Address Book"
	
	on action property
		return "phone"
	end action property
	
	on action title for p with e
		set theName to (first name of p) & " " & (last name of p)
		return "Create link in DEVONthink"
	end action title
	
	on should enable action for p with e
		return true
	end should enable action
	
	on perform action for p with e
		if company of p then
			set theName to organization of p
		else
			set theName to (first name of p) & " " & (last name of p)
		end if
		addToDEVONthink((id of p), theName)
	end perform action
	
end using terms from

on addToDEVONthink(id, theName)
	tell application "DEVONthink Pro"
		if not (exists current database) then error "No database is in use."
		set theLocation to create location pLocation
	end tell
	set theURL to "addressbook://" & id
	tell application "DEVONthink Pro"
		tell application "DEVONthink Pro" to create record with {name:theName, type:link, URL:theURL} in theLocation
	end tell
end addToDEVONthink

Place it in ~/Library/Address Book Plug-Ins/ and restart Address Book.

To link to a card, visit the card in Address Book and control-click the label of a phone number. Choose “Link to DEVONthink” from the pop-up menu. The script creates an “addressbook://” link in your DEVONthink Pro database that opens the card when you double-click it.

Eric.

The addressbook:// links might eventually be useful for me with editable documents rather than as separate uneditable URL documents that the script creates. With that in mind, a modified version of the script could create a Rich Text document with the AB link in its URL metadata field and/or as a clickable link for the document’s initial content.

I too would find this infinitely more useful – for AI purposes particularly – if this were to import the Address Book information into the text area, as well as create the link. Obviously, this is beyond the scope of the nice script provided here. Nonethelss, I think – for archiving, “see also”, and other purposes – this is where such interaction would be more fruitful in the long term (v2?).

No problem, here’s a modified variant of the script. I did intentionally NOT copy all address details into the RTF because it would not update dynamically when the address book changes. I would not want to keep the same data in two different places.

-- Create an RTF docment with a link to an address book record 
-- Created by Eric Böhnisch-Volkmann on Thu Sep 7, 2006. 
-- Copyright (c) 2006. All rights reserved. 

property pLocation : "/"

using terms from application "Address Book"
	
	on action property
		return "phone"
	end action property
	
	on action title for p with e
		set theName to (first name of p) & " " & (last name of p)
		return "Create RTF in DEVONthink"
	end action title
	
	on should enable action for p with e
		return true
	end should enable action
	
	on perform action for p with e
		if company of p then
			set theName to organization of p
		else
			set theName to (first name of p) & " " & (last name of p)
		end if
		addToDEVONthink((id of p), theName)
	end perform action
	
end using terms from

on addToDEVONthink(id, theName)
	tell application "DEVONthink Pro"
		if not (exists current database) then error "No database is in use."
		set theLocation to create location pLocation
	end tell
	set theURL to "addressbook://" & id
	tell application "DEVONthink Pro"
		tell application "DEVONthink Pro" to create record with {name:theName, type:rtf, URL:theURL, rich text:theName & ", <" & theURL & ">"} in theLocation
	end tell
end addToDEVONthink

Eric.

Thanks, Eric. No problems when testing it. A couple questions:

• Does it make sense to include the Middle Name field of the AB record in the RTF?
• Can the link in the RTF be made clickable when it’s created, same as selecting it and running Format > Make Link (shift-command-M) would do afterwards?

Which reminds me… is there a recommended method for selecting text of a clickable link without opening it?

Btw, anyone else notice how three Erics are the most prolific script writers (excluding Christian) making contributions on the forum? :smiley:

Well, here in Europe we don’t use middle names not much. But, here’s a new version that includes also the middle name in the PDF. Making the link clickable is a hard task as modifying RTFs using AppleScript is complicated and error-prone. So, I left this as is for the moment.

-- Create an RTF document with a link to an address book record 
-- Created by Eric Böhnisch-Volkmann on Thu Sep 7, 2006. 
-- Copyright (c) 2006. All rights reserved. 

property pLocation : "/"

using terms from application "Address Book"
	
	on action property
		return "phone"
	end action property
	
	on action title for p with e
		return "Create RTF in DEVONthink"
	end action title
	
	on should enable action for p with e
		return true
	end should enable action
	
	on perform action for p with e
		if company of p then
			set theName to organization of p
		else
			set theName to (first name of p) & " "
			if (middle name of p) is not equal to "" then
				set theName to theName & (middle name of p) & " "
			end if
			set theName to theName & (last name of p)
		end if
		addToDEVONthink((id of p), theName)
	end perform action
	
end using terms from

on addToDEVONthink(id, theName)
	tell application "DEVONthink Pro"
		if not (exists current database) then error "No database is in use."
		set theLocation to create location pLocation
	end tell
	set theURL to "addressbook://" & id
	tell application "DEVONthink Pro"
		tell application "DEVONthink Pro" to create record with {name:theName, type:rtf, URL:theURL, rich text:theName & ", <" & theURL & ">" & return & return} in theLocation
	end tell
end addToDEVONthink

Command-click into the link to place the insertion caret into it. Then use the arrow keys and the Shift key to select the text of the link.

Eric.

I’m not sure how common middle name usage is in the US. Three-character usernames with middle initials were popular among computer hackers in the 1970s/80s, sometimes mirroring full name usage – rms, Richard M. Stallman, being a classic example. I still prefer sjk as my short e-ID whenever/wherever possible though I’ve increasingly dropped the ‘j’ from my full name.

RTF, and this new version divulges an unannounced DEVONthink product name. :slight_smile: Oh, there’s still a ‘u’ missing in “document” in the first comment.

No big deal; it’s easy to do manually if desired.

Ahh, thanks for the tip! I hadn’t thought to try command-click since I’m so accustomed to command being an active modifier key when clicking links in other apps; e.g. to open links in the background from Mail, a particularly useful recent discovery. My new method for selecting an active link in DT documents will be to command-double-click on its first or last word, then drag the opposite direction to select the remainder. More efficient than accurately clicking to place the insertion point exactly before/after the link before dragging.

Does it? Look carefully :smiley:

Eric.

:stuck_out_tongue: And that’s isn’t (wasn’t) the only (or first) place I’ve seen it… check your PM here for details.