Skip to content

Instantly share code, notes, and snippets.

@freayd
Last active December 17, 2015 01:59
Show Gist options
  • Save freayd/5532661 to your computer and use it in GitHub Desktop.
Save freayd/5532661 to your computer and use it in GitHub Desktop.
Run commands when a volume is mounted (OS X only)

How to

Create the files here:

  • ~/Library/LaunchAgents/on-volume-mounted.plist
  • ~/Library/Scripts/on-volume-mounted.rb
  • ~/Library/Scripts/on-volume-mounted.yml

Update paths in file 'on-volume-mounted.plist'.

<?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>on-volume-mounted</string>
<key>Program</key>
<string>/Users/freayd/Library/Scripts/on-volume-mounted.rb</string>
<key>KeepAlive</key>
<false/>
<key>StartOnMount</key>
<true/>
</dict>
</plist>
#!/usr/bin/env ruby
require 'yaml'
require 'open3'
mounted_volume = nil
Open3.popen3('mount') do |stdin, stdout, stderr|
mounted_volume = stdout.readlines.last.match(/^\/dev\/\w+ on \/Volumes\/(.+?) \(.+?\)$/)[1]
end
exit if mounted_volume.nil? or ! File.exist?("/Volumes/#{mounted_volume}")
home_folder = File.expand_path('~')
settings = YAML.load_file(File.join(home_folder, 'Library', 'Scripts', 'on-volume-mounted.yml'))
if settings.has_key?(mounted_volume)
settings[mounted_volume].each do |command|
system command
end
end
'Cloud HD':
- 'open /Applications/Dropbox.app'
- 'open /Applications/Google\ Drive.app'
'Music HD':
- 'open /Applications/iTunes.app'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment