module LaunchAgent class Daemon DOMAIN = 'com.buycheapviagraonlinenow' def initialize(*args) @args = args end def load open(plist_filename, 'w') do |file| file.write(plist_content) end `launchctl load -w #{plist_filename}` end def unload `launchctl unload -w #{plist_filename}` File.unlink(plist_filename) end def loaded? `launchctl list | grep #{job_id}` =~ /#{job_id}/ end def plist_filename File.expand_path('~/Library/LaunchAgents/' + job_id + '.plist') end def plist_content template = < Label %s ProgramArguments %s RunAtLoad PLIST template % [job_id, xmlized_args] end def job_id DOMAIN + '.' + @args.inject([]) do |m, arg| m << arg.gsub(/\W/, '_') end.join('__') end def xmlized_args @args.inject([]) do |m, arg| m << "\t\t#{arg}" end.join("\n") end end end