I’ve got a script that writes a launch daemon and a second shell script. When the second shell script is called by the launch daemon it mostly works, except for the osascript part.
Here’s the function in the parent script that writes the shell script that has the osascript in it.
## CREATES the Script that lives on the machine until completed
createUpdateScript(){
cat << EOF > /private/var/tmp/resetManagement.sh
#!/bin/sh
exec >> "/var/log"/"scriptDebug.log" 2>&1
set -x
/bin/sleep 5
open -b com.apple.systempreferences /System/Library/PreferencePanes/Profiles.prefPane
osascript -e 'display dialog "Please select \"MDM Profile\"and then" & return & "click \"Approve\"." with icon file "Macintosh HD:System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:Actions.icns" buttons {"OK"}'
exit 0
EOF
sudo chmod +x /private/var/tmp/resetManagement.sh
}
And in my log file I get this which I just do NOT understand…
These errors boggle my mind. It’s a simple display dialog!
If I run this manually (sudo /private/var/tmp/resetManagement.sh) it works.
(running macOS 10.15, which isn’t an option in the posting)
If the launch daemon is stumbling on a user interface issue, you could try saving the AppleScript command as an AppleScript application and then opening the application. Save the following command as an application, for example on the desktop at /Users/[your home folder name]/Desktop/Test.app:
Then, replace the osascript command in your shell script with the following:
open -a '/Users/[your home folder name]/Desktop/Test.app'
This is a minor point, but in your original post, the shebang line is correct (#!/bin/sh), but in your subsequent examples, the exclamation point is missing. Just wanted to be sure that it is correct in your actual script.