Created
June 7, 2024 20:59
-
-
Save tedserbinski/820f70a13fda67304ef78e35e347583d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# template script for running a command as user | |
# The presumption is that this script will be executed as root from a launch daemon | |
# or from some management agent. To execute a single command as the current user | |
# you can use the `runAsUser` function below. | |
# by Armin Briegel - Scripting OS X | |
# | |
# sample code for this blog post | |
# https://scriptingosx.com/2020/08/running-a-command-as-another-user/ | |
# Permission is granted to use this code in any way you want. | |
# Credit would be nice, but not obligatory. | |
# Provided "as is", without warranty of any kind, express or implied. | |
# variable and function declarations | |
export PATH=/usr/bin:/bin:/usr/sbin:/sbin | |
# get the currently logged in user | |
currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' ) | |
# global check if there is a user logged in | |
if [ -z "$currentUser" -o "$currentUser" = "loginwindow" ]; then | |
echo "no user logged in, cannot proceed" | |
exit 1 | |
fi | |
# now we know a user is logged in | |
# get the current user's UID | |
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 | |
} | |
################################################ | |
# Set user preferences on MacOS Sonoma | |
# Tested running via Command Code in Mosyle | |
# by TedSerbinski.com | |
# | |
# Permission is granted to use this code in any way you want. | |
# Credit would be nice, but not obligatory. | |
# Provided "as is", without warranty of any kind, express or implied. | |
# show sound controls | |
runAsUser defaults write com.apple.controlcenter.plist Sound -int 18 | |
# show screen mirroring controls | |
runAsUser defaults write com.apple.controlcenter.plist ScreenMirroring -int 18 | |
# show AirDrop toggle | |
runAsUser defaults write com.apple.controlcenter.plist AirDrop -int 18 | |
# Set the BatteryShowPercentage to 1 using PlistBuddy | |
#runAsUser /usr/libexec/PlistBuddy -c "Set :BatteryShowPercentage 1" ~/Library/Preferences/ByHost/com.apple.controlcenter.plist | |
# show battery percentage | |
runAsUser defaults write com.apple.controlcenter.plist BatteryShowPercentage -int 1 | |
# disable desktop click reveal setting | |
# credit goes to the blog post for the setting: | |
# https://derflounder.wordpress.com/2023/09/26/managing-the-click-wallpaper-to-reveal-desktop-setting-in-macos-sonoma/ | |
runAsUser /usr/bin/defaults write com.apple.WindowManager EnableStandardClickToShowDesktop -bool false | |
# Give some time for the setting to be written | |
sleep 2 | |
# Restart SystemUIServer to apply changes | |
runAsUser killall SystemUIServer | |
# Restart ControlCenter to refresh the user interface | |
runAsUser killall ControlCenter | |
# run an AppleScript command | |
runAsUser osascript -e 'display alert "Menu Bar Successfully Updated" message "Menu bar updated to show screen mirroring, sound, airdrop, and battery percentage."' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment