RE: record to string - does this work in other System languages?
RE: record to string - does this work in other System languages?
- Subject: RE: record to string - does this work in other System languages?
- From: Scott Babcock <email@hidden>
- Date: Thu, 25 Mar 2010 21:06:12 +0000
- Thread-topic: record to string - does this work in other System languages?
This is what I've been using for years as my AppleScript "string-izer". This handles every AppleScript data type I've ever thrown at it. I also have a ref_ToString() handler that returns "friendly" text for application object references and a processConst() handler that returns "friendly" text and hexadecimal suite/enum codes for application constants.
Note that the raw_ToString() handler is functionally equivalent the previous examples in this thread.
All of this code relies on ugly hacks and undocumented behaviors to get the job done. A fairly simple scripting addition could probably achieve the same results, but I'm unable to install third-party osaxen in my scripting environment.
on ToString(input)
try
-- if input is list
if ((class of input) is list) then
error -- avoid default list-to-text coercion
-- otherwise, if input is Unicode text
else if ((class of input) is Unicode text) then
-- use input as-is
set output to input
else -- otherwise
-- if input is string
if ((class of input) is string) then
try -- is it really styled text?
set input to (<class ktxt> of (input as record))
end try
end if
-- try to coerce input to Unicode text
set output to (input as Unicode text)
end if
on error
-- use brute-force method
set output to my raw_ToString(input)
try
-- convert to Unicode text
set output to (output as Unicode text)
on error
-- when all else fails...
set output to my fix_ToString(output)
end try
end try
return output
end ToString
on raw_ToString(input)
local output, errmsg
local msgtext, msgbits, revbits, head, tail
try
-- force an error
{_:input} as Unicode text
on error msgtext
-- get error message characters
set msgbits to (characters of msgtext)
-- flip error message characters
set revbits to (reverse of msgbits)
-- get head of string representation of input
set head to ((my getListIndex(msgbits, ":")) + 1)
-- get tail of string representation of input
set tail to (-1 - (my getListIndex(revbits, "}")))
-- extract string representation of input
set output to (text head thru tail of msgtext)
end try
return output
end raw_ToString
on fix_ToString(input)
local oldDelims, msgbits, outbits
local output, thisChar, utxtChar
set oldDelims to AppleScript's text item delimiters
try
-- get input message characters
set msgbits to (characters of input)
set outbits to {}
repeat with itemRef in msgbits
set thisChar to (contents of itemRef)
try
set utxtChar to (thisChar as Unicode text)
on error
set utxtChar to ("?" as Unicode text)
end try
set (end of outbits) to utxtChar
end repeat
set AppleScript's text item delimiters to {""}
set output to (outbits as Unicode text)
on error
set output to "<ToString failed>" as Unicode text
end try
set AppleScript's text item delimiters to oldDelims
return output
end fix_ToString
on getListIndex(theList, theItem)
set listIndex to 0
repeat with theIndex from 1 to (length of theList)
set listItem to (item theIndex of theList)
if ((class of listItem) is Unicode text) then
-- desired format
else if ((class of listItem) is string) then
set listItem to (listItem as Unicode text)
end if
if (listItem is theItem) then
set listIndex to theIndex
exit repeat
end if
end repeat
return listIndex
end getListIndex
-----Original Message-----
Date: Thu, 25 Mar 2010 13:15:17 -0700
From: "Stockly, Ed" <email@hidden>
Subject: Re: coerce record to string - does this work in other System
languages?
To: Skeeve <email@hidden>, AppleScript Users
<email@hidden>
Message-ID: <C7D11265.3B200ķemail@hidden>
Content-Type: text/plain; charset="US-ASCII"
Yours didn't work for me, but this modification does:
HTH,
ES
on to_String(the_record)
try
{a:0} as string
on error cutoff
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"{a:0}"}
set startCut to length of first text item of cutoff
set endCut to length of last text item of cutoff
set AppleScript's text item delimiters to oldDelims
end try
try
the_record as string
on error the_record
set endCut to (the (length of the_record) - endCut)
set the_record to text startCut thru endCut of the_record
end try
return the_record -- as string
end to_String
On 3/25/10 12:48 PM, "Skeeve" wrote:
> Hi!
>
> I wrote this little handler to coerce a record into a string. It works
> with "German" as System language. Does it work with other languages too?
> Who can test?
>
> display dialog to_String(info for POSIX file "/tmp")
>
> on to_String(the_record)
> try
> {a:0} as string
> on error cutoff
> set cutoff to 4 - (length of cutoff)
> end try
> try
> the_record as string
> on error the_record
> set the_record to text 1 thru cutoff of the_record
> end try
> return the_record -- as string
> end to_String
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden