Set Keyboard Input Language

j120p
New Contributor III

We tried this and several other approaches to no avail, are there any currently-working solutions to change the keyboard input language programatically?

#!/bin/bash

# Script to set the default keyboard layout to British

# Set the keyboard layout to British
/usr/bin/defaults write com.apple.HIToolbox AppleCurrentKeyboardLayoutInputSourceID -string "com.apple.keylayout.British"

# Clear the keyboard layout cache
/usr/bin/defaults delete com.apple.HIToolbox AppleEnabledInputSources

# Add the British keyboard layout to the list of enabled input sources
/usr/bin/defaults write com.apple.HIToolbox AppleEnabledInputSources -array-add '<dict><key>InputSourceKind</key><string>Keyboard Layout</string><key>KeyboardLayout ID</key><integer>2</integer><key>KeyboardLayout Name</key><string>British</string></dict>'

# Restart the HIToolbox process to apply changes
/usr/bin/killall -HUP SystemUIServer

echo "Default keyboard layout has been set to British." 

9 REPLIES 9

AJPinto
Esteemed Contributor

The default keyboard is something that is set on the user level not the global level. When you run a script as Jamf, it runs the script as root. Basically you are setting the default keyboard for root, try running the script as the user.

j120p
New Contributor III

How do I configure the policy in Jamf to run at user level, don't see any option for this?

AJPinto
Esteemed Contributor

You don't. Policies all run at the root level as this is where JAMF's binary runs. You can attempt to add user substitution in the script to try to force the commands to run in the user space, but the outcome is not always what you are looking for depending on what exactly you are trying to do.

j120p
New Contributor III

So then how do I "try to run the script as the user" as you suggest?

j120p
New Contributor III

How do I configure the policy in Jamf to run at user level, I don't see any option for that?

Shyamsundar
Contributor III

Use the below function to run your command as user 

 

#!/bin/bash

currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )

uid=$(id -u "$currentUser")

# convenience function to run a command as the current user
# usage:
#   runAsUser command arguments...
runAsUser() {  
	if [ "$currentUser" != "loginwindow" ]; then
		launchctl asuser "$uid" sudo -u "$currentUser" "$@"
	else
		echo "no user logged in"
		# uncomment the exit command
		# to make the function exit with an error when no user is logged in
		# exit 1
	fi
}

runAsUser "enter your Command Here"

j120p
New Contributor III

This does not appear to work, here is the error: 

Script result: sudo: defaults write ~/Library/Preferences/.GlobalPreferences.plist AppleICUForce24HourTime -bool TRUE: command not found

Here is the script

#!/bin/bash

currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )

uid=$(id -u "$currentUser")

# convenience function to run a command as the current user
# usage:
#   runAsUser command arguments...
runAsUser() {  
	if [ "$currentUser" != "loginwindow" ]; then
		launchctl asuser "$uid" sudo -u "$currentUser" "$@"
	else
		echo "no user logged in"
		# uncomment the exit command
		# to make the function exit with an error when no user is logged in
		# exit 1
	fi
}

runAsUser "defaults write ~/Library/Preferences/.GlobalPreferences.plist AppleICUForce24HourTime -bool TRUE"

 

j120p
New Contributor III

What is the best practice to use Composer to package a script to a) deliver it to a user folder so it can be run as the user and b) start its execution automatically? I have done a) but need help with b).

j120p
New Contributor III

We figured the above out now the only issue we're having is that we can't seem to set the input language to British-PC as opposed to british? This does not work:

#!/bin/bash

# Script to set the default keyboard layout to British

# Set the keyboard layout to British
/usr/bin/defaults write com.apple.HIToolbox AppleCurrentKeyboardLayoutInputSourceID -string "com.apple.keylayout.British-PC"

# Clear the keyboard layout cache
/usr/bin/defaults delete com.apple.HIToolbox AppleEnabledInputSources

# Add the British keyboard layout to the list of enabled input sources
/usr/bin/defaults write com.apple.HIToolbox AppleEnabledInputSources -array-add '<dict><key>InputSourceKind</key><string>Keyboard Layout</string><key>KeyboardLayout ID</key><integer>2</integer><key>KeyboardLayout Name</key><string>British-PC</string></dict>'

# Restart the HIToolbox process to apply changes
/usr/bin/killall -HUP SystemUIServer

echo "Default keyboard layout has been set to British."

exit 0