Batch rename for Regex freaks ;-)

For anybody familiar with regular expressions, here comes a small script which lets you rename all selected DT items using regex expressions.

Example usage. Let’s say I’ve got a few docs:

invoice_05_2009.pdf
invoice_09_2008.pdf
invoice_10_2008.pdf

And I would like to reverse the number schema (05_2009 -> 2009_05)…

Using the script below I can specify the source pattern:


([0-9]+)_([0-9]+)

and destination pattern:


\2_\1

and this will do the trick. :slight_smile:

Here’s the script:


set sourcePattern to text returned of (display dialog ¬
	"Enter source pattern" with title ¬
	"DEVONthink batch rename" default answer ¬
	"" buttons {"Accept"} ¬
	default button 1)

set destPattern to text returned of (display dialog ¬
	"Enter destination pattern" with title ¬
	"DEVONthink batch rename" default answer ¬
	"" buttons {"Accept"} ¬
	default button 1)


if sourcePattern is not "" and destPattern is not "" then
	
	tell application "DEVONthink Pro"
		
		set selectedItems to selection
		
		repeat with selectedItem in selectedItems
			
			set itemName to name of selectedItem
			
			set transformedName to do shell script "echo \"" & itemName & "\" | sed -E 's/" & sourcePattern & "/" & destPattern & "/g'"
			
			set name of selectedItem to transformedName
			
		end repeat
		
	end tell
	
end if