Posted on 05-11-2021 11:34 AM
Hey All,
I'm trying to script the Apple ID preference pane to open and have a command that works on the standard logged in user in terminal and in the past when I had gotten this, it was because the script was still trying to run as root, however I'm telling it to run as the logged in user and IM getting the above error "LSOpenURLsWithRole() failed with error -54 for the URL x-apple.systempreferences:AppleIDPrefPane"
Below is the script, if anyone has a suggestion.
#!/bin/sh
currentUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{print $3}')
sudo -u $currentUser open "x-apple.systempreferences:AppleIDPrefPane"
#sleep 150
#jamf recon
exit 0
Gabe Shackney
Princeton Public Schools
Solved! Go to Solution.
Posted on 05-11-2021 11:43 AM
You should use launchctl asuser
rather than sudo -u
like so:
#!/bin/bash
loggedInUser=$( echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ && ! /loginwindow/ { print $3 }' )
loggedInUID=$(/usr/bin/id -u "$loggedInUser" 2>/dev/null)
if [[ -n "$loggedInUser" ]]; then
/bin/launchctl asuser "$loggedInUID" /usr/bin/open /System/Library/PreferencePanes/AppleIDPrefPane.prefPane
fi
exit 0
Posted on 05-11-2021 11:43 AM
You should use launchctl asuser
rather than sudo -u
like so:
#!/bin/bash
loggedInUser=$( echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ && ! /loginwindow/ { print $3 }' )
loggedInUID=$(/usr/bin/id -u "$loggedInUser" 2>/dev/null)
if [[ -n "$loggedInUser" ]]; then
/bin/launchctl asuser "$loggedInUID" /usr/bin/open /System/Library/PreferencePanes/AppleIDPrefPane.prefPane
fi
exit 0
Posted on 05-11-2021 11:55 AM
That did it. Thanks @ryan.ball !
I may have to go through some of my other "open" scripts and adapt them with this modification. Could you by chance describe the difference between the 2 commands?
Gabe Shackney
Princeton Public Schools
Posted on 05-11-2021 12:06 PM
@arminBriegel Has done a great job in explaining some of the use-cases here:
https://scriptingosx.com/2020/08/running-a-command-as-another-user/
In truth it depends on the scenario as to which command you'd use, but I lean toward launchctl asuser
. Im some cases you might need to use a combination of the two:
launchctl asuser $loggedInUID sudo -iu $loggedInUser COMMAND_HERE