11

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.

4

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:

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
| 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. – jsadfeew 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 :) – jsadfeew Feb 9 '11 at 20:05
12

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).

| 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
2

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.

| improve this answer | |
1

MarcoPolo can do actions based on disk attachment.

| improve this answer | |
0

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

  • Get Specified Finder Items
  • Mount Disk Image
  • Run AppleScript
| improve this answer | |
  • 1
    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. – jsadfeew 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
  • As your answer addresses a different question than what is asked, you should consider deleting it. – DanielSmedegaardBuus Nov 12 '19 at 15:02
0

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/

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .