Why does my handler/subroutine not work in DEVONthink?

Based on the Add Suffix To Names script I have written a script to remove the extension from the visible name of a record:

tell application id "DNtp"
		set this_selection to the selection
		if this_selection is {} then error "Please select some contents."
		set res to display dialog "Remove extension from " & (count of this_selection) & " documents?"
		if button returned of res is "ok" then
			repeat with this_item in this_selection
				set current_name to the name of this_item
				set new_item_name to remove_extension(current_name as string)
				set the name of this_item to new_item_name
			end repeat
		end if
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

on remove_extension(this_name)
	if this_name contains "." then
		set this_name to ¬
			(the reverse of every character of this_name) as string
		set x to the offset of "." in this_name
		set this_name to (characters (x + 1) thru -1 of this_name) as string
		set this_name to (the reverse of every character of this_name) as string
	end if
	return this_name
end remove_extension

Activated in DEVONthink it returns always the error DEVONthink Pro got an error: Can’t continue remove_extension.

However when I run the subroutine remove_extension in a standalone script from Script Editor it works fine.

What’s going wrong? Type issue with this_selection?

Whenever you call a handler from within a tell block you need to say my handler(), not handler(), to let AppleScript know your script wants to use one of its own handlers. The ‘told’ application has no idea of what a handler is.
Can’t continue is the error message for this case. It does not apply to the code in the handler.

Thanks heaps alastor!

That was exactly the issue! :slight_smile: