Script failing in push policy but works fine in self service

Spillou
New Contributor III

Hello !

I have a script that runs fine when I execute it from self service, but it fails when I try to push it with a policy. The problem resides when I want to open the app with the command: osascript -e 'open app "application name"'

 

I have an error type -10822 when run from the policy but works fine in self service. I also tried to execute that command with sudo -u $loggedInUser osascript -e 'open app "application name"' or with su $loggedInUser osascript -e 'open app "application name"' but no way. If I make a script locally to use the open command, it's fine when run as the logged user but if I run as root and use the $loggedInUser variable in the script, it fails.

Any idea to help me to resolve this ? 

 

It's on a Big Sur mac with Apple M1 chipset.

1 ACCEPTED SOLUTION

rqomsiya
Contributor III

You need to use the command: 

launchctl asuser

 See this post for more details when executing as root: https://scriptingosx.com/2018/08/user-interaction-from-bash-scripts/

View solution in original post

2 REPLIES 2

rqomsiya
Contributor III

You need to use the command: 

launchctl asuser

 See this post for more details when executing as root: https://scriptingosx.com/2018/08/user-interaction-from-bash-scripts/

Spillou
New Contributor III

Hello !

Sorry for the delay, I was quite busy those last days.

Thank you for your reply, it helped me to find the solution. I had difficulties to make it work until I found this link, following your mention of the launchctl asuser command:
https://community.jamf.com/t5/jamf-pro/script-work-in-terminal-but-fails-when-pushed-from-policy/m-p...

launchctl asuser was a part of the solution. The other part is sudo -iu instead of sudo -u
If I take Firefox as an example, here is what worked:

/bin/launchctl asuser "$userID" sudo -iu $loggedInUser /usr/bin/osascript <<EndOfScript
tell application "Firefox" to activate
delay 3
tell application "Firefox" to quit
EndOfScript

What is strange is that my script with only sudo -u osascript -e works in self service. Anyway, it's solved now. Thanks again !