Re: Building applescript records with lists, accessing applescript records with strings
Re: Building applescript records with lists, accessing applescript records with strings
- Subject: Re: Building applescript records with lists, accessing applescript records with strings
- From: Malcolm Fitzgerald <email@hidden>
- Date: Fri, 4 Feb 2005 17:34:38 +1100
On 04/02/2005, at 2:52 PM, Takaaki Naganoya wrote:
Hmm... It is interesting as an afternoon exercise.
How about this ?
-------
set s_footer to "set target_file to (((path to desktop) as text) &
¥"temprec¥")
set the open_target_file to open for access file target_file with write
permission
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
do shell script ¥"sync¥"
"
set read_file to (((path to desktop) as text) & "temprec")
(* build an applescript record from strings *)
set theStrings to {"x", "y", "z"}
set theValues to {3, 2, 1}
set s_header to "set this_data to {"
set recList to {}
set s to ""
repeat with i from 1 to (count theStrings)
set s to s & ((item i of theStrings as text) & ":" & item i of
theValues
as text)
if i is not (count theStrings) then
set s to s & ","
end if
end repeat
set s to s_header & s & "}" & return & s_footer
run script (s as text)
set the end of recList to read file read_file as record
set s to "" --clean up
--> {x:3, y:2, z:1}
Very nice!
The point of my attempt was to access a record using a string value. In
other words, provide a more flexible way of doing this: do script ("y
of {x:3,y:2,z:1}")
for example:
set theKeys to {"a","b","c","d"}
set theValues to {97,98,99,100}
GetHashValue(item 2 of theKeys, BuildHash(theKeys,theValues))
-- 98
The drawback of these string to record tricks is that you cannot easily
set values in the list. It's only really convenient for getting data.
Also, the do script method is comparatively slow. To get and set it's
easier to handle paired lists (wasn't Arthur doing this a few years
ago?). These are much faster but they don't have the same cachet.
(*
-- build some data
set theKey to "a"
set theValue to "ha ha"
set theValueList to {}
set theKeyList to {}
repeat with i from 97 to 122
set end of theValueList to i
set end of theKeyList to ASCII character i
end repeat
*)
GetItem(theKey, theKeyList, theValueList)
--> 97
SetItem(theKey, theValue, theKeyList, theValueList)
--> {"ha ha", 98, 99, 100, ... }
GetItem(theKey, theKeyList, theValueList)
--> "ha ha"
on GetItem(theKey, theKeyList, theValueList)
set my text item delimiters to ASCII character 8
set delimKey to {"", theKey, ""} as string
set delimKeyList to {"", theKeyList, ""} as string
set n to offset of delimKey in delimKeyList
return item ((count (text items of (text 1 thru n of delimKeyList))) -
1) of theValueList
end GetItem
on SetItem(theKey, theValue, theKeyList, theValueList)
set my text item delimiters to ASCII character 8
set delimKey to {"", theKey, ""} as string
set delimKeyList to {"", theKeyList, ""} as string
set n to offset of delimKey in delimKeyList
set item ((count (text items of (text 1 thru n of delimKeyList))) - 1)
of theValueList to theValue
return theValueList
end SetItem
On 05.2.4 11:44 AM, "Malcolm Fitzgerald" <email@hidden> wrote:
(* build an applescript record from strings *)
set theStrings to {"x", "y", "z"}
set theValues to {3, 2, 1}
set s to {}
repeat with i from 1 to (count theStrings)
set s to s & (do script ("{" & item i of theStrings & ":" & item i of
theValues & "}"))
end repeat
--> {x:3, y:2, z:1} (a record)
The problem not that Applescript cannot or doesn't do it. It plainly
does. The problem that AppleScript doesn't make it easy to do.
The most obvious problem is that you cannot access these variables or
records from outside the "do script" unless you can ask them for their
values. To do that you need to ask using their variables. Catch 22!
However, if you're able to pass the query into "do script" at the same
time as you pass the data in you are able to get the results you'd
expect.
The sub-routines below provide a way to use strings and lists as
records. They may appeal to those of you that don't have better ways
to
do the same thing.
(*
-- build some data
-- it was Jason Bourque's item pricing problem that started this
set theItemPrices to {}
set theItemLabels to {}
repeat with i from 97 to 122
set end of theItemPrices to i
set end of theItemLabels to ASCII character i
end repeat
*)
-- build the hash
set theHash to BuildHash(theItemLabels, theItemPrices)
-- select a key
set theKey to item (random number from 1 to 27) of theItemLabels -- a
string selected at random
-- example 1
GetHashValue (theKey, theHash)
-- example 2
set priceList to {}
repeat with theVar in theItemLabels
if theVar is in {"j", "m", "t"} then set the end of priceList to
GetHashValue(theVar, theHash)
end repeat
get priceList
--
--
on BuildHash (theKeys, theValues)
-- returns a string which the compiler can cast to a record
set s to {}
repeat with i from 1 to (count theKeys)
set end of s to ","
set end of s to item i of theKeys
set end of s to ":"
set end of s to item i of theValues
end repeat
set s to (rest of s) as string
return "{" & s & "}"
end BuildHash
on GetHashValue (theKey, theHash)
return do script (theKey & " of " & theHash)
end GetHashValue
Malcolm Fitzgerald ph: 02 93180877
Database Manager fax: 02 93180530
The Australian Society of Authors www.asauthors.org
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden