{"id":48,"date":"2007-02-11T16:17:42","date_gmt":"2007-02-11T23:17:42","guid":{"rendered":"https:\/\/vocaro.com\/trevor\/blog\/2007\/02\/11\/a-set-of-scripts-to-unmount-drives-before-sleeping\/"},"modified":"2026-05-03T21:02:51","modified_gmt":"2026-05-04T04:02:51","slug":"a-set-of-scripts-to-unmount-drives-before-sleeping","status":"publish","type":"post","link":"https:\/\/vocaro.com\/trevor\/blog\/2007\/02\/11\/a-set-of-scripts-to-unmount-drives-before-sleeping\/","title":{"rendered":"A set of scripts to unmount drives before sleeping"},"content":{"rendered":"<p>When I&#8217;m at home, my <a href=\"http:\/\/www.apple.com\/macbookpro\/\">MacBook Pro<\/a> is hooked up to an <a href=\"http:\/\/www.apple.com\/displays\/\">external display<\/a>, a couple of hard drives, and other peripherals. When I need to go on the road, I put it to sleep, unplug all the peripherals, and away I go.<\/p>\n<p>There&#8217;s only one problem: Unplugging a hard drive can corrupt its filesystem if it&#8217;s still mounted. To prevent that from happening, I wrote a script that automatically unmounts all drives. As an added bonus, the script fails if any of those drives are busy and can&#8217;t be unmounted, warning me to take additional action before hitting the road. Here it is:<\/p>\n<pre>\r\ntell application \u201cFinder\u201d\u009d\r\n    eject (every disk whose ejectable is true)\r\nend tell\r\n<\/pre>\n<p>In most cases, this simple script works great, but it suffers from a few drawbacks:<\/p>\n<ol>\n<li>It ejects not only hard drives, but optical discs as well, which is usually not what I want.<\/li>\n<li>It won&#8217;t eject remotely mounted drives, such as <a href=\"http:\/\/en.wikipedia.org\/wiki\/Network_File_System_%28Sun%29\">NFS<\/a> or <a href=\"http:\/\/en.wikipedia.org\/wiki\/Apple_Filing_Protocol\">AFP<\/a> mounts.<\/li>\n<li>It won&#8217;t work if Finder is not running, which may be the case for <a href=\"http:\/\/www.cocoatech.com\/pf4\/\">PathFinder<\/a> users like myself.<\/li>\n<\/ol>\n<p>To fix these issues, I split the script into two parts, one for local drives and one for remote drives, and I rewrote it so that optical discs would be left alone. Here&#8217;s the part that unmounts local drives:<\/p>\n<pre>\r\n-- Set this list to the names of the local drives\r\n-- you want to unmount\r\nset local_drives to [\u201cBackup drive\u201d\u009d, \u201cClone\u201d\u009d]\r\n\r\nrepeat with drive in local_drives\r\n   \r\n   set drive to \u201c\/Volumes\/\u201d\u009d &#38; drive\r\n   set driveExists to false\r\n   \r\n   try -- Ignore AppleScript warnings\r\n      do shell script \u201ctest -d \u201d\u009d &#38; \u00ac\r\n            quoted form of drive\r\n      \r\n      -- Test completed successfully; drive exists\r\n      set driveExists to true\r\n   end try\r\n   \r\n   if driveExists then\r\n      -- Eject the drive\r\n      do shell script \"umount \" &#38; \u00ac\r\n            quoted form of drive\r\n   end if\r\n   \r\nend repeat\r\n<\/pre>\n<p>And here&#8217;s the part that unmounts the remote drives:<\/p>\n<pre>\r\n-- Set this list to the names of the remote drives\r\n-- you want to unmount\r\nset remote_drives to [\u201cDreamHost\u201d\u009d, \u201cDOC\u201d\u009d]\r\n\r\nrepeat with drive in remote_drives\r\n   \r\n   set drive to \u201c\/Volumes\/\u201d\u009d &#38; drive\r\n   do shell script \u201cumount \u201d\u009d &#38; \u00ac\r\n         quoted form of drive\r\n   \r\nend repeat\r\n<\/pre>\n<p>I don&#8217;t eject optical drives, but if for some reason you need to do so, here&#8217;s how:<\/p>\n<pre>\r\n-- This will eject the optical disc in the\r\n-- primary (built-in) drive\r\ndo shell script \u201cdrutil -drive 1 eject\u201d\u009d\r\n\r\n-- This will eject all discs in all optical drives\r\ndo shell script \u201cdrutil eject\u201d\u009d\r\n<\/pre>\n<p>For a downloadable version of these scripts, see <a href=\"https:\/\/vocaro.com\/trevor\/software\/applescript\/\">my AppleScript page<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I&#8217;m at home, my MacBook Pro is hooked up to an external display, a couple of hard drives, and other peripherals. When I need to go on the road, I put it to sleep, unplug all the peripherals, and away I go. There&#8217;s only one problem: Unplugging a hard drive can corrupt its filesystem [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,12,10],"tags":[],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-mac","category-software","category-tips"],"_links":{"self":[{"href":"https:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":2,"href":"https:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":901,"href":"https:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions\/901"}],"wp:attachment":[{"href":"https:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}