Check automatically if a drive is mounted

applescriptautomatorexternal-diskmount

I'd like to automate my family backups, specifically I'd like to run a shell script (with rsync) when an external drive is connected.

Since it's a family thing, it should be also sort of GUI driven, maybe an icon like Dropbox. Probably a shell script with fswatch isn't really nice to play with, they would probably complain and certainly won't understand if an error occurs.

I was thinking about Automator or AppleScript (I have the GUI for the backup script in AppleScript already, but no automation), since "Folder Actions" isn't available for /Volumes, but any other solution is more than welcome.

Any suggestions?

Best Answer

Ive always done mine based on user login. You would go to Users & Groups click the current user go to Login Items hit the plus icon at the bottom and you can add it there. I built a similar application to detect my NAS and mount if not mounted:

enter image description here

Sample code, not tested:

on run
    tell application "System Events" to set theDisks to name of every disk
    set theNAS to "Shared" ## Change here to name
    set isMounted to false
    if theNAS is in theDisks then set isMounted to true
    if isMounted = false then mount volume "afp://" & theNAS
end run

Some links to mounting:

You could do through a LaunchAgent here are some resources:

There are other approaches like a launchd but you will sacrifice RAM so it can check. I think at login would be your best approach.