Help with script for updating an app

howie_isaacks
Valued Contributor II

There is no urgency for this. What I am trying to do is learn how to create a script to do one-off application updates using a policy instead of using Patch Management. The idea is to include a set of parameters that a Jamf tech can fill in to specify which app we are updating with the policy that the script is attached to. The script below works, but there's just one issue. I'm using an osascript to display the dialog to alert the user that the app is about to be updated. The dialog appears, but it does not show the name of the app we are updating. Instead it shows the name of the parameter. Does osascript not work with parameters, or am I just doing something wrong? I don't want the update to run until the user has quit the app and clicked OK. Here's a screenshot of the dialog that appears. My script is below. Thanks for the help!

Screen Shot 2021-08-06 at 9.28.23 AM.png

 

#!/bin/sh
#Updates application at the path defined in paramater 7 to the version defined in parameter 4. For parameter 5, fill in the full application name to be displayed to the user when the policy runs. Policy trigger for parameter 6 can be found in the Installers and Configs category in Policies.
#Parameter for new software version
newVersion="$4"
appName="$5"
policyTrigger="$6"
appPath="$7"
#What is the version of the app installed? Update the app if not current.
version=$(defaults read $appPath/Info CFBundleShortVersionString)
echo $version
if [[ $version > $newVersion ]]
then echo "Application is not current"
else /usr/bin/osascript -e 'display dialog "$appName is not current. Quit $appName, then click OK to update." buttons {"OK"} default button 1'; jamf policy -event $policyTrigger
fi
#Created by Howie Isaacks | F1 Information Technologies | 08/06/2021
1 ACCEPTED SOLUTION

howie_isaacks
Valued Contributor II

OK I've got it! The displayMessage command needs to look like this with double quotes inside the single quotes surrounding appName.

 

 

jamf displayMessage -message ''"$appName"' is not current. Please quit '"$appName"' then click OK to update.'

 

 

 Doing it this way takes what ever I fill into the appName parameter and displays it in the dialog.

Screen Shot 2021-08-06 at 11.38.10 AM.png

The final version of the script that works exactly as intended is:

 

#!/bin/sh

#Updates application at the path defined in paramater 7 to the version defined in parameter 4. For parameter 5, fill in the full application name to be displayed to the user when the policy runs. Policy trigger for parameter 6 can be found in the Installers and Configs category in Policies.

#Parameter for new software version
newVersion="$4"
appName="$5"
policyTrigger="$6"
appPath="$7"

#What is the version of the app installed? Update the app if not current.
version=$(defaults read $appPath/Info CFBundleShortVersionString)
echo $version
if [[ $version > $newVersion ]]
then echo "Application is not current"
else /usr/bin/osascript -e 'display dialog "'"$appName"' is not current. Please quit '"$appName"', then click OK to update." buttons {"OK"} default button 1'; jamf policy -event $policyTrigger
fi

#Created by Howie Isaacks | F1 Information Technologies | 08/06/2021

 

View solution in original post

4 REPLIES 4

vinu_thankachan
Contributor

try using '$appname'

 

#!/bin/sh
#Updates application at the path defined in paramater 7 to the version defined in parameter 4. For parameter 5, fill in the full application name to be displayed to the user when the policy runs. Policy trigger for parameter 6 can be found in the Installers and Configs category in Policies.
#Parameter for new software version
newVersion="$4"
appName="$5"
policyTrigger="$6"
appPath="$7"
#What is the version of the app installed? Update the app if not current.
version=$(defaults read $appPath/Info CFBundleShortVersionString)
echo $version
if [[ $version > $newVersion ]]
then echo "Application is not current"
else /usr/bin/osascript -e 'display dialog "'$appName' is not current. Quit '$appName', then click OK to update." buttons {"OK"} default button 1'; jamf policy -event $policyTrigger
fi
#Created by Howie Isaacks | F1 Information Technologies | 08/06/2021

 

 

 

Screen Shot 2021-08-06 at 8.47.15 AM.png

Thanks! That works using both osascipt and displayMessage. One issue I ran into is that if the app name has more than one word the dialog will only display the first part of the name, and then nothing else after. Placing the app name in quotes doesn't work right either. I'm happy this is working, so the next step for me is figuring out how to format this thing to display the complete name in parameter 5.

Screen Shot 2021-08-06 at 11.29.31 AM.png

 

Screen Shot 2021-08-06 at 11.27.47 AM.png

 

howie_isaacks
Valued Contributor II

OK I've got it! The displayMessage command needs to look like this with double quotes inside the single quotes surrounding appName.

 

 

jamf displayMessage -message ''"$appName"' is not current. Please quit '"$appName"' then click OK to update.'

 

 

 Doing it this way takes what ever I fill into the appName parameter and displays it in the dialog.

Screen Shot 2021-08-06 at 11.38.10 AM.png

The final version of the script that works exactly as intended is:

 

#!/bin/sh

#Updates application at the path defined in paramater 7 to the version defined in parameter 4. For parameter 5, fill in the full application name to be displayed to the user when the policy runs. Policy trigger for parameter 6 can be found in the Installers and Configs category in Policies.

#Parameter for new software version
newVersion="$4"
appName="$5"
policyTrigger="$6"
appPath="$7"

#What is the version of the app installed? Update the app if not current.
version=$(defaults read $appPath/Info CFBundleShortVersionString)
echo $version
if [[ $version > $newVersion ]]
then echo "Application is not current"
else /usr/bin/osascript -e 'display dialog "'"$appName"' is not current. Please quit '"$appName"', then click OK to update." buttons {"OK"} default button 1'; jamf policy -event $policyTrigger
fi

#Created by Howie Isaacks | F1 Information Technologies | 08/06/2021

 

tlarkin
Honored Contributor

A few things to consider here

1 - the shell doesn't really handle software version strings that well, but there are other options one can leverage.  The zsh has a built in called `is-at-least` that can parse and compare version strings, Python has a method in a module you can use called `LooseVersion` and that is a much better way to approach this.  Remember software versions are string data types, and not integers or floats, so accurately parsing them requires tools that support this.

 

2 - Apps should be quit before updated, almost every app does this by design.  You should prompt to quit the app then payload the policy to patch it