Trying to write a shell script that uses osascrip and run from launchd

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)

Browser: Safari 605.1.15
Operating System: Other

What happens if you remove the icon code? You probably don’t have permission to read files in /System/Library.

Good thought… but no. Didn’t work. Similar/same error.

I also made sure I’m using the full path to the osascript command as someone else reminded me that launchd doesn’t have the same paths mapped.

But that’s not working. I’ve even made the script stupid easy. It’s just writing these two lines and then running the script.

That gives me a big error like before…

So I decided to make it even SIMPLER!

And THAT WORKS!

So the problem is just with display dialog???

More likely with any UI. Can your launch daemon show any UI?

A couple of ideas that may or may not help…

  1. 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:

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"}

or, alternatively, with the display dialog command wrapped in an application:


tell application "System Events"
	activate
	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"}
end tell

Then, replace the osascript command in your shell script with the following:


open -a '/Users/[your home folder name]/Desktop/Test.app'

  1. 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.