AppleScript to eject mounted local volumes

AppleScript to eject mounted attached volumes/disks.

I have a MBP which I use with a Thunderbolt dock to connect to various USB and thunderbolt hard drives, SSDs, and optical drives. This script lets me eject and unmount anything physically connected before disconnecting my Thunderbolt dock.

Yah, sure, OWC makes a utility you can install that’ll do this, but it installs a kernel extension which I wasn’t keen on installing.

-- AppleScript I wrote to eject mounted USB disks before I disconnect my USB-C hub.
-- Notes: 
-- this will halt a time machine backup to a locally mounted 
--   drive when it tries to eject it.
--   will also halt a time machine backup to a network destination.
-- tested in macOS 11.6 (2021.11.19)
-- License:  MIT

tell application "System Events"
  set ds to get disks
  
  -- for debugging
  if false then
    log "list of disks: "
    repeat with d in ds
      log (get the URL of d)
      log (get the name of d)
    end repeat
  end if
  
  log ("Ejecting local ejectable volumes")
  repeat with d in ds
    -- before we try to do anything with this disk    
    -- try to access the disk and catch any errors 
    -- as a way to test if the disk is still present.
    -- set a flag that we can test below.
    set diskAvailable to true
    try
      set trashme to (get the name of d)
    on error errormsg number errorno
      if errorno is -1728 then set diskAvailable to false
    end try
    
    -- Notes:
    -- ejectable excludes sparse volume mounts for time 
    -- machine, but also means other ejectables aren't unmounted…
    -- ...decided to remove this in the if clause below and let it halt 
    -- time machine backups even for network volumes also
    -- since I'm generally calling this script when I'm disconnecting
    -- the laptop and heading out.
    --         d is not ejectable and ¬
    
    -- starts with "Data@" excludes time machine local snapshot
    --    mounts during backups
    -- starts with file:///Volumes excludes file:///System/Volumes/... 
    --  system volumes.
    -- local volume excludes network mounts
    
    
    if diskAvailable and ¬
      d is local volume and ¬
      the URL of d starts with "file:///Volumes/" and ¬
      the displayed name of d does not start with "Data@" then
      log ("........ ejecting:  " & (get the name of d))
      tell application "Finder" to eject (get the name of d)
    else if diskAvailable then
      log ("   Skipping: " & (the URL of d))
    else
      log ("  Skipping disk that is no longer available")
    end if
  end repeat
  log (" all ejectable local volumes ejected")
end tell

Posted as a gist here for easier copying and using.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: