Re: resetting record values to ""
Re: resetting record values to ""
- Subject: Re: resetting record values to ""
- From: Richard 23 <email@hidden>
- Date: Tue, 2 Jan 2001 01:43:51 -0800
>
I don't know a shorter way, not even
>
set yourRec's {a,b,c,d} to {"", "", "", ""} <--ERROR
>
>
So prepare ahead and just make the variable a short one, like 'r', instead
>
of 'yourRec'!
>
>
Maybe richard 23 has some crazy shorter way?
hey I resemble that remark.
i don't know a shorter way or a better way just an alternative way.
set yourRec to {a:"they're", b:"coming", c:"to", d:"take", e:"me",
f:"away"}
-- If you tell a record nicely it will be happy to comply.
tell yourRec to set {its a, its b, its c, its d, its e, its f}
to {"", "", "", "", "", ""}
return yourRec
--> {a:"", b:"", c:"", d:"", e:"", f:""}
But the voices tell me that our friend may be wanting to set properties
of a record where the labels are not known ahead of time. That would
require a more cunning approach. Something like which might be done
over at the One Step Beyond site (
http://homepage.mac.com/richard23/).
First you would need a string literal representation of the record in
question. This can be obtained from my CoerceToString handler.
(
http://homepage.mac.com/richard23/)
Then using text item delimiters and little luck, the following will
extract the labels from the string literal.
property |t/d|:a reference to AppleScript's text item delimiters
-- ----------------------------------------------------------------------
-- GetLabelsOfRecord(theRecord) Formatting 1.0d432 (c) 2000 by R23
-- ----------------------------------------------------------------------
-- returns property labels (as a list of strings) given record theRecord
-- ----------------------------------------------------------------------
-- extracts and returns labels from a record, returning a list of strings
-- method: coercing to string, remove strings and nested lists and
records
-- (otherwise string may contain confusing delimiter characters), finally
-- splitting as the comma and colon delimiters. excessive, but effective
--
-----------------------------------------------------------------------
on GetLabelsOfRecord(theRecord)
log {|GetLabelsOfRecord\r\t\t|:theRecord}
set labelList to {}
if theRecord's class = record then
-- convert to string, remove wrapping braces
-- encode backslashed quotes
set {|t/d|'s contents} to {"\\\"", text 2 thru -2 of
CoerceToString(theRecord)}
set {|t/d|'s contents} to {"~Q~", text items of result's end}
set {|t/d|'s contents} to {"\"", result's end as string}
-- split string at quotes, result alternates normal & quoted.
-- toss quoted strings.
set tempList to text items of result's end
tell tempList to repeat with theIdx from 2 to length by 2
set item theIdx to ""
end repeat
-- rejoin list, split at opening brace,
-- set delimiter to closing brace.
set {|t/d|'s contents} to {"{", tempList as string}
set {|t/d|'s contents, tempList} to {"}", text items of result's
end}
-- eliminate nested records and lists, only single level wanted
set theDepth to 0
repeat with theItem in tempList
tell theItem to if it contains "}" then
set {theDepth, contents} to {theDepth -
(count text items) - 2, "{}" & last text item}
else
if theDepth > 0 then set contents to ""
set theDepth to theDepth + 1
end if
end repeat
-- rejoin the list. split properties. split items.
-- result alternates names & values
set {|t/d|'s contents} to {""}
set {|t/d|'s contents} to {":", tempList as string}
set {|t/d|'s contents} to {", ", text items of result's end}
set {|t/d|'s contents, tempList} to {"", text items of
(result's end as string)}
-- get and return the property labels (odd elements)
tell tempList to repeat with theIdx from 1 to length by 2
set end of labelList to item theIdx
end repeat
else
-- not a record, generate list of empty strings (placeholders)
repeat (length of (theRecord as list)) times
set end of labelList to ""
end repeat
end if
labelList
end GetLabelsOfRecord
After all that work a nice nap would be in order, but you're not out of
the woods yet. You can't set record properties directly from a list of
strings representing the labels. You then need to build and run a custom
script fragment to finish the job.
on ClearRecord(theRec)
set labelList to GetLabelsOfRecord(theRec)
set valueList to {}
repeat (labelList's length) times
set end of valueList to ""
end repeat
set {|t/d|'s contents} to {", its "}
set {|t/d|'s contents} to {"", labelList as string}
tell (run script "me
on f(theRec, theList)
tell theRec to set {its " & result's end & "} to theList
end f") to f(theRec, valueList)
end ClearRecord
And here it is again putting it all together:
on run
set yourRec to {a:"they're", b:"coming", c:"to", d:"take", e:"me",
f:"away"}
ClearRecord(yourRec)
yourRec
end run
--> {a:"", b:"", c:"", d:"", e:"", f:""}
Aren't you glad you asked? I could actually feel myself age while
assembling this message. Hope somebody gets something out of it....
R23