get language of currently logged in user via applescript

让人想犯罪 __ 提交于 2019-12-13 04:01:37

问题


I want to get the system language of the currently logged in user. The line

set lang to do shell script "defaults read NSGlobalDomain AppleLanguages"

returns an string, which looks like

(
    en,
    de,
    ja,
    fr,
    es,
    it,
    pt,
    "pt-PT",
    nl,
    sv,
    nb,
    da,
    fi,
    ru,
    pl,
    "zh-Hans",
    "zh-Hant",
    ko,
    ar,
    cs,
    hu,
    tr
)

returns the users languages, but how can I get the first one of this 'array'? Is there a possibility to parse this as an array an get its first value?


回答1:


There's a more direct method. Applescript has a command "system info" which returns a lot of useful information about the current user. Try this to see...

return system info

The information that will help you from that is "user locale". So you can get the language easily...

return user locale of (get system info)

Play around with this on different users and see if it gives you what you want.




回答2:


The thread is really old, but I just had the problem and this comes up pretty high in a Google search. For other readers, I want to add two comments:

There is a difference between the “return user locale of (get system info)” and the “defaults read NSGlobalDomain AppleLanguages” strategies: The first returns the value from the region settings, the latter the value from the language settings (both in “Language & Text”).

Region settings return a language and a region, separated by underscore. Language settings return either just a language or language and region, separated by hyphen. If you use that in other code, make sure, it is tolerant.

The second point: GREPing works, but the simplest code I found is using the Property List suite from System Events:

on get_language()
    set lang to do shell script "defaults read NSGlobalDomain AppleLanguages"
    tell application "System Events"
        set pl to make new property list item with properties {text:lang}
        set r to value of pl
    end tell
    return item 1 of r
end get_language 

grep my be faster, but this requires less brain twisting.

Jürgen




回答3:


You can use awk and grep to prepare the list a little (get rid of indentation, quotes and parentheses), then split the resulting string:

-- a standard split function
to split of aString by sep
    local aList, delims
    tell AppleScript
        set delims to text item delimiters
        set text item delimiters to sep
        set aList to text items of aString
        set  text item delimiters to delims
    end tell
    return aList
end split

-- pipe the output of defaults through a few more commands
set cmd to "defaults read NSGlobalDomain AppleLanguages | awk '{gsub(/[^a-zA-Z-]/,\"\");print}' | grep -v '^$'"
set langs to do shell script cmd

-- get the first item in the list
set lang to item 1 of (split of langs by return)

The shell command gives you a list like:

en
da
ja
fr
de
es
it
pt
pt-PT
nl
sv
nb
fi
ru
pl
zh-Hans
zh-Hant
ko

So item 1 will be en




回答4:


By now this thread is ancient, but still the most relevant one of all results to my search entry. So I think this may be relevant to others searching for an answer to this question.

Based on what user1635960 wrote, the following “one liner” appears quite simple to me:

set lang to first word of (do shell script "defaults read NSGlobalDomain AppleLanguages")

As my first system language is German, it simply returns de.

Explanation:

do shell script "defaults read NSGlobalDomain AppleLanguages"

returns a list like

"(
    \"de-DE\",
    \"nl-DE\",
    \"en-DE\"
)"

The class word defines a sequence of mainly letters and/or numbers, delimited by one or more of most other characters. So first word of the above list results in de.



来源:https://stackoverflow.com/questions/7160462/get-language-of-currently-logged-in-user-via-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!