get language of currently logged in user via applescript - macos

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?

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.

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

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

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.

Related

How do I prevent zsh from replacing "%5D" with "]"?

[EDIT] Solved.
The actual issue was a typo in one of the handler variables, and not a zsh issue, as I initially thought.
I am trying to prevent zsh from replacing the code "%5D" for the ] right square bracket with the actual character "]".
For my purposes - calling the last.fm API via curl in shell script, to get info about a track from Apple Music - it needs to stay as "%5D", because otherwise the API doesn't recognise it.
I have tried escaping the "%5D" with backslashes or a second "%" but that doesn't work, it still gets replaced with "]"
Here's an example of what I'm trying to do and what is not working:
The original string of the track's name:
"YESTODAY (Extended Version) [Bonus Track]"
How it needs to be spelled for the API to work:
"YESTODAY+(Extended+Version)+%5BBonus+Track%5D"
The whitespaces get replaced with "+" by my AppleScript handler.
The left [ stays as "%5B" (I guess because it is immediately followed by more letters and therefore zsh cannot recognise it as code and replace it).
The right ] is generally the last character of the track string, not followed by anything and thus "%5D" is recognised by zsh and written as "]" .
How do I fix this?
Any help is greatly appreciated :)
For reference:
The part of the AppleScript that is supposed to replace the "[ ]" with code.
(This is obviously not the complete script). If you run this, it replaces the [ ] correctly. The issue only arises once the entire API call is made with do shell script curl_command because of zsh interpreting the code.
set trackreplace to "YESTODAY (Extended Version) [Bonus Track]"
if trackreplace contains "[" then
set trackreplace to replaceChars("[", "%5B", trackreplace)
end if
if trackreplace contains "]" then
set trackeplace to replaceChars("]", "%5D", trackreplace)
end if
on replaceChars(find, replace, subject)
set savedelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set subject to text items of subject
set AppleScript's text item delimiters to replace
set subject to (subject as string)
set AppleScript's text item delimiters to savedelims
return subject
end replaceChars
It was not a zsh issue but actually just a typo in one of the handler variables (trackeplace instead of trackreplace), as pointed out by #GordonDavisson

Bash/Osascript Error: unexpected EOF while looking for matching `)'

I'm splitting a string using osascript (was working this way, not with bash), and assigning the resulting array to a bash variable, and continuing with my bash script. I'm doing it like so:
tempArrayApplications=$(osascript >/dev/null <<EOF
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set theArray to every text item of "$noSplitString"
set AppleScript's text item delimiters to oldDelimiters
return theArray
EOF)
However, the command line returns the error that it went to the end of the file without finding the matching ')'. However, when I don't assign a bash variable to the osascript output, everything works fine, so I know it's not a problem with the AppleScript section. I ran shellcheck, and it doesn't detect any errors, and the other solutions seem to be related to an unclosed quote or unescaped character, but I don't seem to have that problem. Clearly it's due to trying to assign it to a bash variable, but for the life of me I don't know what I'm doing wrong. Thanks for the help.
Have you paused to consider that you're taking a bash variable ($noSplitString); inserting this into an AppleScript that splits the text using / as the delimiter; executing this AppleScript inside a bash command (osascript); then storing its output (which actually gets destroyed) in another bash variable ($tempArrayApplications)...?
My inclination would be to remove the AppleScript altogether (3 out of its 5 lines are redundant, anyway), and create the array from the string within bash.
So, given this:
noSplitString="item 1/item 2/item 3"
Then simply do this:
IFS='/'
tempArrayApplications=($noSplitString)
Now $tempArrayApplications will be an array with three items, starting at index 0 and ending at index 2. You can echo a specific element in the array like this:
echo "${tempArrayApplications[1]}" # "item 2"
IFS is the bash equivalent of the AppleScript text item delimiters. It typically has a default value of ⎵\t\n (where ⎵ indicates a space character). More can be read about it in this article: Bash IFS: its Definition, Viewing it and Modifying it

Mac Contextual Menu for AFP Path Without Spaces

My team requires an Automator service that will allow us to copy file paths from within OS X Finder such that they always start with "afp://server-name/" instead of "/Volumes/server-name/" BUT ALSO replaces all spaces with "%20".
Right now, we are using the following code, which does replace the first space in a given string, but does not replace all spaces.
on run {input, parameters}
set output to {}
repeat with f in input
set end of output to replace(POSIX path of f, "/Volumes/Brand Design", "afp://nycp-afp01/Brand%20Design")
end repeat
set text item delimiters to linefeed
set the clipboard to (output as text)
end run
on replace(input, search, replace)
set text item delimiters to search
set ti to text items of input
set text item delimiters to replace
ti as text
end replace
Any help you can provide will be HUGELY appreciated; thanx!
A.
to get rid of spaces use your replace handler again Inside itself (last line).
my replace(ti as text, " ","%20")

Script to convert lower case characters into upper case is working differently as service action

I am trying a simple script as a service action in automator which performs this function:
Receives selected text in any application and replaces selected text
with the text containing capital letters
So I used this script:
on run {input, parameters}
set upperCaseString to ""
repeat with i in input
if (ASCII number i) > 96 and (ASCII number i) < 123 then
set upperCaseString to upperCaseString & (ASCII character ((ASCII number i) - 32))
else
set upperCaseString to upperCaseString & (ASCII character (ASCII number i))
end if
end repeat
return upperCaseString
end run
But I found this problem:
It was returning first letter of input as an upper case letter, eg.
input - lowercasetext, output - L, whereas the expected output was -
LOWERCASETEXT.
To check the problem I added this line of code in repeat loop:
display dialog i
and found that it is displaying complete text in place of single character at a time ,ie. in place of displaying l.. o.. w.. in lowercasetext it is displaying lowercasetext at once.
Can anyone suggest me why is it bugging me as service action while it is working fine in Apple Script Editor?
This works for a lot of languages:
on toUpper(s)
tell AppleScript to return do shell script "shopt -u xpg_echo; export LANG='" & user locale of (system info) & ".UTF-8'; echo " & quoted form of s & " | tr [:lower:] [:upper:]"
end toUpper
on toLower(s)
tell AppleScript to return do shell script "shopt -u xpg_echo; export LANG='" & user locale of (system info) & ".UTF-8'; echo " & quoted form of s & " | tr [:upper:] [:lower:]"
end toLower
When I run your script, I get the correct result. But one thing you may want to do is to explicitly coerce your result to text. The easiest way to do that would be at the end:
return upperCaseString as text
That may or may not do it for you, but you'll avoid a lot of frustration if you explicitly coerce data when there is a possibility of ambiguity.
Another (faster) way is to leverage the Unix tr (translate) command the via do shell script:
set upperCaseString to ¬
(do shell script ("echo " & input & " | tr a-z A-Z;"))
That's enough for 'English' language, but you can also add diacritical translation, like so
set upperCaseString to ¬
(do shell script ("echo " & input & " | tr a-zäáà A-ZÄÁÀ;"))
tr will translate anything to anything, so you can add any characters you may encounter and what you'd like them to translate to. A 'leet-speak' translator comes to mind.
You will get the same result in the AppleScript Editor if the input variable is set to a list. The input parameter of an Automator action is also a list, so your comparison isn't doing what you think. Note that text id's have obsoleted ASCII character and ASCII number commands - see the 10.5 AppleScript Release notes.
#Matt Strange:
You could also try:
set upperCaseString to ¬
do shell script "echo " & input & " | tr [:lower:] [:upper:]"
If you run 'man tr' on 'OS X 10.10' you may see that the character classes [:lower:] and [:upper:] should be used instead of explicit character ranges like 'a-z' or 'A-Z', since these may not produce correct results as it is explained there, on the manual page.

How to pass variables from ruby script into automator app?

So I have a script in ruby that obviously uses variables. Part of the script opens an app and runs an automator workflow. There are some variables in my ruby script that I need my workflow to use. Is this possible in anyway?
Are you able to:
a) Execute a command line program from Ruby
b) Save your Automator workflow as an application
If so, you should be able to run the open command, e.g. open test.app --args someArg. Or you could use the automator command, such as echo "someArg" | automator -i - test.app
Note that the entire Automator script will run once for each argument - try having ‘Speak Text’ as your first item to verify this.
To work with all the parameters in one go you need to actually pass just one and then split it up, e.g. open test.app --args "one|two|three|four" then something like
on run input
set myArray to my theSplit(input as string, "|")
set a to item 1 of myArray
set b to item 2 of myArray
set c to item 3 of myArray
set d to item 4 of myArray
display dialog "c is " & c
--do stuff
return str
end run
on theSplit(theString, theDelimiter)
-- save delimiters to restore old settings
set oldDelimiters to AppleScript's text item delimiters
-- set delimiters to delimiter to be used
set AppleScript's text item delimiters to theDelimiter
-- create the array
--set theArray to every text item of theString
set theArray to text items of theString
--display dialog theArray as string
-- restore the old setting
set AppleScript's text item delimiters to oldDelimiters
-- return the result
return theArray
end theSplit
However, the AppleScript only seems to work if it’s not the first action. If you need it as the first action, which you probably do, insert a Run Shell Script first that just passes on the arguments:
for f
do
echo “$f"
done
Yes, it is possible to pass parameters to a ruby script. Here is a nice tutorial:
http://ruby.about.com/od/rubyfeatures/a/argv.htm

Resources