0

I have the following launch agent that runs on OS X and runs the below script to delete an app when a user downloads an OS upgrade from the App store. It then displays a message to a user after it is deleted.

#!/bin/bash

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

if [[ $Version -ge 11 ]]
then  
    launchctl unload /Library/LaunchAgents/net.company_name.blocksierra.plist
    rm -f /Library/LaunchAgents/net.company_name.blocksierra.plist
    rm -f /usr/local/bin/blocksierra.sh
exit 0

else
    rm -rf /Applications/Install\ macOS\ Sierra\ Public\ Beta.app/
    osascript -e '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

I am now going to package it and then deploy it to all of our Mac machines. My question is what do I add to the script to get the package to auto "load" the launchagent automatically once the package is installed?

So in other words how do I incorporate this command?

sudo launchctl load /Library/LaunchAgents/net.company_name.blocksierra.plist

Here is the plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"  "http://www.apple.com    /DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>net.company_name.blocksierra</string>
<key>ProgramArguments</key>
<array>
    <string>/usr/local/bin/blocksierra.sh</string>
</array>
<key>KeepAlive</key>
<dict>
    <key>PathState</key>
    <dict>
        <key>/Applications/Install macOS Sierra Public Beta.app/</key>
        <true/>
    </dict>
</dict>
<key>OnDemand</key>
<true/>
</dict>
</plist>

1 Answer 1

1

All jobs related to items installed by packages (i.e. a pkg installer) are done with preflight or postflight scripts which are included in the package.

A preflight script may check if a needed folder exists and if its permissions are set properly. A postflight script may load launch agents and daemons installed by the pkg installer and set their owners and permissions.

In your case you would use a postflight script like this one:

#!/bin/bash

sudo /bin/launchctl load /Library/LaunchAgents/net.company_name.blocksierra.plist

BTW: Using if [[ $Version -ge 11 ]] will unload and delete the launch agent when El Capitan is installed. You may have to change this to 12.

6
  • The postflight script you provided works and the Launchagent deletes the app, however, upon downloading the app again it does NOT delete it. I have verified the plist is loaded by typing launch list. Can you advise?
    – sql1
    Commented Sep 15, 2016 at 13:10
  • In my test it is 10.10.5. The version of the others ranges from 10.8 to 10.11.
    – sql1
    Commented Sep 16, 2016 at 8:59
  • To block both Sierra and beta Sierra. We dont need to block El cap. The goal is to block the upgrade to sierra from any previous OS the user is on E.g if user is on 10.8/10.9/10.10 or 10.11.
    – sql1
    Commented Sep 16, 2016 at 9:40
  • your postflight script works ok again. on reboot the app is deleted. But the osascript, as above, does not display the splash message to the user? Is it because sudo is being used in sudo /bin/launchctl load /Library/LaunchAgents/net.company_name.blocksierra.plist and this is a LA? How could i get a splash message to a user if I do this as a LD?
    – sql1
    Commented Sep 16, 2016 at 12:24
  • @sql The postflight script is executed only once - after installing the pkg. There is no relation between the postflight script and the jobs an la or an ld has to execute. The postflight just loads and starts the new la/ld because they neither can start themselves nor does launchd know them.
    – klanomath
    Commented Sep 16, 2016 at 12:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .