Skip to main content

Execute macOS scripts as Console User

Description

By default, the FileWave Client executes scripts and tasks with elevated permissions (root on macOS). This addition to the start of a script will execute it as the currently logged-in user (also known as the console user).

Ingredients

  • Text editor
  • FileWave Central

Directions

  1. Insert this near the start of your script, before the desired actions.
    consoleuser=`#!/bin/zsh
    current_user=$(stat -f%Su /dev/console`console)
    sudocurrent_user_id=$(id -u $consoleuser whoami
    current_user)
    Example:
    #!/bin/shzsh
    consoleuser=`current_user=$(stat -f%Su /dev/console`console)
    current_user_id=$(id -u $current_user)
    
    launchctl asuser $current_user_id sudo -u $consoleusercurrent_user whoami
    echo "hello world" >> /Users/$consoleuser/current_user/Desktop/test.txt