Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
Did one of these solutions work, and if not, what's the issue? –  Daniel Beck Dec 30 '10 at 8:18
add comment

2 Answers

up vote 4 down vote accepted

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.

share|improve this answer
1  
Maybe 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. pastebin.com/rUUJgS2F –  shadyabhi Dec 28 '10 at 16:52
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
show 6 more comments

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!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.