I’m returning from Applescript after a long layoff and finding that what limited knowledge I had of the language seems to have entirely deserted me. I’m failing to achieve something that I’m sure is relatively simple and can’t find a solution either on here or anywhere else.
I want to give the user a list of countries to choose from with multiple selections allowed. I then want to use the results to name a folder. The thing is, I want to shorten the country names into codes, so Denmark becomes DK, Finland becomes FI etc.
So the user is presented with a list that looks like this:
set whichCountry to choose from list {"Global ", "Australia ", "Australia and New Zealand ", "Denmark ", "Finland ", "Netherlands ", "New Zealand ", "Norway ", "Republic of Ireland ", "Spain ", "Sweden ", "United Kingdom "} with prompt "Select country (Hold CMD key to select multiple)" with title "New Job Setup" with multiple selections allowed
And if they select New Zealand, Norway and Spain, the string returned will be NZ NO SP
I just can’t work out how to make this substitution. Again, apologies if this is simple. I’m just drawing a blank. Thanks in advance for any help anyone can offer.
set mainList to {"Global ", "Australia ", "Australia and New Zealand ", "Denmark ", "Finland ", "Netherlands ", "New Zealand ", "Norway ", "Republic of Ireland ", "Spain ", "Sweden ", "United Kingdom "}
set shortenList to {"GL", "AU", "AZ", "DK", "FI", "NE", "NZ", "NO", "IR", "SP", "SW", "UK"}
set whichCountry to choose from list mainList with prompt "Select country (Hold CMD key to select multiple)" with title "New Job Setup" with multiple selections allowed
set shortenChoice to {}
repeat with aCountry in whichCountry
repeat with i from 1 to count mainList
if (aCountry as text) = item i of mainList then
set end of shortenChoice to item i of shortenList
exit repeat
end if
end repeat
end repeat
shortenChoice
As I am curious I wonder why you inserted a space at the end of every entry in the main list.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 9 mai 2019 17:36:43
use framework "Foundation"
use scripting additions
set countries to {GL:"Global", AU:"Australia", ANZ:"Australia and New Zealand", DK:"Denmark ", FI:"Finland", NL:"Netherlands", NZ:"New Zealand", |NO|:"Norway", IE:"Republic of Ireland", ES:"Spain", SE:"Sweden", UK:"United Kingdom"}
set whichCountry to (choose from list countries as list with prompt "Select country (Hold CMD key to select multiple)" with title "New Job Setup" with multiple selections allowed)
if whichCountry ≠ false then repeat with country in whichCountry
set country's contents to ((current application's NSDictionary's dictionaryWithDictionary:countries)'s allKeysForObject:country) as text
end repeat
whichCountry
Worked like a charm. Thanks so much for your help!
The spaces were because I wanted the country codes to appear with spaces between them in the outputted string when multiples were selected i.e. DK NO NZ, as opposed to DKNONZ. It seemed like a logical way to do it.