Posted on 12-26-2024 12:13 PM
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."
Posted on 12-26-2024 01:10 PM
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.
Posted on 12-26-2024 01:35 PM
How do I configure the policy in Jamf to run at user level, don't see any option for this?
Posted on 12-27-2024 05:46 AM
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.
Posted on 12-27-2024 06:53 AM
So then how do I "try to run the script as the user" as you suggest?
Posted on 12-26-2024 01:15 PM
How do I configure the policy in Jamf to run at user level, I don't see any option for that?
Posted on 12-29-2024 10:37 PM
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"
Posted on 12-30-2024 04:06 PM
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"
Posted on 12-30-2024 04:26 PM
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).
Posted on 01-01-2025 01:31 PM
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