Newbie question: coerce record to string?

Sorry if this has been answered a million times before, but I was wondering if anyone could help me get a string representation of a record.

For example, I have the following code:

set r to {name:“tod”, age:27, cool:true, macs:2.5}
r

Obviously, Script Editor.app knows how to convert this record into a string, as the “Result” pane of Script Editor.app displays the record in a nicely formatted string just like the literal value above. How do I programatically produce that result from within AppleScript??? Seems like this would be a common need, but I can’t find anything about this is in the O’Reilly book or online.

i’d like to say

r as string

but that produces :

AppleScript Error:
Can’t make {name:“tod”, age:27, cool:true, macs:2.5} into type string.

DOH! Well someone is able to convert that record in to type string, or else it wouldn’t appear as text in the error dialog!!! Ah! Anyone know how to make AppleScript convert a record to a string representation?

Thanks!
iTod

This is from Hanaan’s book http://www.applescriptbook.com/

set my_records to {dog:"Rufus", cat:"Fluff"}
try
	my_records as string
on error error_message
end try
--> Can't make {dog:"Rufus", cat:"Fluff"} into a string.
set text item delimiters to "Can't make "
set record_text to item 2 of text items of error_message
set text item delimiters to " into a string."
set record_text to item 1 of text items of record_text
set text item delimiters to ""
record_text
--> {dog:"Rufus", cat:"Fluff"}

Thanks! that’s what i needed. Good lord, that’s a mess tho… wish apple would do something about that. I would really think this is a somewhat common need…

It’s not great, but it does work pretty well. The book points out that records are quite a bit different than lists, but a record can be coerced to a list. Anyway, to clean things up a bit I wrapped the chunk of code in a handler and put it in a script library in case I ever need it.

(*
set coercion to load script file ((path to scripting additions from user domain as string) & "coercion.scpt")
set my_pets to {dog:"Rufus", cat:"Fluff"}
set my_pets to coercion's record_as_string(my_pets)
*)
on record_as_string(my_records)
	try
		my_records as string
	on error error_message
	end try
	set text item delimiters to "Can't make "
	set record_text to item 2 of text items of error_message
	set text item delimiters to " into type string."
	set record_text to item 1 of text items of record_text
	set text item delimiters to ""
	return record_text
end record_as_string

Good idea. Thanks again!

Model: PowerBook (latest)
AppleScript: 1.10
Browser: Safari 412.2
Operating System: Mac OS X (10.4)

I know this is an old post but was amazed when I was this solution for Record ”> String coercion… However, nowadays, this little dandy doesn’t work :/, so I’m bumping this to the forefront again by asking:

Is there is a solution something like this for the latest AppleScript verion to coerce a record into a string?

I’ve always been wanting to do this and have never found a solution… I have found lot’s of “it’s not possible” and “I’m not using AppleScript properly” and “why do you want to do this?” and so forth. But this little bit of trick-code apparently did the job back in 2005… I wasn’t AppleScripting way back then and missed out on those glory days of record label manipulaton.

Aesthir

Most times we already know the keys in the record, so something as simple as this works…

set theRecord to {dog:"Rufus", cat:"Fluff"}
set recordString to "{dog:" & dog of theRecord & ", " & "cat:" & cat of theRecord & "}"

However, if we don’t know that then we can always use a little cocoa method called description to get a string from a record. You may want to clean the string returned by the description method up a bit…

set theRecord to {dog:"Rufus", cat:"Fluff"}
tell application "Automator Runner"
	set recordString to call method "description" of theRecord
end tell

And if you want to use the error method as shown above, here’s a safer way to do that…

set theRecord to {dog:"Rufus", cat:"Fluff"}
recordToString(theRecord)

on recordToString(theRecord)
	try
		return theRecord as text
	on error theError
		set tids to text item delimiters
		set text item delimiters to "{"
		set a to text item 2 of theError
		set text item delimiters to "}"
		set b to text item 1 of a
		set text item delimiters to "\""
		set c to text items of b
		set text item delimiters to ""
		set d to "{" & (c as text) & "}"
		set text item delimiters to tids
		return d
	end try
end recordToString

Safer still, in case any of the values are themselves records or lists, replace ‘text item 2’ with ‘text from text item 2 to -1’ and ‘text item 1’ with ‘text 1 thru text item -2’.

I myself wouldn’t regard stripping out quotes as a true representation of a record “coerced” to text ” but then records aren’t supposed to be coerced to text anyway.

set theRecord to {|"dogs"|:{"Rufus"}, cats:{"Fluff"}}
recordToString(theRecord)

on recordToString(theRecord)
	try
		return theRecord as text
	on error theError
		set tids to text item delimiters
		set text item delimiters to "{"
		set a to text from text item 2 to -1 of theError
		set text item delimiters to "}"
		set b to text 1 thru text item -2 of a
		set d to "{" & b & "}"
		set text item delimiters to tids
		return d
	end try
end recordToString

Thank you Nigel! :smiley: Thank you regulus! :smiley: I have no idea what methods are out there and where to find out what they do when used with AppleScript. Apple’s documentations are great but a bit technical for readers who don’t know the first thing about obj-C. It would be nice to have a list of methods that produce nice effects like this one with explanations intended for AppleScript users. I’ve never found such a documentation… if you guys know of something, let me know.

The reason I needed to know the record labels is because I wrote a shell script that returns some (or all) the properties of some target(s) entered. It returns either the record id, label, value, or any combination…

With some playing around, I figured out another way to get record labels:

return do shell script ("osascript -ss -l 'AppleScript' -e 'tell application \"BBEdit\" to return properties  of  front  window as record'")

The beauty of this method is it returns all record labels. regulus’s solution works, but when I try to return labels from a “get properties” call, it only returns about 10-20% of the record entries. Nigel’s solution works much, much better, returning all record entries, but has the drawback of much of the record labels and values containing chevron syntax, which has to be dealt with… and is probably a real pain to write up a script to find the proper dictionay, search it’s entries, and decode it’s “«class this»” and “«constant that»” garbage.

The method I have is has the drawback of being slower in execution time… but the results are perfect, everytime (even entries containing records, which have entries containing records, and so on to infinitum).

Thanks for all the help guys!

Aesthir

A quick and simple way of getting all the keys and values into some kind of structured string is:

set recSample to {title:"Record to string coercion", status:"rough draft", author:"houthakker", date:(current date), ver:"0.1"}

Rec2String(recSample)

on Rec2String(recAny)
	set the clipboard to recAny
	do shell script "osascript -e 'the clipboard as record'"
end Rec2String