Last Updated: October 07, 2020
·
1.392K
· stelcheck

Install all the DMG's!!!

Not very polished code (nor super solid, technically!), but so far it hasn't failed me once.

I use this to install any DMG's from the command-line, essentially.

function install_from_dmg () {
    URL="${1}"
    MOUNT="${2}"

    pushd /tmp > /dev/null

    echo "Downloading ${URL}"
    curl -L "${URL}" -o app.dmg

    MOUNT_PATH="/Volumes/${MOUNT}"

    echo "Mounting ${MOUNT_PATH}"
    hdiutil mount app.dmg

    MPKG_PATH="$(find "${MOUNT_PATH}" -name "*.mpkg" 2> /dev/null || echo "")"
    PKG_PATH="$(find "${MOUNT_PATH}" -name "*.pkg" 2> /dev/null || echo "")"
    APP_PATH="$(find "${MOUNT_PATH}" -name "*.app" 2> /dev/null || echo "")"
    APP_NAME="$(ls "${MOUNT_PATH}" | grep ".app$" || echo "")"

    if
        [ "${APP_PATH}" != "" ]
    then
        echo "Rsync app to /Applications/${APP_NAME}"
        sudo rsync -av "${APP_PATH}/" "/Applications/${APP_NAME}/"
    elif
        [ "${MPKG_PATH}" != "" ]
    then
        install_from_pkg "${MPKG_PATH}"
    elif
        [ "${PKG_PATH}" != "" ]
    then
        install_from_pkg "${PKG_PATH}"
    else
        abort "No app or pkg found for ${MOUNT}"
    fi

    sleep 5

    echo "Unmounting ${MOUNT_PATH}"
    hdiutil unmount "${MOUNT_PATH}"

    echo "Removing DMG"
    rm -rf app.dmg

    popd > /dev/null
}

function install_from_pkg () {
    echo "Install package ${1}"
    sudo installer -package "${1}" -target "/"
}

Allows:

install_from_dmg \
    http://hc-vagrant-files.s3.amazonaws.com/packages/${VAGRANT_HASH}/Vagrant-${VAGRANT_VERSION}.dmg \
    Vagrant

This should work with DMG containing:

  • .app
  • .pkg
  • .mpkg

I honestly haven't made it better simply cause I am lazy. For instance, I really should specify the path of the app or package to install as an argument, or make it selectable through a read command. Feel free to make those modifications, and let me know about it :)

2 Responses
Add your response

This is perfect thank you.
Well, almost..
I added an exclude in the check for the pkg/mpkg for the word uninstall because some DMGs contain an uninstall package.. and I don't want to run that. :P

MPKG_PATH="$(find "${MOUNT_PATH}" -name "*.mpkg" ! -name "*uninstall*" ! -name "*Uninstall*" 2> /dev/null || echo "")"
PKG_PATH="$(find "${MOUNT_PATH}" -name "*.pkg" ! -name "*uninstall*" ! -name "*Uninstall*" 2> /dev/null || echo "")"
APP_PATH="$(find "${MOUNT_PATH}" -name "*.app" ! -name "*uninstall*" ! -name "*Uninstall*" 2> /dev/null || echo "")"
APP_NAME="$(ls "${MOUNT_PATH}" | grep ".app$" || echo "")"
over 1 year ago ·

I may have gone a little overboard here and modified it to take a list of links to search...
It'll now search html for dmgs or pkgs and sort for the latest version, including relative paths from github.
Paired with installr, i have my fleet deploying again. :)
https://github.com/MacsInSpace/bootstrapinstallr

over 1 year ago ·