Cannot set default printer using lpoptions in a script

Spillou
New Contributor III

Hello !

I have a script that helps me to reconfigure printers and sets them by default depending of their location, department, etc...

The problem is that I can't set the ~/.cups/lpoptions by using lpoptions -d default_printer. To run the command, I use a function that I found and that executes it as the logged user.

Expand
runAsUser() {
     if [[ "${loggedInUser}" != "loginwindow" ]]; then
          launchctl asuser "$userID" sudo -u "${loggedInUser}" "$@"
     else
          echo "No user logged in"
          exit 1
     fi
}

If I execute the command as test:
launchctl asuser 502 sudo -u myuser /usr/bin/lpoptions -d default_printer, it does nothing. The lpoptions file is not populated. Even if I run the command line locally, it doesn't work. It only works if I run the script or the command line as the specific local user.

Any help is welcome !

1 REPLY 1

Spillou
New Contributor III

Well, I found how to manage this with "sudo -iu". I have used this command line in the past, but I got problems resolved with the "launchctl asuser" command, that doesn't work for me with the lpoptions command.