Re: Calling a property by using a string
Re: Calling a property by using a string
- Subject: Re: Calling a property by using a string
- From: has <email@hidden>
- Date: Mon, 25 Sep 2006 23:18:07 +0100
Chris Tangora wrote:
I am trying to get this to work, and I was hoping to have someone
help me figure out the small detail I am missing. The idea is this,
take two strings, put them together and have it be the name of a
previously declared property.
This would be bad practice. What you should do is use an associative
array, which allows you to look up values using other values as keys.
e.g.:
#!/usr/bin/python
Tests = {"Average": "Please Don't Work", "Good": "Shouldn't Work",
"Great": "This worked"}
this = "Great"
print Tests[this]
# This worked
AS doesn't provide an associative array type as standard,
unfortunately, so you'll have to provide your own, e.g. AppleMods
Types library <http://applemods.sourceforge.net/mods/Data/Types.php>
provides a couple suitable objects. I notice the download links at
AppleMods are broken though (Gary?), so I've pasted the code for
creating AssociativeList objects below. It's rather inefficient if
you've large numbers of items to store (the Dictionary object is
better for those), but for small collections it'll do the job:
----------------------------------------------------------------------
--PRIVATE
script _ConsiderCase
on do(obj, params)
considering case, diacriticals, expansion, hyphens, punctuation and
white space
return obj's do(params)
end considering
end do
end script
script _IgnoreCase
on do(obj, params)
considering diacriticals, expansion, hyphens, punctuation and white
space but ignoring case
return obj's do(params)
end considering
end do
end script
--
on _makeAssoc(caseObj)
script
property class : "AssociativeList"
script _k
property k : {} -- keys
property v : {} -- values
end script
property _caseObj : caseObj
--
script _doOffset
on do({theKey, kludge})
if kludge's k does not contain {theKey} then error "key not
found." number -1728
repeat with idx from 1 to count of kludge's k
if kludge's k's item idx is theKey then return idx
end repeat
end do
end script
script _doExists
on do({theKey, kludge})
return (kludge's k contains {theKey})
end do
end script
--
on _findOffset(theKey)
_caseObj's do(_doOffset, {theKey, _k})
end _findOffset
-------
on countItems()
return count _k's k
end countItems
on itemExists(theKey)
_caseObj's do(_doExists, {theKey, _k})
end itemExists
on setItem(theKey, theValue)
try
if itemExists(theKey) then
set _k's v's item _findOffset(theKey) to theValue
else
set _k's k's beginning to theKey
set _k's v's beginning to theValue
end if
return
on error eMsg number eNum
error "Can't setItem: " & eMsg number eNum
end try
end setItem
on getItem(theKey)
try
return _k's v's item _findOffset(theKey)
on error eMsg number eNum
error "Can't getItem: " & eMsg number eNum
end try
end getItem
on deleteItem(theKey)
try
set keyOffset to _findOffset(theKey)
set theValue to _k's v's item keyOffset
if keyOffset is 1 then
set _k's k to get rest of _k's k
set _k's v to get rest of _k's v
else if keyOffset is (count of _k's k) then
set _k's k to get items 1 thru -2 of _k's k
set _k's v to get items 1 thru -2 of _k's v
else
set _k's k to get ((items 1 thru (keyOffset - 1) of _k's k) & ¬
(items (keyOffset + 1) thru -1 of _k's k))
set _k's v to get ((items 1 thru (keyOffset - 1) of _k's v) & ¬
(items (keyOffset + 1) thru -1 of _k's v))
end if
return theValue
on error eMsg number eNum
error "Can't deleteItem: " & eMsg number eNum
end try
end deleteItem
on getKeys()
return _k's k's items
end getKeys
on getValues()
return _k's v's items
end getValues
end script
end _makeAssoc
----------------------------------------------------------------------
--PUBLIC
on makeAssociativeList()
return _makeAssoc(_IgnoreCase)
end makeAssociativeList
on makeAssociativeListConsideringCase()
return _makeAssoc(_ConsiderCase)
end makeAssociativeListConsideringCase
----------------------------------------------------------------------
--EXAMPLE
set Tests to makeAssociativeList()
Tests's setItem("Average", "Please Don't Work")
Tests's setItem("Good", "Shouldn't Work")
Tests's setItem("Great", "This worked")
Tests's getItem("Great") --> "This worked"
----------------------------------------------------------------------
HTH
has
--
http://freespace.virgin.net/hamish.sanderson/
http://appscript.sourceforge.net
_______________________________________________
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