whateverToText

This handler will convert any thing you pass as parameter to a string (where “thing” means “applescript thing”).
Much more simple than R23’s CoerceToString and, though not extensivelly tested, apparently works fine and speedy…
This handler returns “text” in the long meaning of the word “text”. As far as the handler doesn’t know if you prefer as output string (mac-ascii), Unicode text, styled text, etc. as result, It will return various kinds of text, and you will handle the result… If you provide “whatever” to the handler, man, it will also return to you whatever text!!!
Tested only under AS 1.9.3, may (not) work in all systems.

OS version: OS X

--> VARIOUS SAMPLES OF USAGE:

(*
set rec to "string" --> string
set rec to 4.6 --> number
set rec to current date --> date
set rec to {1, "a", {x:2}} --> list
set rec to {a:2, b:{1, 2, 3}, c:"d"} --> record
tell application "TextEdit" to set rec to first window --> TextEdit's object
tell application "Finder" to set rec to first item of desktop --> Finder's object
set rec to «data utxt0065» --> unicode text
set rec to whateverToText --> handler
script ha
	property foo : "bar"
end script
set rec to ha --> script object
set rec to real --> class
set rec to «class unkn» --> raw class

--> and the undefined value, which is the only I know can't be handled by this handler ;-)
set rec to (run script "«class undf»")

whateverToText(rec)
*)

--> and, here, the handler:
on whateverToText(x)
	try
		if class of x is in {string, Unicode text} then
			try
				count x
				return x
			on error msg --> handle data string/utxt
				try
					x * 5
				on error msg
					return text (offset of "«" in msg) thru (offset of "»" in msg) of msg
				end try
			end try
		else
			tell application x to beep
		end if
	on error msg
		set msg to text ((offset of "application " in msg) + 12) thru -2 of msg
		if msg starts with "(" and msg ends with ")" then --> unknown object
			return text 2 thru -2 of msg
		else
			return msg
		end if
	end try
end whateverToText

property |version| : 1.0
property author : "Pescados Software @ pescadosweb.com"
property |date| : date "jueves, 3 junio 2004 15:53:37"
property license : "freeware, open-source"

This won’t compile on an English machine unless the line:

property |date| : date "jueves, 3 junio 2004 15:53:37"

is changed to:

property |date| : date "Thursday, June 3, 2004 3:53:37 PM"

Users should also be aware that there result it a list of Unicode text for an “info for file” conversion and may not be easily parsed in vanilla AppleScript.