Re: hash arrays and other mysteries
Re: hash arrays and other mysteries
- Subject: Re: hash arrays and other mysteries
- From: Arthur J Knapp <email@hidden>
- Date: Tue, 16 Oct 2001 18:25:12 -0400
>
Date: Tue, 16 Oct 2001 17:58:28 +0100
>
From: has <email@hidden>
>
Subject: hash arrays and other mysteries
>
Plunging straight into the deep end (this is what happens when you start
>
sniffing around other languages...), I'd very much like to use hash arrays
>
(that's the correct term, yes?) or their nearest sensible equivalent in
>
Applescript.
We've just had a very lengthy discussion about this on the other list:
<
http://listserv.dartmouth.edu/scripts/wa.exe?A0=macscrpt&D=1&H=0&O=D&T=1>
>
So now I've got this, which uses Smile to cook the books a bit:
>
makehash({{"a", 1}, {"b", "hi"}, {"c", false}})
>
>
on makehash(array)
>
set str to ""
>
repeat with eachitem in array
>
set str to str & item 1 of eachitem & ":\"" & item 2 of
>
[NO-BREAK]eachitem & "\" as " & class of item 2 of eachitem & ", "
>
end repeat
>
tell application "Smile" to do script "{" & (characters 1 thru -3
>
[NO-BREAK]of str) & "}"
>
end makehash
Nice. :)
>
And it's not vanilla either, but it's the closest I've managed to get. Has
>
anyone got any other ideas?
-- 1) The labels, (keys), and values all have to be strings.
-- 2) The labels are case-sensitive, "john" and "John" are different.
script LabeledStrings
property kDelim : ASCII character 1
property sData : ""
on GetString(the_label)
set oldDelim to text item delimiters
set text item delimiters to kDelim & the_label & kDelim
if ((count of text items in sData) = 3) then
set str to text item 2 of sData
else
set str to ""
end if
set text item delimiters to oldDelim
return str
end GetString
on SetString(the_label, str)
set oldDelim to text item delimiters
set text item delimiters to kDelim & the_label & kDelim
if ((count of text items in sData) = 3) then
set tempData to text items of sData
set item 2 of tempData to str
set sData to tempData as string
else
set sData to sData & ,
kDelim & the_label & kDelim & str & ,
kDelim & the_label & kDelim
end if
set text item delimiters to oldDelim
return str
end SetString
end script
copy LabeledStrings to theBeatles -- new LabeledStrings object
tell theBeatles
SetString("John", "1654")
SetString("Paul", "5064")
SetString("George", "6742")
SetString("Ringo", "1191")
GetString("John") --> "1654"
GetString("Paul") --> "5064"
GetString("George") --> "6742"
GetString("Ringo") --> "1191"
end tell
>
Last but not least, does anyone know if it's possible to bounce between
>
using AS and the sparkly new JavaScript OSA within a script? e.g. By
>
writing a script server using AS, then calling its handlers from a
>
javascript. Or vice-versa, to allow an applescript to use a JS-based script
>
server. Lots of interesting possibilities there, assuming it can be done...
You can check out this posting of mine that centers on this basic idea:
<
http://groups.yahoo.com/group/jsosa/message/268>
Arthur J. Knapp
http://www.stellarvisions.com
mailto:email@hidden
Hey, check out:
http://www.zavatone.com/