Reputation: 22478
I'm trying to automate the tedious application installation process that comes with .DMG files.
I want a bash script that will:
~/Downloads
directory.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
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
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