Christian Schlensker
Christian Schlensker

Reputation: 22478

Need a bash script to automate the annoying downloaded .DMG application install process

I'm trying to automate the tedious application installation process that comes with .DMG files.

I want a bash script that will:

  1. Mount the last downloaded .dmg file in the ~/Downloads directory.
  2. Copy the .app file inside it to the /Applications directory.
  3. Unmounts and deletes said .DMG file.

I'm pretty much a bash scripting noob but I think the benefits of this script should be obvious. I've looked around google and haven't found a solution to this problem, which is a shame. Lets make one.

This answer provides a pretty good start but isn't quite automated enough.

Upvotes: 0

Views: 2708

Answers (2)

Christian Schlensker
Christian Schlensker

Reputation: 22478

I just created a ruby script that does this pretty well.

#!/usr/bin/env ruby

require 'fileutils'
require 'pathname'
include FileUtils

#go to downloads directory
puts "installing most recent .dmg"
cd File.expand_path("~/Downloads/")
path = Pathname.new('.')

#find most recent .dmg file
files = path.entries.collect { |file| path+file }.sort { |file1,file2| file1.ctime <=> file2.ctime }
files.reject! { |file| ((file.file? and file.to_s.include?(".dmg")) ? false : true) }
last_dmg = files.last

#if there is no .dmg file then reject this.
if !last_dmg
  puts "No DMG files" 
  exit
end

puts "Mounting #{last_dmg}"

mount_point = Pathname.new "/Volumes/#{last_dmg}"

result = `hdiutil attach -mountpoint #{mount_point}  #{last_dmg}`

#find any apps in the mounted dmg
files = mount_point.entries.collect { |file| mount_point+file }
files.reject! { |file| ((file.to_s.include?(".app")) ? false : true) }

files.each { |app| 
  puts "Copying #{app} to Applications folder"
  `cp -a #{app} /Applications/` 
}

#unmount the .dmg
puts "Unmounting #{last_dmg}"
result = `hdiutil detach #{mount_point}`
puts "Finished installing #{last_dmg}"


#delete the .dmg
rm last_dmg 

It makes a great Alfred extension http://cl.ly/391x0A0D0l2q0s0t3z2f

Upvotes: 2

KARASZI Istv&#225;n
KARASZI Istv&#225;n

Reputation: 31467

What do you miss? I think there is only one thing is missing, to look for the application and copy that to the /Application/ directory.

MOUNTPOINT="/Volumes/MountPoint"
IFS="
"
hdiutil attach -mountpoint $MOUNTPOINT <filename.dmg>

for app in `find $MOUNTPOINT -type d -maxdepth 2 -name \*.app `; do
  cp -a "$app" /Applications/
done

hdiutil detach $MOUNTPOINT

Of course you'll need to set the <filename.dmg> to the destination of the dmg file. That can be the first argument or something like that.

Upvotes: 6

Related Questions