{"id":48,"date":"2007-02-11T16:17:42","date_gmt":"2007-02-11T23:17:42","guid":{"rendered":"http:\/\/www.vocaro.com\/trevor\/blog\/2007\/02\/11\/a-set-of-scripts-to-unmount-drives-before-sleeping\/"},"modified":"2007-02-11T20:00:30","modified_gmt":"2007-02-12T03:00:30","slug":"a-set-of-scripts-to-unmount-drives-before-sleeping","status":"publish","type":"post","link":"http:\/\/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 \u00e2\u20ac\u0153Finder\u00e2\u20ac\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 [\u00e2\u20ac\u0153Backup drive\u00e2\u20ac\u009d, \u00e2\u20ac\u0153Clone\u00e2\u20ac\u009d]\r\n\r\nrepeat with drive in local_drives\r\n   \r\n   set drive to \u00e2\u20ac\u0153\/Volumes\/\u00e2\u20ac\u009d &#38; drive\r\n   set driveExists to false\r\n   \r\n   try -- Ignore AppleScript warnings\r\n      do shell script \u00e2\u20ac\u0153test -d \u00e2\u20ac\u009d &#38; \u00c2\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; \u00c2\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 [\u00e2\u20ac\u0153DreamHost\u00e2\u20ac\u009d, \u00e2\u20ac\u0153DOC\u00e2\u20ac\u009d]\r\n\r\nrepeat with drive in remote_drives\r\n   \r\n   set drive to \u00e2\u20ac\u0153\/Volumes\/\u00e2\u20ac\u009d &#38; drive\r\n   do shell script \u00e2\u20ac\u0153umount \u00e2\u20ac\u009d &#38; \u00c2\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 \u00e2\u20ac\u0153drutil -drive 1 eject\u00e2\u20ac\u009d\r\n\r\n-- This will eject all discs in all optical drives\r\ndo shell script \u00e2\u20ac\u0153drutil eject\u00e2\u20ac\u009d\r\n<\/pre>\n<p>For a downloadable version of these scripts, see <a href=\"http:\/\/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":"http:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":0,"href":"http:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"wp:attachment":[{"href":"http:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/vocaro.com\/trevor\/blog\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}