Toggling the case of record names

I find it useful to switch some of the record names in a group to upper case (sometimes temporarily).

Couldn’t see how to do this automatically, so here is a simple script for toggling the name of the selected record between upper case and lower case (with an initial cap).

(If no record is currently selected, it can also toggle the case of the selected group).

-- TOGGLE CASE OF NAME OF SELECTED RECORD OR GROUP
tell application id "DNtp"
	set oRec to content record
	if oRec is missing value then set oRec to current group
	tell oRec
		if it is missing value then return
		set strName to its name as Unicode text
		if (length of strName) > 1 then
			if my PatternMatch(strName, "^..*?[A-Z]") > 0 then
				set strCMD to "tr [:upper:] [:lower:] | perl -nle 'print ucfirst'" -- Lower case (with initial cap)
			else
				set strCMD to "tr [:lower:] [:upper:]" -- Upper case
			end if
			set its name to (do shell script "echo " & quoted form of (its name as string) & " | " & strCMD)
		end if
	end tell
end tell


on PatternMatch(strText, strPattern)
	try
		(do shell script "echo " & quoted form of strText & " | perl -ne 'if (m/(" & strPattern & ")/) {print \"$+[1]\"}'") as integer
	on error
		0
	end try
end PatternMatch

Super, I looked for something like this, thank you!

This script works for a single file selected. What would I have to change or add in the code to have it work on a multiple selection?

Thanks for listening!
Bernd

-- TOGGLE CASE OF NAME OF SELECTED RECORD OR GROUP
-- Originally posted by Rob Trew on 20110814
-- https://discourse.devontechnologies.com/t/toggling-the-case-of-record-names/12533/1
-- Modified 20130712 by korm: (1) add error handling; (2) all multiple selection

tell application id "DNtp"
	try
		if selection is {} then error "Select some records"
		set theSelection to selection
		repeat with thisItem in theSelection
			tell thisItem
				set strName to its name as Unicode text
				if (length of strName) > 1 then
					if my PatternMatch(strName, "^..*?[A-Z]") > 0 then
						set strCMD to "tr [:upper:] [:lower:] | perl -nle 'print ucfirst'" -- Lower case (with initial cap)
					else
						set strCMD to "tr [:lower:] [:upper:]" -- Upper case
					end if
					set its name to (do shell script "echo " & quoted form of (its name as string) & " | " & strCMD)
				end if
			end tell
		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
			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 PatternMatch(strText, strPattern)
	try
		(do shell script "echo " & quoted form of strText & " | perl -ne 'if (m/(" & strPattern & ")/) {print \"$+[1]\"}'") as integer
	on error
		0
	end try
end PatternMatch

You again! :smiley:

Thank you very much!