Versioning

Here are two more advanced scripts. The first one creates locked copies of selected contents in subgroups, the second one switches an old and the current version of the selected content.


-- Create new version
-- Created by Christian Grunenberg on Wed Feb 01 2006.
-- Copyright (c) 2006. All rights reserved.

tell application "DEVONthink Pro"
	try
		set theSelection to the selection
		repeat with theRecord in theSelection
			if type of theRecord is not group then
				set theLocation to location of theRecord
				set theLocation to theLocation & my replace_chars(name of theRecord, "/", "_") & " Versions"
				set theGroup to create location theLocation
				set theDuplicate to duplicate record theRecord to theGroup
				set name of theDuplicate to (modification date of theRecord) as string
				set locking of theDuplicate to true
			end if
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then
			try
				display alert "DEVONthink Pro" message error_message as warning
			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 replace_chars(this_text, search_string, replacement_string)
	local od
	set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, search_string}
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to od
	return this_text
end replace_chars


-- Restore old version
-- Created by Christian Grunenberg on Wed Feb 01 2006.
-- Copyright (c) 2006. All rights reserved.

tell application "DEVONthink Pro"
	try
		set theSelection to the selection
		if (count of theSelection) is not 1 or type of item 1 of theSelection is group then error "Please select one content."
		set theRecord to item 1 of theSelection
		set theLocation to location of theRecord
		set theLocation to theLocation & my replace_chars(name of theRecord, "/", "_") & " Versions"
		if (exists record at theLocation) is false then error "No versions have been saved."
		set theGroup to get record at theLocation
		if type of theGroup is not group then "No versions have been saved."
		set theVersions to name of children of theGroup
		if theVersions is {} then error "No versions have been saved."
		choose from list theVersions with prompt "Select a version to restore"
		if the result is not false then
			set theVersion to item 1 of the result
			set theDuplicate to child named theVersion of theGroup
			set locking of theDuplicate to false
			if exists parent 1 of theRecord then
				move record theDuplicate to (parent 1 of theRecord)
			else
				move record theDuplicate to root of current database
			end if
			set name of theDuplicate to name of theRecord
			move record theRecord to theGroup
			set name of theRecord to (modification date of theRecord) as string
			set locking of theRecord to true
		end if
	on error error_message number error_number
		if the error_number is not -128 then
			try
				display alert "DEVONthink Pro" message error_message as warning
			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 replace_chars(this_text, search_string, replacement_string)
	local od
	set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, search_string}
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to od
	return this_text
end replace_chars