Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I want to run an Applescript when I mount a specific disk. It isn't if the script is located on the disk or not, it should just run when OS X mounts the disk.

I tried to accomplish this with folder actions, but it looks like it isn't possible to do it this way.

Clarification:

  1. I get home with my Macbook
  2. I plug my USB Hub into the device
  3. External disks are mounted in OS X
  4. My Applescript gets run, does some stuff on those disks

I want to automate step 4.

share|improve this question
    
possible duplicate of Automatically launch a program or action when an external drive or disk is connected –  user588 Feb 27 '11 at 19:10
add comment

6 Answers 6

up vote 3 down vote accepted

You don't mention under which context you are mounting the disk, but what about going the other way and mounting the disk with the Applescript?

I mention context because when mounting a disk with Applescript is the big caveat that if it is a network volume, and the network is password-protected, then the user name and password would have to be stored as plain text in the script for a seamless execution.

UPDATE: A bit of searching found a similar question elsewhere. Essentially, a script can be made to do this by applying the script as a Folder Action to /Volumes:

property start_date : date "Friday, September 17, 1965 12:00:00 am"

on adding folder items to this_folder after receiving these_items
    repeat with aItem in these_items
        tell application "Finder"
            if (local volume of aItem) and (name of aItem is "[Name of Hard Drive]") then
                -- do something with the drive
                -- eject aItem
            end if
        end tell
    end repeat
end adding folder items to
share|improve this answer
    
I don't want to mount a disk with AS, I want to run an AS when a disk gets mounted. I'll edit my post to make it more clear. –  the-kenny Feb 9 '11 at 18:59
    
I understood the question, but I don't think it is possible to do this very easily. I did, however, tooled around a bit and found something that should work with a bit of tweaking to meet your needs. –  Philip Regan Feb 9 '11 at 19:18
    
I made some small modifications to the script. Works fine :) –  the-kenny Feb 9 '11 at 20:05
add comment

The likely most robust solution is to create a launchd job with the StartOnMount property set to -boolean YES:

StartOnMount <boolean>
This optional key causes the job to be started every time a filesystem is mounted.

This is how Time Machine does it (see /System/Library/LaunchDaemons/com.apple.backupd-attach.plist).

share|improve this answer
    
I feel like this is the most robust solution, as it is dependent only on launchd, which is where "this sort of thing" properly resides. –  Matt Dec 30 '12 at 20:34
add comment

Use a Finder FolderAction that monitors /Volumes. When a new folder appears, that means a new disk has been attached. Now your FolderAction can run an Automator action, AppleScript, or shell script to do your thing.

share|improve this answer
add comment

A solution would be to create an Automator application chaining the 3 following actions :

  • Get Specified Finder Items
  • Mount Disk Image
  • Run AppleScript
share|improve this answer
    
Again, I don't want to mount an image or a disk with Applescript, I want to run a script when a disk gets mounted. I can't find a way to accomplish that with your steps. –  the-kenny Feb 9 '11 at 19:09
    
Yep, i see after clarification that it doesn't work because that's hard drive you're mounting (and not image disk). –  kraymer Feb 9 '11 at 19:57
add comment

MarcoPolo can do actions based on disk attachment.

share|improve this answer
add comment

Depending on your needs, this can be applied to specific folder/mount points and is great to use when mounting a USB flash drive for example...

http://www.jbmurphy.com/2011/07/15/os-x-running-a-script-when-a-usb-drive-is-inserted/

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.