Skip to main content
Question

Set Keyboard Input Language

  • December 26, 2024
  • 9 replies
  • 6 views

Forum|alt.badge.img+6
  • Contributor
  • 13 replies

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

AJPinto
Forum|alt.badge.img+26
  • Legendary Contributor
  • 2734 replies
  • December 26, 2024

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.


Forum|alt.badge.img+6
  • Author
  • Contributor
  • 13 replies
  • December 26, 2024

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


Forum|alt.badge.img+6
  • Author
  • Contributor
  • 13 replies
  • December 26, 2024
AJPinto wrote:

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.


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


AJPinto
Forum|alt.badge.img+26
  • Legendary Contributor
  • 2734 replies
  • December 27, 2024
j120p wrote:

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


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.


Forum|alt.badge.img+6
  • Author
  • Contributor
  • 13 replies
  • December 27, 2024
AJPinto wrote:

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.


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


Shyamsundar
Forum|alt.badge.img+13
  • Jamf Heroes
  • 296 replies
  • December 30, 2024

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"

Forum|alt.badge.img+6
  • Author
  • Contributor
  • 13 replies
  • December 31, 2024
Shyamsundar wrote:

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"

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"

 


Forum|alt.badge.img+6
  • Author
  • Contributor
  • 13 replies
  • December 31, 2024

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).


Forum|alt.badge.img+6
  • Author
  • Contributor
  • 13 replies
  • January 1, 2025

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings