3

I have a USB modem which is mounted as say XYZ(which contains the Dialer Software and Readme) when its inserted. In order to connect using it, I have to eject it and then connect it using ppp dialer.

Let us say that it mounts under the name /Volumes/XYZ

I want to do something like if I insert a USB device and it mounts under the name XYZ, I want it to be ejected immediately.

How do I do that?

Daniel Beck
  • 109,300
  • 14
  • 287
  • 334
Abhijeet Rastogi
  • 3,497
  • 5
  • 33
  • 43

2 Answers2

4

I did something similar once upon a time, where whenever a drive named a certain way was mounted, a script immediately fired off to sync the contents of a set of folders to the drive.

To do something similar here, the script would need to look like this:

#!/bin/bash

if [ -d /Volumes/XYZ ];
    then
    echo “Ejecting XYZ!”;
    umount /Volumes/XYZ
    exit;
fi

Save it somewhere, your ~/bin/ directory if you have one, or maybe ~/Library/Scripts/, just remember where.

The second piece of the puzzle is the LaunchAgent that will actually handle the event whenever a drive is mounted:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.superuser.226504.example</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Path/To/Script/unmount-modem.sh</string>
    </array>
    <key>QueueDirectories</key>
    <array/>
    <key>StartOnMount</key>
    <true/>
    <key>WatchPaths</key>
    <array/>
</dict>
</plist>

That needs to be saved as a .plist (named similar to the string used in place of “com.superuser.226504.example”) and saved to ~/Library/LaunchAgents. You can either load it from the terminal via launchctl load ~/Library/LaunchAgents/pathtoplist or log out / log back in and it should get loaded if everything is configured properly.

peelman
  • 4,831
  • 21
  • 23
  • 1
    Maybe [MarcoPolo](http://www.symonds.id.au/marcopolo/) can be used to detect the drive instead? (Not sure if that makes things easier though. Nice answer!) – Arjan Dec 28 '10 at 15:15
  • I haven’t used MarcoPolo before, but looking over the website, it could probably handle it too, in place of a LaunchAgent. I’m a fan of launchd and making things work with the native implementations though :) – peelman Dec 28 '10 at 15:18
  • 1
    I love `StartOnMount`. Great answer! – Daniel Beck Dec 28 '10 at 15:39
  • @peelman I followed what you said but the drive is still mounting.. Please help. http://pastebin.com/rUUJgS2F – Abhijeet Rastogi Dec 28 '10 at 16:52
  • @shady There's a difference between "mounting and immediately unmounting" and "preventing mounting". Peelman's answer (as well as mine) only can do the former, just as you wanted in your question. – Daniel Beck Dec 28 '10 at 16:54
  • @Daniel I meant that the drive remains mounted even after doing all this. Its as if the shell is not getting executed. – Abhijeet Rastogi Dec 28 '10 at 17:03
  • 1
    @shady Change the volume name in the shell script to the one of your modem. Save the XML file with a `plist` extension, e.g. `com.superuser.226504.example.plist`. Specify the actual path to the script on your system in the XML file. Don't specify the path to the shell script using relative paths, only absolute and without ~ for your home directory. Make the shell script executable using `chmod +x unmount-modem.sh`. – Daniel Beck Dec 28 '10 at 17:29
  • @Daniel Thats what I did. Please see this link which I also posted earlier.. http://pastebin.com/rUUJgS2F – Abhijeet Rastogi Dec 28 '10 at 17:36
  • @shady You have a trailing `/` in the if condition, don't know if that makes a difference though. What happens when you `$ /bin/unmount-modem.sh`? You can also open `/Applications/Utilities/Console.app` and look for error messages by launchd. And, lastly, you can try using my answer :-) – Daniel Beck Dec 28 '10 at 17:39
  • @shady there are huge rings of permissions surrounding launchd items, for obvious reasons; they are version powerful tools that can be used for good and bad purposes. You most likely can’t run a root-owned script from a protected portion of the drive, from a user-owned launchd item inside your home directory. move the script or move the launchd item. – peelman Dec 28 '10 at 19:11
  • Also, this script will ONLY run when a new drive is mounted, and it will ONLY unmount that newly mounted drive (pending the names match). – peelman Dec 28 '10 at 19:13
1

You can use the following AppleScript:

on adding folder items to this_folder after receiving added_items
    set the item_count to the number of items in the added_items

    if the item_count is greater than 1 then
        display dialog "Multiple additions, I can't handle that!"
    else
        set the volume_name_raw to (item 1 of the items in the added_items as text)
        set AppleScript's text item delimiters to ":"
        set volume_name to text item 1 of volume_name_raw as text
        if volume_name is "Carbon Copy Cloner" then
            do shell script "hdiutil detach '/Volumes/" & volume_name & "'"
        end if
    end if
end adding folder items to

Based on the folder action "new item alert" by Apple

Change the name of the volume you want to eject in the following line:

if volume_name is "Carbon Copy Cloner" then

To activate it:

  1. Open /Applications/Utilities/AppleScript Editor and paste the script above.
  2. Save it as a script (e.g. eject.scpt) in ~/Library/Scripts/Folder Action Scripts/.
  3. Close AppleScript Editor.
  4. Right-click any folder in Finder, select Services » Folder Actions Setup
  5. Dismiss the sheet dialog.
  6. Check Enable Folder Actions
  7. Click the + button to the lower left, and press Command-Shift-G in the resulting file dialog. Enter /Volumes. Don't select an item, just press OK.
  8. Select the list entry Volumes and click the + button below the right side list.
  9. Select eject.scpt in the sheet dialog and click Attach
  10. Close Folder Actions Setup.

Done!

Daniel Beck
  • 109,300
  • 14
  • 287
  • 334