Skip to content

Instantly share code, notes, and snippets.

@jasikpark
Created May 15, 2025 18:05
Show Gist options
  • Save jasikpark/2014eedc929a69b01ddfd16c8110222a to your computer and use it in GitHub Desktop.
Save jasikpark/2014eedc929a69b01ddfd16c8110222a to your computer and use it in GitHub Desktop.
Update DNClient Desktop via the cli
#!/bin/bash
set -eo pipefail
IFS=$'\n\t'
# Script to update DNClient Desktop.app
# Usage: ./update_dnclient.sh <download_url>
# Check if download URL is provided
if [ -z "$1" ]; then
echo "Error: Download URL is required"
echo "Usage: ./update_dnclient.sh <download_url>"
exit 1
fi
DOWNLOAD_URL="$1"
APP_NAME="DNClient Desktop.app"
DOWNLOAD_FILENAME=$(basename "$DOWNLOAD_URL")
# Find the app location
echo "Looking for $APP_NAME..."
APP_PATH=$(mdfind "kMDItemFSName == '$APP_NAME'" | head -1)
if [ -z "$APP_PATH" ]; then
echo "Error: Could not find $APP_NAME on this system"
exit 1
fi
echo "Found $APP_NAME at: $APP_PATH"
# Ask the app to quit using AppleScript
echo "Asking $APP_NAME to quit..."
osascript -e "tell application \"$APP_NAME\" to quit"
# Wait a moment to ensure the app has time to quit
sleep 3
# Check if app is still running
if pgrep -f "$APP_NAME" >/dev/null; then
echo "Warning: $APP_NAME is still running. Forcing quit..."
pkill -f "$APP_NAME"
sleep 2
fi
# Create a temporary directory for the download
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR" || exit 1
echo "Downloading new version from $DOWNLOAD_URL..."
# Download the new version
curl -L -O "$DOWNLOAD_URL"
# Mounting the DMG
echo "Mounting DMG..."
MOUNT_POINT=$(/usr/libexec/PlistBuddy -c "Print :system-entities:0:mount-point" /dev/stdin <<<"$(hdiutil attach "$DOWNLOAD_FILENAME" -nobrowse -plist)")
echo "DMG mounted at $MOUNT_POINT"
# Determine the app location in the downloaded content
NEW_APP_PATH=$(find "$MOUNT_POINT" -name "$APP_NAME" -type d | head -1)
if [ -z "$NEW_APP_PATH" ]; then
echo "Error: Could not find $APP_NAME in the downloaded package"
# Clean up
if [[ "$DOWNLOAD_FILENAME" == *.dmg ]]; then
hdiutil detach "$MOUNT_POINT" -force
fi
rm -rf "$TEMP_DIR"
exit 1
fi
# Get the parent directory of the existing app
APP_PARENT_DIR=$(dirname "$APP_PATH")
echo "Removing old version..."
rm -rf "$APP_PATH"
echo "Installing new version..."
cp -R "$NEW_APP_PATH" "$APP_PARENT_DIR/"
# Unmount the DMG
hdiutil detach "$MOUNT_POINT" -force
# Clean up
rm -rf "$TEMP_DIR"
echo "Starting $APP_NAME..."
open "$APP_PARENT_DIR/$APP_NAME"
echo "Update complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment