Skip navigation

Is it possible to open a program when connecting an external hard drive?

295 Views 14 Replies Latest reply: Dec 29, 2012 7:56 AM by Topher Kessler RSS
fraveu Level 1 Level 1 (0 points)
Currently Being Moderated
Dec 27, 2012 4:44 PM

Is it possible to open a program when connecting an external hard drive?

 

I have a external hard drive with an iphoto library. I just want to connect my drive and automatically see iphoto open! Possible?

 

I know there are some action scripts, am i right?

MacBook Air, Mac OS X (10.7.3)
  • Topher Kessler Level 6 Level 6 (9,295 points)

    Yes you can do this. I've written about how to do this to automatically change the Safari download folder when a drive is connected, but a similar approach can be taken to have the system launch iPhoto whenever a specific drive (or any drive) is attached.

     

    http://reviews.cnet.com/8301-13727_7-20101099-263/applescript-fun-automatically- changing-safaris-downloads-folder/

     

    I have to book it right now, but if later on I can write up a small edit to these steps that will have them work in your situation, if you are not able to figure out how to do this on your own from the steps in that article.

  • Topher Kessler Level 6 Level 6 (9,295 points)

    Do you want just this drive to cause iPhoto to open, or any drive you attach to cause the program to open?

  • Topher Kessler Level 6 Level 6 (9,295 points)

    Ok, here are the steps to take. Doing this involves generating two scripts, the first of which will have the system monitor the attachment of a hard drive, and the second which will conditionally open iPhoto. To make these and set them up, perform the following steps (it is a little involved, but will get you the result you want):

     

    Script 1: A launch agent to monitor attached hard drives

     

    1. Open the Terminal utility in the /Applications/Utilities/ folder. Then copy and paste the following command into it and press Enter, which will open up an editor program in the Terminal:

     

    pico ~/Library/LaunchAgents/iphotodrive.plist

     

    2. Now copy and paste the following text to the Terminal window in its entirety so it is all entered into the Terminal editor:

     

    <?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>local.iPhotoDrive</string>
              <key>QueueDirectories</key>
              <array/>
              <key>WatchPaths</key>
              <array>
                        <string>/Volumes</string>
              </array>
              <key>ProgramArguments</key>
              <array>
                        <string>/Users/USERNAME/Library/iphotodrive.sh</string>
              </array>
    </dict>
    </plist>

     

    3. Use the arrow keys to navigate your cursor down to the place where it says /Users/USERNAME/Library..., and change the text of "USERNAME" to be the same name as your account home folder. For example, my login name in OS X is "tkessler" so my home folder is also "tkessler," so for me this is what should go in place of "USERNAME" in the text above.

     

    4. Now press Control-O to save the file, and then Control-X to exit the editor (use Control, and not the standard Command key here).

     

     

    Script 2: A shell script to open iPhoto when your drive is attached

     

    1. Type (or copy and paste) the following command in the same Terminal window to make the second script in the same editor:

     

    pico ~/Library/iphotodrive.sh

     

    2. Now copy and paste the following text into the Terminal editor:

     

    #/bin/bash
    
    if [ -d /Volumes/"IPHOTODRIVE" ]; then
            open -a iPhoto;
    fi
    

     

    3. Again use the arrow keys to navigate, and edit the text IPHOTODRIVE to be the exact name of the drive you would like to spur iPhoto to open. The name will be case-sensitive, and spaces are OK within the quotes.

     

    4. Again save this file by pressing Control-O, then press Control-X to quit the editor.

     

    5. Next ensure this new script can be run, so to do this now copy and paste the following command:

     

    chmod +x ~/Library/iphotodrive.sh

     

     

    Now to activate these scripts you can log out of your account and log back in, and they should work as expeted, and will launch iPhoto only when a drive named "IPHOTODRIVE" (or whatever you edited this entry to be) is attached to the system. This procedure will create two files at the following locations on your computer, so to undo it simply remove these two files followed by logging out and back in, and you should be good to go:

     

    /Users/USERNAME/Library/iphotodrive.sh

    /Users/USERNAME/Library/LaunchAgents/iphotodrive.plist

     

    If you run into any problems, then write back here and we can figure out if there are any nuance details to cover for your setup.

    MacBook Pro, OS X Mountain Lion (10.8.2), 17&quot;, 2.5GHz i7, 16GB RAM, 512GB SSD
  • Topher Kessler Level 6 Level 6 (9,295 points)

    Yikes! ok...you can do the following to prevent it from continually opening iPhoto. Its odd that it should do this, but it is...

     

    1. Open the Terminal and edit the shell script file by running the following command:

     

    pico ~/Library/iphotodrive.sh

     

    2. Modify it so it looks like the following (or simply erase its contents and copy and paste the following):

     

     

    #/bin/bash
    
    killall -d iPhoto &> /dev/null
    
    if [ -d /Volumes/"IPHOTODRIVE" ] && [ $? ]; then
            open -a iPhoto;
    fi
    

     

     

    3. Press Control-O followed by Control-X to save and exit, and then the script should run both contitionally on the presence of the drive and only do so when iPhoto is not running. If iPhoto is running then the script should not do anything.

     

    I'll think about this more, but the one problem here that is not addressed is that if you quit iPhoto then as long as the drive is attached then this script will run and re-launch the program, so one approach is to remember to unmount the drive first and then quit the program.

    MacBook Pro, OS X Mountain Lion (10.8.2), 17&quot;, 2.5GHz i7, 16GB RAM, 512GB SSD
  • Topher Kessler Level 6 Level 6 (9,295 points)

    These replies might be emailed to you by Apple's systems. I made a typo in that last script which would prevent it from working. I updated it just now, so it should work again.

  • Topher Kessler Level 6 Level 6 (9,295 points)

    One last edit...make that last script where it says [ $? ], change this to be the following:

     

    [ $? == 1 ]

  • Topher Kessler Level 6 Level 6 (9,295 points)

    Ugh...sorry about this. I'm mixing up scripting approaches here. The last script should be the following, so you can edit it as mentioned above and ensure its contents are as follows:

     

    #/bin/bash
    
    killall -d iPhoto &> /dev/null
    
    STATUS=`echo $?`
                            
    if [ -d /Volumes/"IPHOTODRIVE" ] && [ "$STATUS" -eq 1 ] ; then
            open -a iPhoto;
    fi
    
    MacBook Pro, OS X Mountain Lion (10.8.2), 17&quot;, 2.5GHz i7, 16GB RAM, 512GB SSD
  • softwater Level 5 Level 5 (5,370 points)

    I think those scripts should start with

     

    #! /bin/bash

     

    not

     

    #/bin/bash

  • Topher Kessler Level 6 Level 6 (9,295 points)

    Yes you're correct. It wont necessarily hurt anything to not to have this since the default interpreter for a user's account is already bash, but the proper syntax for this is indeed #!/bin/bash

     

    I'm having one of those days where every word is a typo...the delete key is getting abundant exercise.

  • softwater Level 5 Level 5 (5,370 points)

    Fair enough.

     

    BTW, you probably already know this, but a safer way to provide scripts online is to put them in pastebin, then provide a link to it from here. That way, you can avoid the limitations of ASC timeout on editing posts and correct the script on pastebin as necessary.

     

    I learned that one the hard way too!

  • Topher Kessler Level 6 Level 6 (9,295 points)

    That is very bizarre that it is continually running and invoking iPhoto. Do you have many external hard drives and other attached storage drives (even network shares) set up on your system? If it is blocking your work you can drag the iPhoto window to be smaller temporarily and then edit the script file to be as I mentioned below. Saving this will then have it run conditionally on iPhoto's running status so it does not always load the program and bring it to the front.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.