Prefix DEVONthink Item Name with Date Stamp

Anyone who has been following my efforts over the last few months may have noted that I have occasionally made reference to migrating from Evernote to DEVONthink. As part of that move I have been taking the opportunity to sort out some of my lax filing. One of those aspects is the naming of files and specifically the inclusion of date stamps where relevant - e.g. on bills, documentation releases, etc. Fortunately I was able to bring a little bit of automation to bear on this activity, which has been helping speed things up significantly.

Date Stamping files

When I did my initial import from Evernote, using DEVONthink’s inbuilt import feature, it brought across the creation timestamps for the file attachments that then appear as separate items in DEVONthink. In the vast majority of cases, the cases I can automate, I want to use that date in the item’s name. The question you may be asking is why? After all, that information is already in DEVONthink.

The reason is that it helps me sort, organise and find files both within DEVONthink, and outside of it if I am doing any batch file processing or sharing on. Date stamping is often a useful addition to file management.

My personal recommendation is to prefix a file with the date stamp in the format “yyyy-mm-dd “. This allows for quick and easy chronological based sorting, and the use of hyphens as separators allows me to do things like search for things like matching months or days (e.g. “-07-“ would be a search for July, and “-01 “ a search for the first of any month).

An AppleScript Solution

Fortunately, DEVONthink comes with a wealth of AppleScript scripts and an extensive AppleScript dictionary all ready to provide access to a huge amount of DEVONthink’s functionality. Inclusive within this is a way to interact with and update items stored in DEVONthink.

The script I came up with processes as follows:

  1. Get the current selection of items in DEVONthink.
  2. For each item in the selection, get the creation date from the item’s meta data, construct a date stamp from it, and prefix that to the item’s name.
tell application id "DNtp"
	set thisSelection to the selection
	try
		--Check selection
		if not (exists think window 1) then error "No window is open."
		
		repeat with theRecord in thisSelection
			--Build creation date stamp string
			set dtRecordDate to date of theRecord
			set strYear to year of dtRecordDate
			set strMonth to texts -1 thru -2 of ("0" & (month of dtRecordDate as number))
			set strDay to texts -1 thru -2 of ("0" & day of dtRecordDate)
			set strDateStamp to strYear & "-" & strMonth & "-" & strDay
			
			--Rename with date stamp prefix
			set strNewName to strDateStamp & " " & name of theRecord
			set name of theRecord to strNewName as string
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

Many scripts both in DEVONthink and online focus on working with the current item, but I was really keen to work on batches of files (usually identified through a prior search), and so the processing of a selection rather than the currently viewed item has been absolutely core to the speed of processing improvement this automation has provided me.

I saved the script to the following file path:

/Users/stephen/Library/Application Scripts/com.devon-technologies.think3/Menu/Rename/Prefix Title with Datestamp.scpt

This then makes the script available via DEVONthink’s script menu.

While I am using this a lot, I have also tied it to a keyboard shortcut via Keyboard Maestro, but you could use any number of Mac automation tools to do this, including making a Quick Action (née Service) using Apple’s macOS inclusive options of Shortcuts or Automator, and then associating a keyboard shortcut with that via the Mac’s keyboard preferences.

Summary

Being able to speed up my renaming has been great, and for the thousands of files I apparently have, this is going to have saved me hours of time, improved my accuracy, and stopped my mind going completely numb. It isn’t a particularly involved or complex automation, but t is often these smaller automations which are used frequently that can make the biggest impact on your computing time.

Even if you do not need to incorporate date stamps into your DEVONthink use, maybe you have other batch processing needs where you could adapt the script above. My advice is always to give it a go. Do let me know over on Twitter if you found this useful, or if you have adapted it in some way for your own batch processing use.

Author: Stephen Millard
Tags: | applescript | devonthink |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read