Skip to content

Instantly share code, notes, and snippets.

@phillymjs
Last active February 19, 2024 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillymjs/0839b664c99c6d9743afe8dcf34a5536 to your computer and use it in GitHub Desktop.
Save phillymjs/0839b664c99c6d9743afe8dcf34a5536 to your computer and use it in GitHub Desktop.
This script will download remote assets for Nudge when needed. See first comment for more information.
#!/bin/bash
set -u
prefFile="com.github.macadmins.Nudge.plist"
tmpFile="/tmp/com.github.macadmins.Nudge.Settings.plist"
remoteAssetBaseURL="https://yourwebserver.com/nudge_assets"
# Assets we need to pull locally
declare -a valueArray=("iconDarkPath" "iconLightPath" "screenShotDarkPath" "screenShotLightPath")
declare -a currentFilenames=()
function cleanDefunctAssets() {
for checkFile in ${localAssetDir}*; do
if [[ ! " ${currentFilenames[*]} " =~ " $(basename $checkFile) " ]]; then
rm "$checkFile"
fi
done
}
# Figure out if we're using a config profile or local preference file
if [ -f "/Library/Managed Preferences/${prefFile}" ]; then
prefFile="/Library/Managed Preferences/${prefFile}"
elif [ -f "/Library/Preferences/${prefFile}" ]; then
prefFile="/Library/Preferences/${prefFile}"
fi
# If we found a Nudge preference file on the machine then check for/download the assets, otherwise bail
if [ ! -z "${prefFile}" ]; then
# Loop through the preference keys that require local assets
for value in ${valueArray[@]}
do
# Get the asset file and directory it lives in from the plist
plistAssetFile=$(/usr/libexec/PlistBuddy -c "Print :userInterface:${value}" "${prefFile}" 2>/dev/null)
if [ ! -z "$plistAssetFile" ]; then
localAssetDir=$(/usr/bin/awk 'BEGIN { FS = OFS = "/" } { $NF=""}1' <<< "$plistAssetFile")
localAssetFile=$(/usr/bin/awk 'BEGIN { FS = OFS = "/" } { print $NF }' <<< "$plistAssetFile")
currentFilenames+=($localAssetFile)
# Create the local directory if it doesn't exist
[ ! -d "${localAssetDir}" ] && /bin/mkdir -p "${localAssetDir}"
# Pull the file from the webserver if it doesn't already exist
[ ! -f "${plistAssetFile}" ] && /usr/bin/curl "${remoteAssetBaseURL}/${localAssetFile}" -o "${localAssetDir}/${localAssetFile}" -s
fi
done
# Set permissions on the local directory and its contents
[ ! -z "${localAssetDir}" ] && [ -d "${localAssetDir}" ] && /bin/chmod -R 777 "${localAssetDir}"
cleanDefunctAssets
# Now that the assets are stored locally, execute Nudge
/Applications/Utilities/Nudge.app/Contents/MacOS/Nudge
fi
@phillymjs
Copy link
Author

phillymjs commented Feb 19, 2024

This script should be run via your Nudge launchagent in place of just the Nudge executable. It will read the Nudge preference file (managed or local) and download graphical assets for iconDarkPath, iconLightPath, screenShotDarkPath, and screenShotLightPath from a remote server if needed. It will also delete any unused assets from the local directory. Once all needed assets exist locally, it will execute Nudge.

Example:

If your Nudge preference file/config profile specifies this:

<key>iconDarkPath</key>
<string>/Library/nudgestuff/my_icon_dark.png</string>

Then your web server should have this file:

https://yourwebserver.com/nudge_assets/my_icon_dark.png

The script will read in the Nudge preferences, look for my_icon_dark.png in /Library/nudgestuff/, and download it if it's not found. (Make sure /Library/nudgestuff is writable by your users or the download will fail.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment