As a MacBookPro user I want to run a script that ejects my USB drive when I disconnect the power source so that I don't have to remember to eject it before grabbing my laptop and hurrying off to a meeting.
- NO third party apps
- NO polling. It must be event driven
I already have a script that I use for detecting network cable changes. I launch it using launchctl with a WatchPath like this in my .plist file:
<key>WatchPaths</key> <array> <string>/Library/Preferences/SystemConfiguration</string> </array>
I'm hoping to find a similar way to launch a different script when my power cord is connected/disconnected.
I already know how to detect whether AC power is connected or not... I just need to find an event that tells me I can do it.
AC_POWER=`ioreg -l | grep ExternalConnected | cut -d"=" -f2 | sed -e 's/ //g'` if [[ "$AC_POWER" == "No" ]] then for MEDIA_PATH in "/Volumes/Backup Drive 1/" /Volumes/MyPhotos/ do if [ -e "$MEDIA_PATH" ] then echo "Ejecting $MEDIA_PATH" diskutil eject "$MEDIA_PATH" else echo "$MEDIA_PATH not mounted" fi done else echo "AC Power connected" fi