Create a Mac favorite/bookmark desktop shortcut with a script

If you want to create a bookmark for a webpage using a script (which, for example, you can run at user login), just create a script with this code:

/usr/libexec/PlistBuddy -c "add :URL string 'http://YOURWEBSITE.COM'" ~/Desktop/BOOKMARK.webloc
Just substitute in the actual website you want for YOURWEBSITE.COM and the actual name of the desktop short for BOOKMARK.

If you'd prefer not to use the deprecated login hook, you can use Outset to run your login scripts.


Using Plistbuddy to delete a string from an array

I have two previous posts about using Plistbuddy to add to an array in a .plist file (specifically Munki's SelfServeManifest file):
Bash script to add optional installs for Munki
Terminal command to mark a Munki optional install for installation

What if you want to remove an item from the array? It's not a simple thing to do. One option you have is to read the entire array, remove the offending entry, and then write back the modified array.

The most straightforward (but still roundabout) way I could think to do it is to find the index of the offending entry and then remove the entry by index number. Here's an example:

# See if Acrobat DC is set to be installed in the self-serve manifest
acrobatDC=$(/usr/libexec/PlistBuddy -c "Print managed_installs" /Library/Managed\ Installs/manifests/SelfServeManifest | /usr/bin/grep -n "AdobeAcrobatDC" | /usr/bin/awk -F ":" '{print $1}')

if [ ! -z "$acrobatDC" ]; then
# Item to delete is the number minus two

/usr/libexec/PlistBuddy -c "Delete :managed_installs:$itemToDelete" /Library/Managed\ Installs/manifests/SelfServeManifest

The first command puts into a variable the output of reading the managed_installs after finding out what line number the offending entry is on. Since it prints the word "Array {" on the first line and since the indexing starts at zero, the third line would be index 1, the fourth line would be index 2, etc.

So, assuming the variable is not empty, the item to delete is the line number minus 2, and then we can remove it by that index number.


Bash script to add optional installs for Munki

In Terminal command to mark a Munki optional install for installation, I showed a one-off command to mark a Munki optional install item for installation.

What if you want to do it in bulk, though? For example, if you're scripting a thin image to install some software by default, but you want the option for your users to uninstall the software later (i.e., you don't want the items to be managed install items).

I wrote a little script that will add various items of your choice to a the client machine's SelfServeManifest.

The syntax gets a bit tricky with bash variable expansion and single quotes, so I did a bit of a hack to get it working (created a temporary variable to store the source variable surrounded by single quotes).


Terminal command to mark a Munki optional install for installation

If you're a beginning Munki administrator, you're probably pretty familiar with adding software to these categories:

  • Managed Installs: Software that has to be installed on the client machine and does not appear as an optional install to be removed.
  • Managed Uninstalls: Software that must be removed, so if it's installed will be force-removed from the client machines.
  • Optional Installs: Software that users can install or uninstall themselves using Managed Software Center.
  • Managed Updates: Software whose update-for software will install automatically. For example, if you have MSOffice2011 installed and then have MSOffice2011Updates set as an update package for MSOffice2011, you would want MSOffice2011 in Managed Updates.

But what if you want to keep a package in optional installs but trigger it to install for a group of users (based on a script or a nopkg, for example)?

The terminal command you want to kick off is something like

/usr/libexec/PlistBuddy -c "Add :managed_installs:0 string 'Firefox'" /Library/Managed\ Installs/manifests/SelfServeManifest
where the name (the actual name—not the display name) of the package is in single quotes (example above is Firefox, but you'd substitute in whatever package you want to trigger). This is equivalent to the user launching up Managed Software Center and then clicking the Install button next to an optional install.

Not that functionally there is any difference in having a duplicate managed install in the SelfServeManifest, but if you want to check first that the entry isn't in there, you can use this command (perhaps assign it to a variable) to see if the entry's already there before you decide to add it:

/usr/libexec/PlistBuddy -c Print:managed_installs /Library/Managed\ Installs/manifests/SelfServeManifest | grep "Firefox"

Acknowledgements: Full credit goes to Arjen van Bochoven's post on the Munki Dev mailing list for this tip.