Get User Language Preferences

This script gets the user’s language preferences from System Preferences > International > Languages
and stored in ~/Library/Preferences/.GlobalPreferences.plist .
This script uses the Terminal command defaults.
The rest of the script can be used to parse Terminal output of a non-nesting list that doesn’t include the comma in its items.

set intllangs to do shell script "defaults read NSGlobalDomain AppleLanguages"
set intllangs to findAndReplace of intllangs from "(" to ""
set intllangs to findAndReplace of intllangs from ")" to ""
set intllangs to findAndReplace of intllangs from "\"" to ""
set oldtid to text item delimiters
set text item delimiters to ", "
set intllangs to text items of intllangs
set text item delimiters to oldtid
return intllangs

on findAndReplace of TheString from tofind to toreplace
	set ditd to text item delimiters
	set text item delimiters to tofind
	set textItems to text items of TheString
	set text item delimiters to toreplace
	if (class of TheString is string) then
		set res to textItems as string
	else --  if (class of TheString is Unicode text) then
		set res to textItems as Unicode text
	end if
	set text item delimiters to ditd
	return res
end findAndReplace

.or


set oldtid to text item delimiters
set text item delimiters to ", "
set intllangs to words of (do shell script "defaults read -g AppleLanguages") as text
set text item delimiters to oldtid
return intllangs

:wink:

3 points:

  1. -g | -globalDomain | NSGlobalDomain : any one of these can be used.
  2. We want a list of strings in the end, not a long string, so the “… as text” should be removed.
  3. (the main point):
    In my script, I’m removing (the leading and ending) parentheses and quotation marks (that enclose any string with a hyphen).
    Codes like “zh-Hans” (Sinplified Chinese) do not work with your script because “words of …” removes the hyphens and interprets it as a separator.
    For example, when the defaults command gives me
(en, "zh-Hans", "zh-Hant", ja, fr, de, es, it, nl, sv, nb, da, fi, pt, ko)

your script gives me

"en, zh, Hans, zh, Hant, ja, fr, de, es, it, nl, sv, nb, da, fi, pt, ko"

as a result. So your script does not always work.
I modified my original script:
a) I verified that an array (like AppleLanguages) always starts and ends with a parenthesis [an empty one shows up as ()]. I changed the way the script removes the parentheses.
b) I made it a handler and changes the name of the variable inside it to prevent ambiguity. I added the handler call at the end.
c) I changed NSGlobalDomain to -g, since you seem to prefer it.

on intllangs()
    set langs to do shell script "defaults read -g AppleLanguages"
    set oldtid to text item delimiters
    set text item delimiters to ""
    set langs to (characters 2 thru -2 of langs) as text
    set langs to findAndReplace of langs from "\"" to "" --"
    set text item delimiters to ", "
    set langs to text items of langs
    set text item delimiters to oldtid
    return langs
end intllangs

on findAndReplace of TheString from tofind to toreplace
    set ditd to text item delimiters
    set text item delimiters to tofind
    set textItems to text items of TheString
    set text item delimiters to toreplace
    if (class of TheString is string) then
        set res to textItems as string
    else --  if (class of TheString is Unicode text) then
        set res to textItems as Unicode text
    end if
    set text item delimiters to ditd
    return res
end findAndReplace

intllangs()

By the way I added –" to line 6 because MacSripter’s syntax highlighter sucks and this way I can “close the quote” so it doesn’t highlight the rest of the script like a quote.

hm, this script and also your first script returns a list including one string like

{" en, de, it, fr "}
and all language tokens have leading spaces

try this, I use it for a script to change languages with a keystroke


set langList to words of (do shell script "/usr/bin/defaults read -g AppleLanguages")
if langList contains "zh" then set langList to convertList(langList) -- convert list if chinese languages are installed

on convertList(theList)
    set i to 1
    set x to {}
    repeat while i < (count theList)
        if item i of theList is "zh" then
            set end of x to quote & item i of theList & "-" & item (i + 1) of theList & quote
            set i to i + 2
        else
            set end of x to item i of theList
            set i to i + 1
        end if
    end repeat
    return x
end convertList

My script gives me {"en", "zh-Hans", "zh-Hant", "ja", "fr", "de", "es", "it", "nl", "sv", "nb", "da", "fi", "pt", "ko"}
Your script gives me {"en", "\"zh-Hans\"", "\"zh-Hant\"", "ja", "fr", "de", "es", "it", "nl", "sv", "nb", "da", "fi", "pt"}
which has quotation marks in it.
Whoa, whoa whoa! You think “zh” is the only language that is followed by a hyphen?! Don’t include a list of exceptions!!! It’s unnecessary (there could be a lot of hyphen languages) and unreliable (Apple could always add more languages).

I don’t understand why my script would give you a list of a single string. I even ran the script again by clicking the link on this page and it returned a list of the languages.
Theories (sorted by likeliness):

  1. a different shell (I use bash.)
  2. a different version of defaults included with a different version of Mac OS X (I have 10.4.11)
  3. a different version of bash (type bash --version ; I have version 2.05b.0(1)-release.)
    To test these three, tell me the exact string returned when you type into Terminal: “defaults read -g AppleLanguages”
    Thanks!

the difference is the system version. The returned strings in Tiger and Leopard are different