Hi all,
I have a script that takes the first four characters of a filename and sets it to a variable called pub_code what i then need to do is correlate the pub_code with an email address, in the past of done this by using else if:
set pub_code to "abus"
if pub_code = "cmot" then
set email_address to "steved@address.com"
else if pub_code = "twek" then
set email_address to "lauraw@address.com"
else if pub_code = "abus" then
set email_address to "nikj@address.com"
end if
but the list of else if’s is getting very long so I wondered if it would be better to use a record e.g
set pub_code to "abus"
set email_lookup to {cmot:"steved@address.com", twek:"lauraw@address.com", abus:"nikj@address.com"}
set email_address to email_lookup's pub_code
The problem is that when I define the pub_code I then need the email_address record to recognize the string as a record entry and I can’t see how to do this. Is there a way of doing this or am I barking up the wrong tree?
Thanks,
Nik
You can’t use records that way, since their labels aren’t text and you have to know what label you want to access when writing the script. You could use a list of lists, though; something like this:
set pub_code to "abus"
set email_lookup to {{"cmot", "steved@address.com"}, {"twek", "lauraw@address.com"}, {"abus", "nikj@address.com"}}
repeat with this_entry in email_lookup
if (item 1 of this_entry is pub_code) then
set email_address to item 2 of this_entry
exit repeat
end if
end repeat
-- display dialog email_address
Addendum: Sorry. I was forgetting about ‘run script’, which allows you to do such things textually. However, it could be a little slower as it has to compile the text into a script each time it’s used:
set pub_code to "abus"
-- A text representation of a record. NB the escaped quotes round the record's text values.
set email_lookup to "{cmot:\"steved@address.com\", twek:\"lauraw@address.com\", abus:\"nikj@address.com\"}"
-- Compile and run a script that returns the relevant text value.
set email_address to (run script (email_lookup & "'s " & pub_code))