Kamesh S S
Kamesh S S

Reputation: 15

macOS - Switch to admin user when running a shell script as a root user

So, I have this name.sh script with bunch of shell commands. I need to change the wallpaper before ending that script.

I'm using

osascript -e 'tell application "System Events.app" to set picture of every desktop to "/Library/Desktop Pictures/my.jpg"'

at the end of the line. The thing is the Apple Script won't run under root. Because System Events will throw 10810 at first and then 600 (Application isn't running). If I run this osascript under admin user, it will work just fine. The wallpaper will be set.

Let me know how you guys can help me in this!

Upvotes: 1

Views: 737

Answers (1)

tripleee
tripleee

Reputation: 189377

If you are already root, you have full privileges to switch to a different user account at any time. The command for that is su.

If your user is admin,

su - admin <<\:
    osascript -e 'tell application "System Events.app" to set picture of every desktop to "/Library/Desktop Pictures/dneg.jpg"'
:

There are various ways to pass the command as standard input to su; perhaps see also Pass commands as input to another command (su, ssh, sh, etc)

Upvotes: 1

Related Questions