macoscommand-lineterminaldmg.app

How can I silently install a MacOS .app from a dmg, using a single command?


I'm writing an auto update facility for my cross platform application. The updater portion downloads the installer file and executes a shell command to install it. On MacOS our "installer" takes the form of .dmg file. I need to be able to silently mount the disk image, copy/overwrite the contained .app(s) to the destination directory, then unmount the disk image. I am assuming the disk image contains a bundle that can be directly copied to /Applications or elsewhere. There is no sensible way to handle an arbitrary .dmg file as asked before, as its contents cannot be known. Some assumptions must be made.


Solution

  • VOLUME=$(hdiutil attach -nobrowse '[DMG FILE]' |
        tail -n1 | cut -f3-; exit ${PIPESTATUS[0]}) &&
    (rsync -a "$VOLUME"/*.app /Applications/; SYNCED=$?
        (hdiutil detach -force -quiet "$VOLUME" || exit $?) && exit "$SYNCED")
    

    I'll break this down: