Make a "Universal" Inbox using Folder Actions

This script creates a kind of “universal” inbox, when compiled and saved in the ~/Library/Scripts/Folder Action Scripts folder and applied to any folder as a folder action. New items dropped into that folder will cause DTPO to display the group selector HUD so that you can select the destination.

  1. DTPO needs to be running, else it will be launched and the folder action might time out
  2. Displays groups from open databases, not closed databases or favorites
  3. Doesn’t prompt for Tags (the current group selector does not show the Tags input)
  4. Doesn’t delete items from the folder; but that is easy to do by adding a directive to Finder in the script
  5. Folder actions sometimes get cranky and stop working; this is usually cleared up at the next reboot

This is offered as a light alternative to the Sorter (greater range and on-the-fly group selection) and the Global Inbox (limited to one destination).

on adding folder items to this_folder after receiving added_items
	try
		if (count of added_items) is greater than 0 then
			tell application id "com.devon-technologies.thinkpro2" to launch
			tell application id "com.devon-technologies.thinkpro2"
				set theGroup to display group selector "Select the destination group"
				repeat with theItem in added_items
					try
						set thePath to theItem as text
						if thePath does not end with ".download:" then
							import thePath to theGroup
						end if
					end try
				end repeat
			end tell
		end if
	end try
end adding folder items to

Works great!

I created a folder on my desktop, and drag email attachments into it, then move to the correct folder. I know there is a service to move email attachments to DTP, and was wondering if your script could be added to that service…

Also, since I am a newbie to scripting, how would I approach your final point about deleting from Finder after moving to DTP?

Thanks!

I opened up the service, “Send Mail Attachment to DevonThink Pro,” and here is the script contained therein:

on run {input, parameters}
	using terms from application "Mail"
		tell application "Finder"
			set strAppSupport to (path to application support from user domain as string)
		end tell
		set theSelectedMessages to input
		repeat with theMessage in theSelectedMessages
			set theText to the content of theMessage
			set messageURL to "message:%3c" & (message id of theMessage) & "%3e"
			try
				set theNames to name of mail attachments in theMessage
				repeat with i from 1 to the count of theNames
					set strFileName to item i of theNames
					set strTempPath to strAppSupport & "DEVONthink Pro 2:Inbox:" & strFileName
					save mail attachment i of theMessage in strTempPath
					my updateComments(theText, strFileName, messageURL)
				end repeat
			on error msg number num
				display alert "It seems that there was no attachment found" message msg as warning
			end try
		end repeat
	end using terms from
	return input
end run

on updateComments(theText, strFileName, messageURL)
	--truncate the file extension if any because DT2 doesn't include the extension in the record name
	try
		set oldDelims to AppleScript's text item delimiters --save current state
		set AppleScript's text item delimiters to "."
		set recordName to the first text item of strFileName
		set AppleScript's text item delimiters to oldDelims
	on error
		set AppleScript's text item delimiters to oldDelims
	end try
	
	--find the newly created record in DT2
	tell application "DEVONthink Pro"
		set lstInbox to (records of inbox)
		set blnFound to false
		set lngLoops to 0
		
		-- Allow for some delay between storage of the file in the DEVONthink 2 Inbox 
		-- and the first visibility of that file to the programming interface
		repeat until blnFound
			set lstFound to (records of inbox) where name = recordName
			--
			if (count of lstFound) > 0 then
				set blnFound to true
				set oRec to last item of lstFound
				set comment of oRec to theText --update the comments to the body of the email
				set URL of oRec to messageURL --include the URL of the Mail message that had the attachment
			end if
			set lngLoops to lngLoops + 1
			if lngLoops > 10000 then exit repeat
		end repeat
	end tell
end updateComments

I have played around a bit to try to insert the "set theGroup to display group selector “Select the destination group” language, but cannot get it to work yet.

Any tips?

This is excellent but how does it differ from just dragging-and-dropping the items onto the DT icon in the dock?

If you mean korm’s original script:

Dragging items to DT’s Dock icon doesn’t support group selection.

Dragging items to the Dock icon supports group selection if DEVONthink > Preferences > Import > Destination is set to “Select Group”