Skip to content

Instantly share code, notes, and snippets.

@afgomez
Created November 29, 2012 22:29
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save afgomez/4172338 to your computer and use it in GitHub Desktop.
Save afgomez/4172338 to your computer and use it in GitHub Desktop.
Download and install a .dmg
#!/bin/bash
# Downloads and install a .dmg from a URL
#
# Usage
# $ dmginstall [url]
#
# For example, for installing alfred.app
# $ dmginstall http://cachefly.alfredapp.com/alfred_1.3.1_261.dmg
#
# TODO
# - currently only handles .dmg with .app folders, not .pkg files
# - handle .zip files as well
if [[ $# -lt 1 ]]; then
echo "Usage: dmginstall [url]"
exit 1
fi
url=$*
# Generate a random file name
tmp_file=/tmp/`openssl rand -base64 10 | tr -dc '[:alnum:]'`.dmg
apps_folder='/Applications'
# Download file
echo "Downloading $url..."
curl -# -L -o $tmp_file $url
echo "Mounting image..."
volume=`hdiutil mount $tmp_file | tail -n1 | perl -nle '/(\/Volumes\/[^ ]+)/; print $1'`
# Locate .app folder and move to /Applications
app=`find $volume/. -name *.app -maxdepth 1 -type d -print0`
echo "Copying `echo $app | awk -F/ '{print $NF}'` into $apps_folder..."
cp -ir $app $apps_folder
# Unmount volume, delete temporal file
echo "Cleaning up..."
hdiutil unmount $volume -quiet
rm $tmp_file
echo "Done!"
@d1rtyvans
Copy link

Great work. Using perl to get the Volume name is a nice solution.

@angeloschl
Copy link

Thanks for that!

@avibathula
Copy link

Nice one.

I wonder if it can be extended to also take a github repo name and pick the latest release and do the needful (like check if the app is already installed and if so check if the latest release is newer and only then install). The problem I am seeing is that many FOSS github repos are not publishing their products onto homebrew and that makes it harder to "upgrade" over time.

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