Some useful little scripts for navigating DT

I often find it tiresome using DT Pro’s contextual menus to move, replicate etc, and drag and drop can be a pain, especially on a notebook. So I’ve whipped together a number of little scripts that, when assigned hotkeys (for example, using Red Sweater Software’s wonderful FastScripts program) makes it possible to zip about DT with a couple of keystrokes. They’re all dead simple, but perhaps people with more scripting know-how than my cruelly limited skills can do more with them.

The first moves the selected records to a location chosen via the standard pop-up list:

--MOVE TO...
tell application "DEVONthink Pro"
		set theRecords to the selection
		set inGroup to display group selector "Move to:" buttons {"Cancel", "OK"}
		
		repeat with thisRecord in theRecords
			move record thisRecord to inGroup
		end repeat	
end tell

The second allows you to replicate records in the same way:

--REPLICATE TO...
tell application "DEVONthink Pro"
		set theRecords to the selection
		set inGroup to display group selector "Replicate to:" buttons {"Cancel", "OK"}
		
		repeat with thisRecord in theRecords
			replicate record thisRecord to inGroup
		end repeat
end tell

The third takes you to the desired location ie opens its window. Not much of an advance on double-clicking in the Show Groups list, but might come in handy for some uses:

-- GO TO...
tell application "DEVONthink Pro"
		set inGroup to display group selector "Go to:" buttons {"Cancel", "OK"}
		open window for record inGroup	
end tell

The fourth creates a new text note in the desired location, and opens the window, ready for you to type in. Put this in the general scripts folder, assign a hot-key, and you have system-wide, quick-access way to enter notes in DT:

--QUICK TEXT NOTE:
	tell application "DEVONthink Pro"
		activate
		set inGroup to display group selector "Make new note in:" buttons {"Cancel", "OK"}
                 display dialog "Title of note:" default answer "" buttons {"Cancel", "OK"} default button 2
                set theTitle to the text returned of the result
		set theNote to create record with {name:theTitle, type:txt} in inGroup
		open window for record theNote
	end tell

The fifth opens a browser window to whatever URL you enter (and saves the address for good measure):

--GO TO URL:
	tell application "DEVONthink Pro"
		activate
		set inGroup to display group selector "Make link in:" buttons {"Cancel", "OK"}
		display dialog "Go to URL:" default answer "" buttons {"Cancel", "OK"} default button 2
		set theURL to "http://" & the text returned of the result
		set theLink to create record with {name:"New Link", type:link, URL:theURL} in inGroup
		open window for record theLink
	end tell

The sixth “bookmarks” the URL of whatever page you are viewing in the location indicated:

--SAVE URL
	tell application "DEVONthink Pro"
		activate
		set inGroup to display group selector "Bookmark URL in:" buttons {"Cancel", "OK"}
		set theURL to URL of front window
		set theTitle to name of front window
		set theLink to create record with {name:theTitle, type:link, URL:theURL} in inGroup
	end tell

I’ve used the pop-up folder list in each of these, but of course you could set a fixed location to move, replicate etc into as follows:


set inGroup to create location "/IN BOX"

[i]File your documents with one keypress! Jump straight to your most-used folders!

Finally, a couple of extras. This one saves the currently open tabs in NetNewsWire in html form:[/i]

--SAVE NETNEWSWIRE TABS
tell application "NetNewsWire"
	set titleList to titles of tabs
	set urlList to URLs of tabs
	set tabComp to ""
	repeat with i from 2 to the count of titleList
		set tabData to "<a href='" & item i of urlList & "' target='_blank'>" & item i of titleList & "</a>
" & "[" & item i of urlList & "]" & "

"
		set tabComp to tabComp & tabData
	end repeat
end tell
tell application "DEVONthink Pro"
	set inGroup to create location "/IN BOX"
	create record with {name:"NetNewsWire Tabs", type:html, source:tabComp} in inGroup
end tell

And this one hacks together the standard-issue DT script for importing messages from Mail and Red Sweater’s Save HTML Content script. Presto: a script that imports html messages:

--IMPORT HTML MESSAGES FROM MAIL
property pNoSubjectString : "(no subject)"
property pLocation : "/IN BOX"

tell application "Mail"
	try
		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 theSelection to the selection
		if the length of theSelection is less than 1 then error "One or more messages must be selected."
		repeat with theMessage in theSelection
			my importMessage(theMessage, theLocation)
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then
			try
				«event sysodisA» "Mail" given «class mesS»:error_message, «class as A»:«constant EAlTwarN»
			on error number error_number
				if error_number is -1708 then display dialog error_message buttons {"OK"} default button 1
			end try
		end if
	end try
end tell

on importMessage(theMessage, theLocation)
	tell application "Mail"
		try
			set theDateReceived to the date received of theMessage
			set theDateSent to the date sent of theMessage
			set theSender to the sender of theMessage
			set theSubject to subject of theMessage
			if theSubject is equal to "" then set theSubject to pNoSubjectString
			
			set theContent to the source of theMessage
			set theHTMLContent to my TextBetweenTwoTags(theContent, "<html", "</html>")
			tell application "DEVONthink Pro" to create record with {name:theSubject, type:html, creation date:theDateSent, modification date:theDateReceived, URL:theSender, plain text:(theHTMLContent as string)} in theLocation
			set the flagged status of theMessage to true
		end try
	end tell
end importMessage

on TextBetweenTwoTags(theText, startTag, endTag)
	set foundText to ""
	set startOffset to offset of startTag in theText
	set endOffset to offset of endTag in theText
	if endOffset is equal to 0 then
		set endOffset to length of theText
	else
		set endOffset to endOffset + (length of endTag)
	end if
	
	-- If we got a reasonable looking subset...
	if (startOffset is greater than 0 and endOffset is greater than startOffset) then
		set foundText to (texts startOffset through endOffset of theText)
	end if
	return foundText
end TextBetweenTwoTags

I have no AppleScript skills. How do you go about installing these scripts — where do you paste them into?

I am a newbie and clueless, but I’d like this functionality. Thanks.

Robyn :blush:

Open Script Editor, hit cmd-N to create a new script, paste the code in, compile it (hit cmd-K or click on the little hammer icon in the toolbar), save to ~/Library/Scripts/Applications/DEVONthink Pro.

You can run it from the pulldown menu in the menubar, among other options. Better yet, get FastScripts, and you can hotkey it.

Here’s a streamlined version of the “Go to URL” script, for those who want to see the page now and file the link later:

tell application "DEVONthink Pro"
	activate
	display dialog "Go to:" default answer "" buttons {"Cancel", "OK"} default button 2
	set theURL to "http://" & the text returned of the result
	set theLink to create record with {type:link, URL:theURL}
	open window for record theLink
	set the name of theLink to theURL
end tell

Here’s an even quicker way to input short notes into DT, from wherever:

--EVEN QUICKER NOTE!
tell application "DEVONthink Pro"
	activate
	display dialog "Title of new note:" default answer "" buttons {"Cancel", "OK"} default button 2
	set theTitle to the text returned of the result
	display dialog "Text of new note:" default answer "" buttons {"Cancel", "OK"} default button 2
	set theText to the text returned of the result
	set theNote to create record with {name:theTitle, type:txt, plain text:theText}
end tell

acoyne,

Thanks for these scripts. Short & simple, and just what I need to streamline my DT workflow.