Last active
December 28, 2016 16:15
-
-
Save steverichey/9a5230ee3e412f9ef87b6af53affdf48 to your computer and use it in GitHub Desktop.
Python script used in Automator workflow to eject trashed images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python script to eject disk images passed as args | |
# Create a new Folder Action in Automator and add a Run Shell Script step | |
# Set the shell to /usr/bin/python *AND* set "Pass input:" to "as arguments" | |
# Paste this script and save! Images moved to the trash are now auto-ejected | |
# Mostly based on http://www.guidingtech.com/26874/eject-delete-dmg-files-automatically/ | |
import os, sys | |
# Bail if we didn't wipe a DMG | |
if not any(".dmg" in s for s in sys.argv): | |
quit() | |
# Get list of all images | |
lines = os.popen("hdiutil info").readlines() | |
should_eject = False | |
for line in lines: | |
if line.startswith("image-alias"): | |
# Get this image's Finder path | |
path = line.split(":")[1] | |
image_path = path.lstrip().rstrip() | |
# Flag this image for ejection if it was an arg | |
if image_path in sys.argv: | |
should_eject = True | |
elif line.startswith("/dev/") and should_eject is True: | |
# This line is the actual image path, if flagged, eject | |
eject_res = os.popen("hdiutil eject %s" % line.split()[0]) | |
should_eject = False | |
elif line.startswith("###"): | |
# We've moved on to a new image | |
should_eject = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment