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: Takaaki Naganoya <email@hidden>
- Date: Fri, 04 Feb 2005 12:52:54 +0900
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}
--------
--
Takaaki Naganoya
email@hidden
http://www.appleco.jp/piyomarusoft/
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
>
_______________________________________________
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