A display message to a User.

Hello,


I would appreciate someone's help with with displaying a simple pop up/notification message to a user after a LaunchDaemon has succesfully run. It has to be a Launchdaemon and not a Launchagent.


I have the plist and script in place and its works great but I now need to be shown how to display a message to an end user.


thanks

MacBook Pro with Retina display, OS X Yosemite (10.10.3)

Posted on Sep 30, 2016 7:31 AM

Reply
24 replies

Oct 3, 2016 1:29 AM in response to VikingOSX

Hi VikingOSX


The script is a bash script, here it is.


The osascript below does not display the message.


I don't know how to do add what you say, should that in theory work?


#!/bin/bash


Version=$(sw_vers | grep ProductVersion | tail -c 7 | cut -d . -f 2)


if [ $Version -ge 12 ]

then

launchctl unload /Library/LaunchDaemons/net.sierra.plist

rm -f /Library/LaunchDaemons/net.sierra.plist

rm -f /usr/local/bin/sierra.sh

exit 0


else

rm -rf /Applications/Install\ macOS\ Sierra.app/

osascript -e 'tell app "System Events" to display dialog "macOS Sierra is not allowed on computers at this time." with title "Technology Notice" buttons {"OK"} default button "OK" giving up after 30'

fi

Oct 3, 2016 4:34 PM in response to infmac

There is nothing wrong with your osascript and AppleScript syntax. If the rm fails, it will do so quietly and the osascript never gets run. I added a conditional to check if the installer exists before trying to remove it, in which case, if it does exist, the osascript gets run. Also, you might consider setting the tail -c 7 to tail -c 8.


Change:


else

rm -rf /Applications/Install\ macOS\ Sierra.app/

osascript -e ...

fi


to:


elif [ -d /Applications/Install\ macOS\ Sierra.app ]

then

rm -rf /Applications/Install\ macOS\ Sierra.app

osascript -e ...

fi


If you wanted the slide in notification, then you would use display notification syntax.

Oct 3, 2016 5:20 PM in response to infmac

Hello


You'd need to have application "System Events" be at front lest its dialogue is hidden behind.



#!/bin/bash osascript -e 'tell application "System Events" activate display dialog "macOS Sierra is not allowed on computers at this time." with title "Technology Notice" buttons {"OK"} default button 1 giving up after 30 end tell'



or



#!/bin/bash osascript <<'EOF' tell application "System Events" activate display dialog "macOS Sierra is not allowed on computers at this time." with title "Technology Notice" buttons {"OK"} default button 1 giving up after 30 end tell EOF



Regards,

H

Oct 4, 2016 7:14 AM in response to Hiroto

I checked the log at the point where the disaply message should appear. Does this make any sense to anyone?


com.apple.xpc.launchd[1] (com.apple.xpc.launchd.domain.system): Could not import service from caller: caller = osascript.1115, service = com.apple.systemevents.42912, error = 134: Service cannot load in requested session

Oct 4, 2016 9:23 AM in response to infmac

Incorporate Hiroto's osascript HERE syntax will display the message if the Sierra installer is present. The '¬" symbol is a statement continuation character (option + L) for formatting improvement.


#!/bin/bash

Version=$(sw_vers | grep ProductVersion | tail -c 7 | cut -d . -f 2)

if [ $Version -ge 12 ]

then

echo "Do Sierra related stuff"

elif [ -d /Applications/Install\ macOS\ Sierra.app ]

then

# rm -rf /Applications/Install\ macOS\ Sierra.app

osascript <<'EOF'

tell application "System Events"

activate

display dialog "macOS Sierra is not allowed on computers at this time." ¬

with title "Technology Notice" buttons {"OK"} default button 1 giving up after 30

end tell

EOF

fi

exit 0

Oct 4, 2016 6:21 PM in response to infmac

Hmm. I'm not sure but I'd guess your script is run in an environment with no connection to window server. I'd try the following that executes osascript by a user who is running an instance of SystemUIServer and thus has connection to window server.



#!/bin/bash # get uid who is running an instance of /System/Library/CoreServices/SystemUIServer.app/Contents/MacOS/SystemUIServer uid=$(ps -ax -o uid,command | awk '/MacOS\/SystemUIServer/ {print $1}' | head -n1) # if no such uid is found, exit [[ -n $uid ]] || exit # execute osascript by user of the uid sudo -u "#$uid" osascript <<-'EOF' tell application "System Events" activate display dialog "macOS Sierra is not allowed on computers at this time." with title "Technology Notice" buttons {"OK"} default button 1 giving up after 30 end tell EOF




* You may replace your osascript line with the codes above.


Good luck,

H

Oct 5, 2016 4:07 AM in response to VikingOSX

I'm not sure I am understanding that when you say


then

echo "Do Sierra related stuff"


I did try your script with what i thought was right but it did not delete the .app or display the message.


thanks again.


#!/bin/bash

Version=$(sw_vers | grep ProductVersion | tail -c 7 | cut -d . -f 2)

if [ $Version -ge 12 ]

then

echo "Do Sierra related stuff"

elif [ -d /Applications/Install\ macOS\ Sierra.app ]

then

# rm -rf /Applications/Install\ macOS\ Sierra.app

osascript <<'EOF'

tell application "System Events"

activate

display dialog "macOS Sierra is not allowed on computers at this time." ¬

with title "Technology Notice" buttons {"OK"} default button 1 giving up after 30

end tell

EOF

fi

exit 0

Oct 5, 2016 7:22 AM in response to infmac

  1. You replace the echo "Do Sierra related Stuff" with your real commands that you would run, if you are on macOS Sierra. That echo statement was a stub for my testing.
  2. The code will not take the elif branch if you are on a macOS Sierra booted system, or the macOS Sierra installer app is missing.
    1. The rm command is commented out with '#'. You need to remove the comment.
    2. The osascript code tests fine here, when the elif condition is met.

Oct 5, 2016 8:00 AM in response to VikingOSX

Ok I have amended it to the below. But unfortunately there is still no display message but the sierra.app did delete upon download. Again I would add if I make a test directory /Applications/Install\ macOS\ Sierra.app it deletes and you get the display message.


#!/bin/bash

Version=$(sw_vers | grep ProductVersion | tail -c 8 | cut -d . -f 2)

if [ $Version -ge 12 ]

then

launchctl unload /Library/LaunchDaemons/net.sierra.plist

rm -f /Library/LaunchDaemons/net.sierra.plist

rm -f /usr/local/bin/sierra.sh

exit 0

elif

[ -d /Applications/Install\ macOS\ Sierra.app ]

then

rm -rf /Applications/Install\ macOS\ Sierra.app

osascript <<'EOF'

tell application "System Events"

activate

display dialog "macOS Sierra is not allowed on computers at this time." ¬

with title "Technology Notice" buttons {"OK"} default button 1 giving up after 30

end tell

EOF

fi

exit 0

Oct 5, 2016 8:03 PM in response to infmac

I meant you might try something like the following. (I'm not responsible for other parts than the osascript related 😉 )



#!/bin/bash Version=$(sw_vers | grep ProductVersion | tail -c 7 | cut -d . -f 2) if [[ $Version -ge 12 ]] then launchctl unload /Library/LaunchDaemons/net.sierra.plist rm -f /Library/LaunchDaemons/net.sierra.plist rm -f /usr/local/bin/sierra.sh exit 0 else rm -rf /Applications/Install\ macOS\ Sierra.app/ # get uid who is running an instance of /System/Library/CoreServices/SystemUIServer.app/Contents/MacOS/SystemUIServer uid=$(ps -ax -o uid,command | awk '/MacOS\/SystemUIServer/ {print $1}' | head -n1) # if no such uid is found, exit [[ -n $uid ]] || exit # execute osascript by user of the uid sudo -u "#$uid" osascript <<-'EOF' tell application "System Events" activate display dialog "macOS Sierra is not allowed on computers at this time." with title "Technology Notice" buttons {"OK"} default button 1 giving up after 30 end tell EOF fi



Good luck,

H

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

A display message to a User.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.