Get contents of disk images

I really like how Hazel has the ability to extract archive files and then delete the archive, but I also wanted the same thing for disk images. I couldn't get it to work with built-in methods, so I wrote this script:
You can modify the destination path "destPath" at the top to set where you want the contents of the disk image to go. Just paste the code into a text editor and give it the extension ".rb". Then just have Hazel look for disk images and run an embedded shell script:
I can also modify it to move any applications straight to the Applications folder if that would be useful. I tested this out on a few application disk images, so let me know if anything isn't working right and I'll try to fix it.
- Code: Select all
# Destination for disk image contents, change if desired
destPath = "/Volumes/Macintosh HD/Downloads/"
# Get path of disk image from Hazel
path = ARGV[0]
# Mounts the disk image, agrees to the license agreement and supresses the Finder window that normally pops up. I tried using -noautoopen, but that didn't work for me, so I used -nobrowse, which hides the mounted image from the Finder entirely. Change if desired
output = %x[echo "y" | hdiutil attach -nobrowse #{path.dump}]
# Gets the path of the new mounted volume
volPath = output.split("\n")[-1].split("\t")[-1]
# Gets every item in the disk image
items = %x[cd #{volPath.dump}; ls].split("\n")
# Goes through all the items and copies anything that's not an alias (Usually to the Applications folder) to the destination path specified above
items.each do |item|
if !%x[GetFileInfo -a #{volPath.dump + "/" + item}].include?("A")
%x[cp -R #{volPath.dump + "/" + item + " " + destPath.dump}]
end
end
# Unmounts the disk image and moves it to the trash
%x[hdiutil unmount #{volPath.dump}; mv #{path.dump} ~/.Trash]
You can modify the destination path "destPath" at the top to set where you want the contents of the disk image to go. Just paste the code into a text editor and give it the extension ".rb". Then just have Hazel look for disk images and run an embedded shell script:
- Code: Select all
ruby "/Path/To/Script/CoolScript.rb" "$1"
I can also modify it to move any applications straight to the Applications folder if that would be useful. I tested this out on a few application disk images, so let me know if anything isn't working right and I'll try to fix it.