Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've made two .command files, "lock" and "unlock". The first hides hidden files with: defaults write AppleShowAllFiles TRUE, then refreshes finder with killall Finder. The second does the opposite, I simply replaced TRUE with FALSE, in the first command.

I'd like to copy these over to a USB thumb drive in such a way that when I plug this USB into my laptop, it automatically executes the "unlock" file, and as soon as I unplug it, the "lock" file is executed.

I don't know to what extent this is possible and what drawbacks/problems it may entail, but any help or input is very much appreciated. I'm also only doing this for fun, so I'm perfectly okay with experimenting!

share|improve this question
Keyboard Maestro 6 apparently has a feature like this, but I haven't used it yet. There's also EventScripts: which may be able to do what you are looking for. – TJ Luoma May 30 '13 at 7:16

A launchd service supports the WatchPaths option, which launches a program when the contents of a folder are changed:

WatchPaths <array of strings> This optional key causes the job to be started if any one of the listed paths are modified.

By watching /Volumes, a shell script can be created which would check whether your external disk is mounted or not° and run the appropriate commands°°.

° The simplest way would be checking for the presence of a /Volumes/disk-name/ folder, but it would also be the simplest to fool with by renaming the partition or having another one with the same name, causing the mount point to be modified.

°° It would be appropriate to use a temporary file storing the result of the last execution to avoid killing the Finder if another disk was to be [un]mounted.

share|improve this answer

I couldn't get this to work reliably, but you could try saving a property list like this as ~/Library/LaunchAgents/showall_external.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN >
<plist version="1.0">
        <string>\ AppleShowAllFiles
sleep 3
if [[ -d /Volumes/WD/ ]]; then
    if [[ $(defaults read $k) = 0 ]]; then
        defaults write $k -bool true
        osascript -e 'quit app "Finder"'
    if [[ $(defaults read $k) = 1 ]]; then
        defaults write $k -bool false
        osascript -e 'quit app "Finder"'

It can be loaded with launchctl load ~/Library/LaunchAgents/showall_external.plist or by logging out and back in.

Without the sleep command /Volumes/WD/ sometimes existed when the volume was being unmounted. The program is also run when files under /Volumes/WD/ are modified, so it checks the current value of the key first. The program gets throttled if it has been run in the last 10 seconds, so there can be a delay of up to 10 seconds before it is run.

ControlPlane can run a script when a USB device is attached, but I couldn't figure out how to run a script when a USB device is disconnected or a volume is ejected.

share|improve this answer

Your Answer


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.