Folder action to start app on detecting named USB volume

Hi,


I'm trying something really simple, but struggling (bear of little brain...)


I am trying to write a folder action that will detect when a specific volume on a USB HDD is added, and then launch an app that I have written in AppleScript.


I have this so far, but it won't work, and I am REALLY not a software man;


on adding folder items to Volumes after receiving <specific folder name>

tell application "Finder"

if name of <specific_folder_name> is "MobileVAULT" then

open application "USB MobileVAULT"

end if

end tell

end adding folder items to


the app "USB MobileVAULT" works a treat (decrypts the volume without the user having to add his password), but I just need to automate the process.


[to give you the whole picture, the user will be issued with a USB HDD, encrypted with TrueCrypt. The app 'USB MobleVAULT' will mount this volume without further interraction (password etc) if the user runs the app. I really want the user to not have to do anything - a USB HDD would normally mount itself and appear in Finder, which is what I want to achieve by using the Folder Action process to call the [USB MoblieVAULT' app]



Thanks


Browdee

MacBook Pro, Mac OS X (10.6.7)

Posted on May 25, 2011 5:14 AM

Reply
10 replies

May 25, 2011 8:01 AM in response to browdee

The structure of a Folder Actions handler is as follows:


on adding folder items tothisFolderafter receivingtheseItems

repeat with thisItem in theseItems


-- code here

end repeat

end adding folder items to

thisFolder and theseItems are variables filled by the system when the handler is called; theseItems is always a list of items (even if there's only one), and so you need to cycle through the list. Replace <specific_folder_name> with thisItem in your if statement.

May 25, 2011 12:49 PM in response to browdee

I have a feeling you're chasing a lost cause.


Unless I'm mistaken, attaching the USB device doesn't actually mount the volume until the associated app is launched, right?


If that's the case then inserting the device won't trigger your folder action since the encrypted volume doesn't mount.


It sounds to me like what you want to do is trap the insertion of the USB device, not the actual mounting of the drive, no? If that's the case that's way beyond AppleScript's ability.


If I'm wrong and the volume does mount even without the app being launched you may need an additional check in your code to detect whether the newly mounted device is the encrypted or unencryped volume - for example, if the disk mounts encrypted and you launch the app (either manually or automatically via Folder Actions) then the unencrypted disk appears on your desktop then your script is going to fire again. Unless the encrypted and unencryped volumes have different names you're going to then needlessly re-launch the app.

May 25, 2011 3:14 PM in response to Camelot

This may be a lost cause, but I'm going to crack on till St Jude tells me otherwise... :)


Camelot, you are right, it is the associated app that mounts the TC volume.


The USB device has two volumes, an OSX Extended vol and a FAT one which is encrypted. Both show up in /dev. The first mounts normally, as one would expect; the other needs mounting, which can be scripted but needs a trigger.


You are also right on the trapping of the device rather than the mounting, however; if the first volume mounts (as one would expect with a normal USB device) then maybe this event could be the trigger for the app to mount the encrypted vol? If I give the first vol a specific name or value (like a specifically named file contained file) then this could be the trigger for the app...? (if /Volumes sees /Our_USB then execute the app


Incidentally, the app to do the mounting includes a command: --mount /dev/rdisk1r2 /Volumes/MobileVAULT. It is this that directs the mounting point. The problem here is that the /dev location alters depending on which way the wind blows (at least it's different each time I plug the device in to test - rdisk0s2, 1s2 etc), which doesn't help the overall effect, so I may have to add something in the mounting script that triggers a different version of the mounting app depending on the /dev placing (such as if the vole vol appears in /dev/rdisk0s2, then call the app relating to that /dev location etc).


Thank you very much for your input here. I'm a noob, and your comments are most welcome - every day is a school day!!

May 26, 2011 10:01 AM in response to browdee

launchd is a better way to go. It's more bullet proof.



----------


You need to put your folder action on /Volumes

When OS X mounts a volume, it put the name in /Volumes


Macintosh-HD -> Applications -> Utilities -> Terminal


mac $ ls /Volumes/
Applications                Macintosh-HD@               Utilities
Classical Masters [Disc 7]/ Spot More/
DOS/                        Terminal
mac $


Whenever a volume is mounted, you applescript will get invoked. You need to look for your voume name.


/Voumes is hidden. This applescript unhides things.



(*
 Name: toggle-hidden


    Author: rccharles


    Copyright 2010 rccharles
    GNU General Public License 

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation,  version 3

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    For a copy of the GNU General Public License see <http://www.gnu.org/licenses/>.

    *)

on run
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "

    try
        set results to do shell script "defaults read com.apple.finder AppleShowAllFiles"
        log "results = " & results
    on error errorMsg
        -- Most likely, the variable AppleShowAllFiles does not exit.  
        -- AppleShowAllFiles isn't defined in a newly created account. 
        -- The write command below will create the AppleShowAllFiles variable.
        log "Error ..." & errorMsg
        set results to "FALSE"
    end try

    if results = "FALSE" then
        display dialog "Displaying all files.  Finder will restart." giving up after 2
        set the_rc to do shell script "defaults write com.apple.finder AppleShowAllFiles TRUE  ;killall Finder"

    else
        display dialog "Hiding hidden files.  Finder will restart.." giving up after 2

        set the_rc to do shell script "defaults write com.apple.finder AppleShowAllFiles FALSE  ;killall Finder"

    end if

end run

May 26, 2011 10:36 AM in response to browdee

I have a simple folder action created in Automator that could be the basis for what is being requested. It is for opening an encrypted volume on a flash drive when I plug the flash drive in.


Folder action receives files and folders added to /Volumes

Action 1: Get Specified Finder items. The item is the path and name of the disk image on the flash drive

Action 2: Mount Disk Image


I suppose this runs whenever any disk is mounted but nothing happens unless the drive has that name and that disk image. Or, I guess if I had the flash drive plugged in and I had unmounted the encrypted disk image, it would remount it.

Jun 11, 2011 6:13 PM in response to browdee

here is a variation on what I use for automatic backups. save it as an applescript (use the applescript editor, not automator) and attach it as a folder action attached to /Volumes:


on adding folder items tothisFolderafter receivingtheseThings

set rightStuff to checkForDisk(theseThings)

if rightStuff is not {} then isMounted(rightStuff)

end adding folder items to


on removing folder items fromthisFolderafter losingtheseThings

set rightStuff to checkForDisk(theseThings)

if rightStuff is not {} then isUnmounted(rightStuff)

end removing folder items from


on checkForDisk(theseThings)

set rightStuff to {}

repeat with thisThing in theseThings

tell application "Finder"

if class of thisThing is disk and name of thisThing is "MobileVAULT" then

copy thisThing to end of rightStuff

end if

end tell

end repeat

return rightStuff

end checkForDisk


on isMounted(theDisks)

repeat with thisDisk in theDisks


-- do what you need to do here when disk is mounted

end repeat

end isMounted


on isUnmounted(theDisks)

repeat with thisDisk in theDisks


-- do what you need to do here when disk is unmounted

end repeat

end isUnmounted

Jun 12, 2011 7:51 PM in response to browdee

Here it is. It is for disk images (encrypted or otherwise) built using OS X, not TrueCrypt. You may have to replace the Mount Disk Image action with Run Applescript or Launch Application.


Download, unzip, open in Automator. Plug in your flash drive and wait for it to mount. In the Get Specified Finder Items action, click "add" then navigate to the disk image you want to mount. Save it. It should end up in user/library/workflows/applications/folder actions.


That's the instructions for its intended use. You may have to make some changes for your use.


files.me.com/pwb3/204qvb

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Folder action to start app on detecting named USB volume

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.