Publish Mac App Store apps in Self Service – just like on iOS

(As of Casper Suite 9.4 the JSS natively supports Mac App Store apps in Self Service)

A topic that went around the office recently was listing Mac App Store apps in Self Service on the Mac much in the way that can do so for iOS Self Service.  In our Self Service this is something that I had already done via a script to give users one-click access to Apple Configurator, iBooks Author and Xcode (for starts) while updating their inventory records upon an actual install of the app.

Here is the script that achieves this result:

#!/bin/bash

# The iTunes address for the App (can be grabbed from its App Store page) is passed
# from the JSS into the 'appAddress' variable from parameter 4.
# Example (you would not include quotes): "itunes.apple.com/us/app/ibooks-author/id490152466"
appAddress="$4"


# The name of the app is pass from the JSS into the 'appName' variable from parameter 5.
# Example (you would not include quotes): "iBooks Author.app"
appName="$5"

# A file is created for timestamp comparison
touch /tmp/timestamp


# The user is prompted with a dialog and then Self Service is hidden.
osascript -e 'Tell application "System Events" to display dialog "Self Service will be suspended in the background until you have closed the Mac App Store." with title "Now opening Mac App Store" with text buttons {"OK"} default button 1 giving up after 5'
osascript -e 'tell application "System Events" to tell process "Self Service" to set visible to false'


# The App Store is opened to the specified app.
open macappstore://"${appAddress}"


sleep 3
# Every three seconds the running process list is polled to see if the App Store is open.
while TRUE; do
    openCheck=$(ps -u `logname` -x | awk '/Applications\/App\ Store.app/ {print $2}')
    if [ -z "${openCheck}" ]; then
        break
    fi
    sleep 3
done

# Once the App Store is closed Self Service is shown again.
osascript -e 'tell application "System Events" to tell process "Self Service" to set visible to true'

# The inventory record for that Mac is updated if the user installed the app after the
# timestamp file had been created.  If not, recon is not run.
if [ "/Applications/${appName}" -nt "/tmp/timestamp" ]; then

    osascript -e 'Tell application "System Events" to display dialog "Now updating your inventory..." with title "Returning to Self Service" with text buttons {"OK"} default button 1 giving up after 5'
    jamf recon
fi

# Removes the timestamp file.
rm /tmp/timestamp


exit 0

Comments are included throughout the script, but I can give you a breakdown of what is happening:

  • An empty file is created to log a timestamp of when this process has begun.
  • The user is informed that Self Service is going to be hidden until they clos the Mac App Store.  They can either click “OK” or the prompt will timeout after 5 seconds.
  • The Mac App Store is opened to the chosen app.
  • Every three seconds in the background the script is polling the running process list to see if the Mac App Store is still open.
  • Once the Mac App Store has been closed Self Service will be brought back into view.
  • The date of the app that should be present in the /Applications directory is compared to our timestamp file.  If it is NEWER than the timestamp then we run a Recon to grab the inventory for the JSS.  If it is OLDER then the Recon is not run (thus saving some cycles).

The script works great for populating a catalog of Mac App Store apps within Self Service on the Mac just as we can for our iOS devices.  It is limited in the fact that it is not a solution for redemption codes to paid apps, but it at least can point our people to the right apps and grab inventory if they do install them.

Feel free to share this around and improve upon!

Advertisements

4 thoughts on “Publish Mac App Store apps in Self Service – just like on iOS

    • I have another version now that only performs the “Open” function. I ran into trouble with the dialogue prompts in Mavericks and decided the inventory capture was not really required.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s