Intercept Sleep and Shut Down

I’ve got a network-attached storage volume. It needs to be ejected prior to shut down or it risks corruption. I have to drag it manually to the trash to eject it before doing a shutdown or sleep.

Can AppleScript intercept the Sleep and Shut Down commands, eject the mounted volume, and then complete the Sleep / Shut Down?

Probably. What we need for openers is for someone to remind us of the shell script command to dismount a drive.

Question: what about loggin out?

Will Applescript do?

tell application "Finder" to eject (every disk whose ejectable is equal to true)

Best wishes

John M

Thanks, guys. I guess I wasn’t clear. Here’s what I want to do:

  1. Select “Sleep” or “Shut Down…” items from the Apple menu
  2. Have that event (i.e., selecting the menu item) trigger a script.
  3. Script ejects / unmounts the external mounted drives
  4. Return control to Finder to continue the Sleep or Shut Down.

Question: what about loggin out?

Answer: Good point. For Shut Down, automatically log out all logged-in users. I’ve tried to get other users to log out when they are done, but that is hopeless. I just log them out when I shut down. If they lose work, tough luck. They’ve been warned.

As far as I know, there is no mechanism that allows Applescript to intercept standard OSX menu cmds.
So I do not see any solution other than circumvene the standard Sleep/Shut down commands (e.g. using Fruitmenu)
Rebuild the Apple menu and mimic Sleep/Shut down with a script that FIRST ejects any removable media as indicated and THEN sleeps or shuts down your CPU.